This question already has answers here:
Trying to replace parts of string start with same search chars
(3 answers)
Closed 7 years ago.
I am using str_replace() function to replace some values in array and use new value later in mysql query. However, I found a weird situation that I can't understand. Why these queries aren't returning the same output?
The first version of code returns: 214847
<php? $idPage=array(18,21,22);
$idCompetition=array(2147,2148,2149);
$idT=str_replace($idPage,$idCompetition,18);
echo $idT; ?>
And if I change the order in arrays, the result is: 2147
<php? $idPage=array(21,22,18);
$idCompetition=array(2148,2149,2147);
$idT=str_replace($idPage,$idCompetition,18);
echo $idT; ?>
The 2nd query returns the required result, and I used it in my code, but it's unclear to me why the first query isn't working correctly.
In real code I provide the subject of replacement (e.g. 18) by reading the global variable of page:
global $objPage;
$idT=str_replace($idPage,$idCompetition,$objPage->id);
Thanks.
In first code:
str_replace finds first element of $idPage (== '18') in search string (== '18') and replaces it with '2147', then iterates to second value of $idPage (== '21'), finds it in search string (== '2147' atm) and replaces it with '2148', then iterates to third value of $idPage (== '22') and can't find it in search string (== '214847' atm).
Related
This question already has answers here:
How do I count comma-separated values in PHP?
(5 answers)
Why does counting an empty string return 1?
(3 answers)
Count() returning 1 with comma separated string with explode [duplicate]
(2 answers)
Closed 3 years ago.
i have this data which when tested with direct input it works and file_put_contents confrims that data is exactly same but when i try to get the value through site it gives only 1
i tried to declare array but everytime it gives 1 only
this array count gives count as 6
$total_id_counttt = count(array(13068,13067,13066,13065,13064,13063));
echo $total_id_counttt;
but when i use this here it return 1
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = array($str_finalppo);
$total_id_counttt = count($schools_array);
it returns 1 can anyone tell where i am doing mistake
First of all, Convert the string into an array. Look it in this code you may get the idea.
Use explode function to convert string to array.
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
$total_id_counttt = count($schools_array);
Check more about explode
In PHP, value in quotes('') consider as string.
So when you try to count this is always returning the 1.
Try to convert the string into an array by using explode().
for example:
$str_finalppo ='13068,13067,13066,13065,13064,13063';
$schools_array = explode(',',$str_finalppo);
echo $total_id_counttt = count($schools_array);
this will give you the right answer.
And if you still confuse try to print #schools_array before using dd($schools_array);
It will definitely remove your confusion.
This question already has answers here:
Comparing String to Integer gives strange results
(5 answers)
How does PHP compare strings with comparison operators?
(4 answers)
Closed 3 years ago.
This is my code
$preference = '151000';
$range = 'above';
if($preference <= $range){
echo "Yes"; die;
}else{
echo "No"; die;
}
This provides 'Yes', i want to know why.
You can see it in the php manual. https://www.php.net/manual/en/language.operators.comparison.php
When comparing a string, number or resource with another string, number or resource:
Translate strings and resources to numbers, usual math
Btw: '151000' is a string, not a number. 15100 would be a number.
Here you basically compare two strings and php uses their ASCII codes to compare them. The first symbol 1 is lower than 'a'.
If you want to compare two strings properly, use function:
strcmp()
If you want compare different types, you can read about PHP type comparison tables.
If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically.
Read more here.
This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 7 years ago.
I'd like to check if a string is equal to another string. By equal, I mean that I want to check if the second string includes the first one in order to make mysql join's in a dynamic way. (I hope I'm clear, sorry for my english...)
I've seen some functions as strcmp() but it only checks if it is purely equal.
It's the same as "$var1 === $var2".
Is there a function which can do that ? Or could you give me some leads to do that ?
if (strpos($a,'are') !== false) {
echo 'true';
}
How do I check if a string contains a specific word in PHP?
This question already has answers here:
Strange echo, print behaviour in PHP?
(4 answers)
Closed 9 years ago.
How does this statement get evaluated to produce 234.
echo '3'.(print'2')+3; // output 234.
You are using the echo command to display a string. This string is a concatenation of a string "3" and the result of the call to print '2' added with the number 3.
During the concatenation print '2' is evaluated, thus the output starts with that. Afterwards the concatenated string '34' is printed, where the 4 is the result of 1 (=true, the result of the print call) + 3
Way too much text, but I hope that covers it all.
This question already has answers here:
How to convert some character into numeric in php?
(6 answers)
Closed 9 years ago.
Without using a loop, is there a way (function) to convert, for example, the letter "c" to its numeric sequence in the alphabet (3)?
I'm trying to take a literal string - $var = "c" and apply a function to it that returns 3.
Are there any built-in PHP functions that do this? I can't find any online and would rather avoid writing the function if necessary.
Anyone know of such a conversion function?
Yup.
$alphabet_position = ord(strtoupper($character)) - ord('A');