【问题标题】:Issues with strpos inside while loop [duplicate]while循环内的strpos问题[重复]
【发布时间】:2014-11-08 00:51:03
【问题描述】:

我的问题主要是关于一个“while 条件”,它在应该返回“FALSE”时显然返回“TRUE”。这是代码:

<?php
$sentence='is is is';
$search='is';
$length=strlen($search);
$offset=0;
$j=1;
while(($pos = strpos($sentence,$search,$offset)) !==FALSE)
{
 echo $j.'. '.$pos.'<br>';
 $offset += $length;
 $j++;
}
?>

返回:

  1. 0
  2. 3
  3. 6

拳头结果显示为0,为假?为什么“while 循环”中的代码运行起来都是一样的?

【问题讨论】:

  • 因为0 !== FALSE.

标签: php while-loop strpos


【解决方案1】:

拳头结果显示为0,为假

我认为你误解了 0 是第一次出现 is 的位置,而不是布尔值 0

$sentence='is is is';
           ^  ^  ^
           0  3  6

【讨论】:

  • 我想我误解了 !== 运算符...
  • 我的答案是否澄清或者我应该进一步扩展
  • 我在这里收到的所有答案都给了我一个非常明确的答案,即没有完全理解操作员在我的循环中的作用。
猜你喜欢
  • 2011-06-19
  • 2021-01-03
  • 2016-03-22
  • 2019-07-23
  • 1970-01-01
  • 1970-01-01
  • 2013-09-28
  • 2020-09-17
  • 1970-01-01
相关资源
最近更新 更多