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条)

报告相同问题?

悬赏问题

  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面