Why does while(0) not execute the code? [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
<?php
$efFect=0;
$find='is';
$find_len=strlen($find);
$string22 ='this is space is function';
while ($str_position =strpos($string22,$find,$effect)){
echo $find.'the postion in '. $str_position.'<br>';
$efFect = $str_position + $find_len ;
}
?>

0 is a falsy value. You should make your check more reliable by checking if strpos actually returns false.
<?php
$effect=0; // not $efFect as variables names are case sensitive in PHP
$find='is';
$find_len=strlen($find);
$string22 ='this is space is function';
while (($str_position = strpos($string22, $find, $effect)) !== False){
echo $find.'the postion in '. $str_position.'<br>';
$effect = $str_position + $find_len ;
}
?>
From the documentation:
Returns the position of where the needle exists relative to the beginning of the haystack string (independent of offset). Also note that string positions start at 0, and not 1.
Returns FALSE if the needle was not found.
Someone mentioned that your code creates an infinite loop but this is not the case. strpos accepts an offset as the third parameter which you have correctly used. On each iteration the offset is incremented such that the new search will begin at the position where the last found result string ends, hence avoiding the infinite loop.

Related

Why is array_pop() returning the last item of the array instead of deleting it? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
array_pop() is returning the last item of the array instead of deleting the last item of the array. Why is this happening?
$blam = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$blam = explode("/", $blam);
$blam = array_pop($blam);
print_r($blam);
You can test out the php here.
Problem
You are doing the pop operation correctly, but immediately you are assigning the currently removed element to the variable $blam. So it iffectively makes that variable hold the value i.e the element that was popped.
Solution
Don't assign the value returned by pop function to the variable.
Code
$blam = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$blam = explode("/", $blam);
array_pop($blam);
print_r($blam);
You're overriding the variable. If you need the removed value, change it to
$foo = array_pop($blam);
The function is behaving as expected. From the docs:
array_pop() pops and returns the last value of the array, shortening the array by one element.
When you use
$blam = array_pop($blam);
You're saying:
"Remove the last element from $blam and store it inside $blam", replacing the previous content with said element.
To elaborate, I think the confusion here is that array_pop does NOT return the new array, it instead returns the removed value, changing the original array.

Using strpos to finds words in a string [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
so I am trying to check if a string contains a specific set of words, and set a variable if they do. I have the code below, first it was working great except that it would not work if one of the key words was the very first word, so I looked at the php manual and found out about === and implemented that, but now it sets the variable to one every time even if none of the words are found!
So basically it reads a text file to an array, the text file contains the key words, then it checks the string to see if any of those key words are present in the sting. If none of the key words are found then wc would equal 0 and so would inc. If it finds any then wc is incremented every time a word is found, and if it is greater than 0 it will set inc to 1 to flag that key words were included.
Hopefully that all makes sense....
Here is my code:
$inc = 0;
$list = file("filter.txt", FILE_IGNORE_NEW_LINES);
$cnt = count($list);
$wc = 0;
for ($i=0; $i<$cnt; $i++)
{
if (strpos($string,$list[$i]) === false)
{
$wc ++;
}
if ($wc > 0)
{
$inc = 1;
}
}
It doesn't work because you are increasing wc if the string is not found.
You have to replace === with !==.

PHP - Trouble with Brakeing from a loop [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Question First: Am i going about this the wrong way?
I had trouble braking from my for each with a conditional if inside. I wrote this just to save CPU Time yet i could not brake from my foreach.
I am trying to comply with with Active Directory strict password enforcement and i don't believe in having maximum length passwords. As i have found some websites have Maximum length requirements.
foreach(count_chars($new_password, 1) as $key => $value){//Strength Test Results can be derived from $value
if(!ctype_upper(chr($key))){$check_upper=1;}//if Upper-case
if(!ctype_lower(chr($key))){$check_lower=1;}//if Lower-case
if(!ctype_digit(chr($key))){$check_digit=1;}//if Numeric
if(!ctype_punct(chr($key))){$check_punct=1;}//if Symbol
if($check_upper + $check_lower + $check_digit + $check_punct>= 3){
break;
}//Save us from checking the entire string
}
You use the wrong keyword! It should mean break
foreach(count_chars($new_password, 1) as $key => $value){//Strength Test Results can be derived from $value
if(!ctype_upper(chr($key))){$check_upper=1;}//if Upper-case
if(!ctype_lower(chr($key))){$check_lower=1;}//if Lower-case
if(!ctype_digit(chr($key))){$check_digit=1;}//if Numeric
if(!ctype_punct(chr($key))){$check_punct=1;}//if Symbol
if($check_upper + $check_lower + $check_digit + $check_punct>= 3){
break;
}//Save us from checking the entire string
}
Well that's simple: break
Brake is what a car does.
http://php.net/manual/en/control-structures.break.php

if( strpos($_SERVER['REQUEST_URI'], '/') === TRUE) doesn't work [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
if( strpos($_SERVER['REQUEST_URI'], '/') === TRUE) doesn't work.
What I am trying to is achieve that when on the homepage e.g. http://www.amazon.co.uk/ that it will trigger what is inside the statement.
This may be complicated by the fact this is a redirect from another page
var_dump($_SERVER['REQUEST_URI']); = /string(1)
echo $_SERVER['REQUEST_URI']; = "/"
php version 5.4
Thank you for your help, I have fixed my issue
That is because, like the docs tell you, strpos() never returns true.
It either returns a positive integer (found) or boolean false (not found).
So check for !== false.
The strpos() function, returns a numeric value representing the position of the second string in the first string. Hence it will never be equal to TRUE.
strpos() can return FALSE if the string isn't found, hence you should be writing
if( strpos($_SERVER['REQUEST_URI'], '/') !== FALSE)
strpos() from manual:
strpos — Find the position of the first occurrence of a substring in a
string
So you are checking if the return is TRUE, and it never is. You need to check it with !== FALSE. Why? Because === checks for the value AND the type of variable. When you do == it will check only for value, and when value is 0 PHP will cast it to FALSE, since PHP is loosely typed.

PHP explode(), delimiter being returned? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 11 months ago.
Improve this question
I'm not sure why but the explode function doesn't seem to be working for me.
I have a string which contains one or more sets of comma-seperated values. These sets are delimied by starting / ending square brackets.
After stripping off the ending "[" and "]", I thought it would be simple to then use the explode function to get the results seperated by "][". Instead, I get something weird.
$rawInserts = '[1,2,3,4,5][2,3,4,5,6][3,4,5,6,7]';
$the_inserts = substr($rawInserts,1,strlen($rawInserts)-2);
echo "$the_inserts \n"; //returns "1,2,3,4,5][2,3,4,5,6][3,4,5,6,7"
$inserts = explode($the_inserts , "][");
echo print_r($inserts)."\n"; // returns one item array containing "][";
why is it returning "]["? (FYI, I tried this exact example and it fails).
Thanks in advance.
array explode ( string $delimiter , string $string [, int $limit ] )
Delimiter first, string second.
Switch the parameters. It's delimiter first and string as second parameter:
$inserts = explode('][', $the_inserts);
it should be $inserts = explode("][",$the_inserts );
I myself cannot remember the parameters for every function, so just go to the php.net website and search for the explode function.

Categories