while loop in the PHP code excecuting infinite times












-2















<?php
$string="I am Azizul hakim.I am a student.I am feeling good today";
$find="am";
$i=0;
$find_length=strlen($find);
while($i<strlen($string))
{
$pos=strpos($string,$find,$i);
echo $pos;
$i=$pos+$find_length;
}
?>


The php code is executing infinite times.Although the limit is restricted by specifying the string length of the string.Why it is executing infinite times?










share|improve this question























  • While not familiar with PHP think about unmet condition of '$i=$pos+$find_length;'

    – bummi
    Dec 31 '18 at 13:57






  • 1





    please debug your code before you post. strpos returns false when your remaining string doesn't contain any $find. so it will loop back to 0 + $find_length => 2, A simple fix would be a break if $pos === false

    – Isitar
    Dec 31 '18 at 14:01













  • Yes I have run that one and it works fine.But what is the problem with this one? @bummi

    – Azizul Hakim Chowdhury
    Dec 31 '18 at 14:02











  • OK thanks got it. @Isitar

    – Azizul Hakim Chowdhury
    Dec 31 '18 at 14:04













  • I guess that strpos can return negative values, when $i (search offset) grows bigger than length of $string. Add an additional condition to while loop e.g. $i >= 0

    – FlorIT
    Dec 31 '18 at 14:06
















-2















<?php
$string="I am Azizul hakim.I am a student.I am feeling good today";
$find="am";
$i=0;
$find_length=strlen($find);
while($i<strlen($string))
{
$pos=strpos($string,$find,$i);
echo $pos;
$i=$pos+$find_length;
}
?>


The php code is executing infinite times.Although the limit is restricted by specifying the string length of the string.Why it is executing infinite times?










share|improve this question























  • While not familiar with PHP think about unmet condition of '$i=$pos+$find_length;'

    – bummi
    Dec 31 '18 at 13:57






  • 1





    please debug your code before you post. strpos returns false when your remaining string doesn't contain any $find. so it will loop back to 0 + $find_length => 2, A simple fix would be a break if $pos === false

    – Isitar
    Dec 31 '18 at 14:01













  • Yes I have run that one and it works fine.But what is the problem with this one? @bummi

    – Azizul Hakim Chowdhury
    Dec 31 '18 at 14:02











  • OK thanks got it. @Isitar

    – Azizul Hakim Chowdhury
    Dec 31 '18 at 14:04













  • I guess that strpos can return negative values, when $i (search offset) grows bigger than length of $string. Add an additional condition to while loop e.g. $i >= 0

    – FlorIT
    Dec 31 '18 at 14:06














-2












-2








-2








<?php
$string="I am Azizul hakim.I am a student.I am feeling good today";
$find="am";
$i=0;
$find_length=strlen($find);
while($i<strlen($string))
{
$pos=strpos($string,$find,$i);
echo $pos;
$i=$pos+$find_length;
}
?>


The php code is executing infinite times.Although the limit is restricted by specifying the string length of the string.Why it is executing infinite times?










share|improve this question














<?php
$string="I am Azizul hakim.I am a student.I am feeling good today";
$find="am";
$i=0;
$find_length=strlen($find);
while($i<strlen($string))
{
$pos=strpos($string,$find,$i);
echo $pos;
$i=$pos+$find_length;
}
?>


The php code is executing infinite times.Although the limit is restricted by specifying the string length of the string.Why it is executing infinite times?







php while-loop strpos






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Dec 31 '18 at 13:50









Azizul Hakim ChowdhuryAzizul Hakim Chowdhury

64




64













  • While not familiar with PHP think about unmet condition of '$i=$pos+$find_length;'

    – bummi
    Dec 31 '18 at 13:57






  • 1





    please debug your code before you post. strpos returns false when your remaining string doesn't contain any $find. so it will loop back to 0 + $find_length => 2, A simple fix would be a break if $pos === false

    – Isitar
    Dec 31 '18 at 14:01













  • Yes I have run that one and it works fine.But what is the problem with this one? @bummi

    – Azizul Hakim Chowdhury
    Dec 31 '18 at 14:02











  • OK thanks got it. @Isitar

    – Azizul Hakim Chowdhury
    Dec 31 '18 at 14:04













  • I guess that strpos can return negative values, when $i (search offset) grows bigger than length of $string. Add an additional condition to while loop e.g. $i >= 0

    – FlorIT
    Dec 31 '18 at 14:06



















  • While not familiar with PHP think about unmet condition of '$i=$pos+$find_length;'

    – bummi
    Dec 31 '18 at 13:57






  • 1





    please debug your code before you post. strpos returns false when your remaining string doesn't contain any $find. so it will loop back to 0 + $find_length => 2, A simple fix would be a break if $pos === false

    – Isitar
    Dec 31 '18 at 14:01













  • Yes I have run that one and it works fine.But what is the problem with this one? @bummi

    – Azizul Hakim Chowdhury
    Dec 31 '18 at 14:02











  • OK thanks got it. @Isitar

    – Azizul Hakim Chowdhury
    Dec 31 '18 at 14:04













  • I guess that strpos can return negative values, when $i (search offset) grows bigger than length of $string. Add an additional condition to while loop e.g. $i >= 0

    – FlorIT
    Dec 31 '18 at 14:06

















While not familiar with PHP think about unmet condition of '$i=$pos+$find_length;'

– bummi
Dec 31 '18 at 13:57





While not familiar with PHP think about unmet condition of '$i=$pos+$find_length;'

– bummi
Dec 31 '18 at 13:57




1




1





please debug your code before you post. strpos returns false when your remaining string doesn't contain any $find. so it will loop back to 0 + $find_length => 2, A simple fix would be a break if $pos === false

– Isitar
Dec 31 '18 at 14:01







please debug your code before you post. strpos returns false when your remaining string doesn't contain any $find. so it will loop back to 0 + $find_length => 2, A simple fix would be a break if $pos === false

– Isitar
Dec 31 '18 at 14:01















Yes I have run that one and it works fine.But what is the problem with this one? @bummi

– Azizul Hakim Chowdhury
Dec 31 '18 at 14:02





Yes I have run that one and it works fine.But what is the problem with this one? @bummi

– Azizul Hakim Chowdhury
Dec 31 '18 at 14:02













OK thanks got it. @Isitar

– Azizul Hakim Chowdhury
Dec 31 '18 at 14:04







OK thanks got it. @Isitar

– Azizul Hakim Chowdhury
Dec 31 '18 at 14:04















I guess that strpos can return negative values, when $i (search offset) grows bigger than length of $string. Add an additional condition to while loop e.g. $i >= 0

– FlorIT
Dec 31 '18 at 14:06





I guess that strpos can return negative values, when $i (search offset) grows bigger than length of $string. Add an additional condition to while loop e.g. $i >= 0

– FlorIT
Dec 31 '18 at 14:06












2 Answers
2






active

oldest

votes


















0














strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.






share|improve this answer































    0














    In cases like this, I believe perl compatible regular expression, (pcre), is always the best choice. Why, because you will use only (1) function / method call, and then simple loop to process the result. With string type functions / methods, each time your $needle is found in your $haystack you will need to call another string type function / method to handle the next occurrence of your $needle being found in your $haystack!






    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988228%2fwhile-loop-in-the-php-code-excecuting-infinite-times%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.






      share|improve this answer




























        0














        strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.






        share|improve this answer


























          0












          0








          0







          strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.






          share|improve this answer













          strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Dec 31 '18 at 14:06









          IsaacIsaac

          3901725




          3901725

























              0














              In cases like this, I believe perl compatible regular expression, (pcre), is always the best choice. Why, because you will use only (1) function / method call, and then simple loop to process the result. With string type functions / methods, each time your $needle is found in your $haystack you will need to call another string type function / method to handle the next occurrence of your $needle being found in your $haystack!






              share|improve this answer




























                0














                In cases like this, I believe perl compatible regular expression, (pcre), is always the best choice. Why, because you will use only (1) function / method call, and then simple loop to process the result. With string type functions / methods, each time your $needle is found in your $haystack you will need to call another string type function / method to handle the next occurrence of your $needle being found in your $haystack!






                share|improve this answer


























                  0












                  0








                  0







                  In cases like this, I believe perl compatible regular expression, (pcre), is always the best choice. Why, because you will use only (1) function / method call, and then simple loop to process the result. With string type functions / methods, each time your $needle is found in your $haystack you will need to call another string type function / method to handle the next occurrence of your $needle being found in your $haystack!






                  share|improve this answer













                  In cases like this, I believe perl compatible regular expression, (pcre), is always the best choice. Why, because you will use only (1) function / method call, and then simple loop to process the result. With string type functions / methods, each time your $needle is found in your $haystack you will need to call another string type function / method to handle the next occurrence of your $needle being found in your $haystack!







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Dec 31 '18 at 16:39









                  Stephanie TempleStephanie Temple

                  1923




                  1923






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53988228%2fwhile-loop-in-the-php-code-excecuting-infinite-times%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Mossoró

                      Error while reading .h5 file using the rhdf5 package in R

                      Pushsharp Apns notification error: 'InvalidToken'