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.
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:
PHP Extract numbers from a string
(8 answers)
Closed 7 years ago.
What is the best way to extract the number in the following string:
"This is a test string {2461245} and I need to extract the number"
Thank you.
This should help you :
$myText = 'This is a test string {2461245} and I need to extract the number';
preg_replace("/[^0-9]/","",$myText);
What you are saying there is : I want to replace everything which is not a number in the myText variable by "", which means everything which will stay in the end is your number.
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).
This question already has answers here:
PHP string number concatenation messed up
(5 answers)
Closed 8 years ago.
I wrote a simple script for echoing total of two variables to <h1> tag, but it gives strange results
<?php
$i=10; $j=20;
var_dump($i);
var_dump($j);
echo '<h1>'.$i+$j.'</h1>';
?>
Result of this script:
int(10)
int(20)
20</h1>
I was expecting 30 but it ate up <h1> tag, how can i do this as expected?
Try this:
echo '<h1>'.($i+$j).'</h1>';
This explains why you need to concatenate like this. Here is a working demo.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Strange echo, print behaviour in PHP?
The following PHP code:
print (2).(3 * (print 3));
displays "323" to the output? How is it processed?
This is because of the brackets (operation precedence) - the
(print 3)
in the end of the line displays the first digit of the final output (3), but all PHP print statements return 1. Always (check the manual). So after this, we've got:
print (2).(3 * 1);
which is the same as:
print (2).(3);
Now it's just a simple concatenation which will output "23". So we've got "323" displayed.
Note that
print (2).(1 - (print 3));
would display "320".