Show tagged entries
Thursday, April 24. 2008
Posted by Mark van der Velden in PHP, PHPQuiz, PlanetPHP
Simple things of PHP can be just as fun as the advanced things in PHP!
Below you'll see 10 pieces of PHP5 code, the goal is to figure out the code and know the output before you actually run it.
Comment and closing tag
<?php // Without cheating.. What will you see when you run this single line of ?\?> php code
Echo print and get what
What wil eval to true
First a cast, then an increment, but what is shown?
Array key fun
Array key existence
Array evil sort
Array reference voodoo
Display comments as (Linear | Threaded)
Nice quiz! I do recognize some ZCE study guide references here
Oh definitely, it's the books like that which give you good inside knowledge!
Not long ago ordered the ZCE study guide ..thanks for the motivation Dynom!
Hm, I didn't do that bad: 6 out of 10 is good enough for me. Although I get most of them, I really don't see what the hell is happening in the last one though. Care to elaborate?
Because it's passed by references it's created as referenced variable. It therefore holds the second last variable during the second loop (If it would iterate one more time it would be the exclamation mark rather then a 'b')
Dynom.. I am still waiting for part 2 .. when can I expect this?
"First a cast, then a increment but what is shown?" didn't surprise me by itself, but it led me to try it without the casting...
I never knew that ++ does that to strings!
nice quiz. got 9 out of 10 because read ZCE guide and prepaing for exam. Specially the array voodoo is an tricky one
only question i didn't get is the evaluation of $boolean = '00';
forgot that == false only maches false & 0/'0'
I knew I didn't know PHP well enough, but you just had to go and prove it to me!
I would very much like to see an explanation of why the Float Boat question output is 8.
For that matter, this quiz would be much more valuable if there were a discussion of each question on a separate page. Would that be possible?
The reason the output is 8 and not 9, is because 0.1 + 0.7 is stored as 0.79999999999~ when multiplied the value remains stored as such and when casted to an int everything past floating point gets discarded. Resulting in 7. And 7 + 1 = 8.
0.1 + 0.7 = 0.799~ * 10 = (int) 7.999~ = 7 + 1 = 8
This is only scenario where it will happen (that I know about) when you try it with different values you'll notice that the outcome is what you expect.
I could make a page about every question explaining the how and the why, but I suggest you just ask a question and I'll try to answer it.
Thanks for the explanation. So I don't understand how anybody (see #6 ivar) got that Float Boat question right. Do you know? Long, sad experience?
Experience, reading about it or hearsay. The problem here is the cast causes the unexpected. If the example had used round() the outcome would be what you expected.