Looking for an explanation

php.internals

Oliver Block

20 years ago
Hello list, as you are familiar with the source code of php it might be easy to explain how the brain of php interprets the following code: print ($life = "hard") -1; print "\nlife: {$life}\n"; Best regards, Oliver P.S. Actually, the origin of that code was the output of a string literal concatenated with count($array) -1

Sara Golemon

20 years ago
> print ($life = "hard") -1; >
Store "hard" in $life, Substract 1 from the result of that assignment ("hard") "hard", when used in a string context is casted to int (0) 0 - 1 == -1 Output result of subtraction (casted to string): "-1"
> print "\nlife: {$life}\n"; >
Concatenate "\nlife: " with contents of $life ("hard") and concatenate that with "\n" Output the result of concatenation -Sara

David Tulloh

20 years ago
Oliver Block wrote:
> Hello list, > > as you are familiar with the source code of php it might be easy to explain > how the brain of php interprets the following code:
I think that this would be better suited to the startard PHP list. I would suggest posting any further followups there.
> > print ($life = "hard") -1;
($life="hard") assigns "hard" to $life and returns "hard" "hard" -1 casts "hard" to an integer to do a math operation, "hard" casts to 0 0 -1 is -1, so -1 is printed
> print "\nlife: {$life}\n";
Now you simply print the value of $life, "hard" David