dongwu9647
2015-12-12 23:40可以在while循环中声明变量吗?
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.
- 点赞
- 回答
- 收藏
- 复制链接分享
2条回答
为你推荐
- 如何在for循环初始化中声明uint8,因为for循环初始化中不允许使用var
- it技术
- 互联网问答
- IT行业问题
- 计算机技术
- 编程语言问答
- 1个回答
- 该程序运行时刀输入序号的时候输入不进去为什么?有大神知道吗
- c语言
- 1个回答
- 为什么python中while不能使用全局变量呢??
- python
- 开发语言
- 1个回答
- qt按列读取txt内的数据 为什么只能读前三列 不能读之后的呢
- c++
- 1个回答
- 大佬们,能帮忙改成指针式的程序,成绩也可以升序或者降序输出
- c语言
- c++
- 1个回答