dongqichang7988 2011-07-16 21:10
浏览 49
已采纳

Php无限循环一遍又一遍?

<?php

$offset = 0;
$find = "is";
$find_length = strlen($find);
$string = "This is a string, and it is an example.";

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

echo $find. " Found at ". $string_position ."<br>";
$offset = string_position + find_length;

}


?>

keeping getting "is Found at 2" over and over . i am expecting " is found at 2", then 5, then 25

  • 写回答

3条回答 默认 最新

  • doutui6241 2011-07-16 21:14
    关注
    $offset = string_position + find_length;
    

    Use variables instead of constants

    $offset = $string_position + $find_length;
    

    If the needle is found at the beginning of the string, strpos() returns 0, which means, that the loop will never start.

    while(($string_position = strpos($string, $find, $offset)) !== false) {
      // code
    }
    

    Additional change your error settings in your development environment

    error_reporting(E_ALL | E_STRICT);
    

    Now you will get this as notice

    PHP Notice:  Use of undefined constant string_position - assumed 'string_position' in php > shell code on line 4
    PHP Stack trace:
    PHP   1. {main}() php shell code:0
    
    Notice: Use of undefined constant string_position - assumed 'string_position' in php shell > code on line 4
    
    Call Stack:
        1.2172     636208   1. {main}() php shell code:0
    
    PHP Notice:  Use of undefined constant find_length - assumed 'find_length' in php shell code on line 4
    PHP Stack trace:
    PHP   1. {main}() php shell code:0
    
    Notice: Use of undefined constant find_length - assumed 'find_length' in php shell code on line 4
    
    Call Stack:
        1.2172     636208   1. {main}() php shell code:0
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器