可以在while循环中声明变量吗?(Can a variable be declared within a while loop?)

编程入门 行业动态 更新时间:2024-10-25 22:36:37
可以在while循环中声明变量吗?(Can a variable be declared within a while loop?)

我试图理解这个视频中的基本PHP代码,它演示了如何在一个更大的字符串中列出特定字符串的所有位置(在这种情况下,将列出$ string中$ find的所有位置):

<?php $find = 'is'; $find_leng = strlen($find); $string = 'This is a string, and it is an example.'; while ($string_position = strpos($string, $find, $offset)) { echo '<strong>'.$find.'</strong> found at '.$string_position.'<br>'; $offset = $string_position + $find_length; } ?>

令我困惑的是,在while循环的()中,似乎正在声明一个新变量$ string_position。 但是while循环不是输入0或1,布尔值吗? $ string_position变量不是true / false变量,它用于存储使用strpos()函数传递的字符串的位置。

基本的while循环我习惯使用来自w3schools的比较运算符:

<?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?>

可以在while循环中声明变量吗? 如果是这种情况,它在这个例子中是如何工作的? 这是我关于Stack Overflow的第一个问题,所以我希望我能在正确的位置发布这个问题,这不是一个问题的新手。

I am trying to understand the basic PHP code in this video, which demonstrates how you can list all the positions of a specific string within a larger string (in this case, all the positions of $find within $string will be listed):

<?php $find = 'is'; $find_leng = strlen($find); $string = 'This is a string, and it is an example.'; while ($string_position = strpos($string, $find, $offset)) { echo '<strong>'.$find.'</strong> found at '.$string_position.'<br>'; $offset = $string_position + $find_length; } ?>

What confuses me is that within the () of the while-loop, it seems that a new variable $string_position is being declared. But doesn't the while-loop take an input of 0 or 1, a Boolean? The $string_position variable is not a true/false variable, it is meant to store the position of the string it is passed with the strpos() function.

The basic while-loop I'm used to uses a comparison operator like this one from from w3schools:

<?php $x = 1; while($x <= 5) { echo "The number is: $x <br>"; $x++; } ?>

Can a variable be declared in a while-loop? And if that is the case, how does it work in this example? This is my first question on Stack Overflow so hopefully I'm posting this in the right place and it's not too newbie of a question.

最满意答案

是的,while循环使用类似boolean的参数。 通过做

while ($string_position = strpos($string, $find, $offset)) {

它首先执行括号内的内容,然后计算结果,以确定循环是否应该退出。

如果没有找到针, strpos将返回False,因此$string_position将为false,因此评估结果也将为False,循环将退出。 如果找到针,则在归属后继续循环。

另外需要注意的是, strpos可能会返回一个计算结果为False的非布尔值,因此请注意该结构。

关于strpos的参考

Yes, while loops take boolean-like as arguments. By doing

while ($string_position = strpos($string, $find, $offset)) {

it first executes what is inside the parenthesis, then evaluates the result of that to determine if the loop should quit or not.

strpos will return False if the needle was not found, so $string_position will be false and as such the evaluation result will also be False and the loop will quit. If the needle is found, then loop continues after the attribution.

Also it should be noted that strpos might return a non-boolean that evaluates to False, so be careful with that construction.

Reference on strpos

更多推荐

本文发布于:2023-08-06 02:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1442149.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   声明   variable   declared   loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!