php 等待函数

发布于 2024-11-16 21:20:15 字数 436 浏览 2 评论 0原文

这是一些 PHP 代码示例;

<?php
function myFunc() {
    echo "blue";
}

for ($i=1;$i<=5;$i++) {
    echo "I like the colour " . myFunc() . "</br>";
}
?>

这会生成输出;

blueI like the colour 
blueI like the colour 
blueI like the colour 
blueI like the colour 
blueI like the colour

在我的实际项目中,myFunc 正在进行 MySQL 调用(如果提到这一点有任何区别的话)。我怎样才能让我的循环在继续之前等待该函数返回,否则输出将像上面那样乱序。

This is some example PHP code code;

<?php
function myFunc() {
    echo "blue";
}

for ($i=1;$i<=5;$i++) {
    echo "I like the colour " . myFunc() . "</br>";
}
?>

This generates the output;

blueI like the colour 
blueI like the colour 
blueI like the colour 
blueI like the colour 
blueI like the colour

In my actual project, myFunc is making an MySQL call (if it makes any difference mentioning that). How can I make my loop wait for that function to return before carrying on otherwise the output is out of order like above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

娇妻 2024-11-23 21:20:15

问题在于 myFunc() 在连接之前进行评估。从 myFunc() 返回值并在循环中使用该值,而不是在 myFunc() 中回显。

The problem is that myFunc() is evaluated before concatenating. Return the value from myFunc() and use that in your loop, rather than echoing in myFunc().

叫嚣ゝ 2024-11-23 21:20:15

尝试将代码更改为:

<?php
function myFunc() {
    return "blue";
}

for ($i=1;$i<=5;$i++) {
    echo "I like the colour " . myFunc() . "</br>";
}
?>

注意 return 而不是 echo

Try changing the the code to this:

<?php
function myFunc() {
    return "blue";
}

for ($i=1;$i<=5;$i++) {
    echo "I like the colour " . myFunc() . "</br>";
}
?>

Take note the return rather than echo.

财迷小姐 2024-11-23 21:20:15

您不想在函数中使用echo。请改用 return

当您评估循环时,首先评估函数以查看是否有返回值要放入字符串中。由于您调用 echo 而不是 return,因此会发生来自函数的回显,然后发生来自循环的回显。

You do not want to use echo in your function. Use return instead.

When you evalute the loop, the function is evaluated first to see if there is a return value to put into the string. Since you call echo and not return, the echo from the function is happening and then the echo from the loop is happening.

飘落散花 2024-11-23 21:20:15

使用返回语句:

 <?php
 function myFunc() {
     return "blue";
 }

Use the return statement:

 <?php
 function myFunc() {
     return "blue";
 }
别闹i 2024-11-23 21:20:15
  <?php
  function myFunc() {
      return "blue";
  }

  for ($i=1;$i<=5;$i++) {
      $color = myFunc();
      echo "I like the colour " . $color . "</br>";
  }
  ?>
  <?php
  function myFunc() {
      return "blue";
  }

  for ($i=1;$i<=5;$i++) {
      $color = myFunc();
      echo "I like the colour " . $color . "</br>";
  }
  ?>
盗琴音 2024-11-23 21:20:15

这不是你的问题,你的问题是你有两个行动。

you are concatenating a string
you are calling a function.

结果字符串是“I like the color”之间的串联。 myFunc() 。 "",但在连接这些值之前,必须执行 myFunc()。

当您执行 myFunc 时,您正在向输出写入“blue”。

那么,结果是(对于每一行)。

蓝色我喜欢这个颜色
(首先,评估 myFunc() 然后将返回值 (void) 连接到静态字符串。

也许您想这样做:

<?php    
function myFunc() {
        return "blue";
    }
for ($i=1;$i<=5;$i++) {
    echo "I like the colour " . myFunc() . "</br>";
}

?>

this is not your problem, your problem is that you have two actions.

you are concatenating a string
you are calling a function.

The result string is the contcatenation between "I like the colour " . myFunc() . "", but before you can concatenate these values, you must execute myFunc().

When you execute myFunc, you are writing "blue" to the output.

then, the result are (for every line).

blueI like the colour
(first, evaluate myFunc() an then concatenate the return value (void) to the static string.

maybe you want to do that:

<?php    
function myFunc() {
        return "blue";
    }
for ($i=1;$i<=5;$i++) {
    echo "I like the colour " . myFunc() . "</br>";
}

?>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文