Simple things of PHP can be just as fun as the advanced things in PHP!
To prove that I wanted to make a small quiz, and here it is.
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.
Have fun!
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
<?php
// What do you expect from this ?
echo print( 1 ); ?>
What wil eval to true
<?php
// How many times will we get 'true' ?
$boolean = false;
echo ($boolean) ? 'true' : 'false';
$boolean = '0';
echo ($boolean) ? 'true' : 'false';
$boolean = '00';
echo ($boolean) ? 'true' : 'false';
First a cast, then an increment, but what is shown?
<?php
// What number will you get to see?
$integer = (int) 'PHP';
echo ++$integer;
Float boat
<?php
// What number will be output?
$a = 0.1 + 0.7;
echo (int) ($a*10) + 1 ;
Array key fun
<?php
$a[ 1 ] = "The one";
$a["a"] = "The second";
$a["1"] = "The who?";
$a["A"] = "CaSe FuN";
$a["01"]= "The last";
echo count($a); // How many do you expect?
Array count
<?php
$one = range(1, 3);
$two = 7;
echo count($one) + count($two); // How much will the be?
Array key existence
<?php
// Will it be isset?
$a = range(10,20);
$a[11] = "The one and only";
$a['11'] = null;
unset($a[1]);
if (isset($a[11])) {
echo "Yes, it's isset!";
} else {
echo "No, it's not isset.";
}
Array evil sort
<?php
$a = array(0, 1, 0.1=>'a', 1.1=>'A');
sort($a);
echo count($a); // Is it what you expected?
Array reference voodoo
<?php
$a = array('i','b','b','q','b','!');
foreach($a as &$value) {}
foreach($a as $value) {}
echo str_rot13( implode('', $a) ); // how many letters ?