while loop in the PHP code excecuting infinite times
<?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
add a comment |
<?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
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 abreakif$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
add a comment |
<?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
<?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
php while-loop strpos
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 abreakif$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
add a comment |
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 abreakif$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
add a comment |
2 Answers
2
active
oldest
votes
strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.
add a comment |
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!
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.
add a comment |
strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.
add a comment |
strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.
strpos returns false, which evaluates to 0, when your remaining $string doesn't contain $find and the loop starts over again.
answered Dec 31 '18 at 14:06
IsaacIsaac
3901725
3901725
add a comment |
add a comment |
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!
add a comment |
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!
add a comment |
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!
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!
answered Dec 31 '18 at 16:39
Stephanie TempleStephanie Temple
1923
1923
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
breakif$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