PHP QUIZ

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!

Array pointer

What will the output be ?

$array = range(0,5);
next($array);
foreach($array as $v) {
  echo $v;
}

 Ouput:
——-
012345

ArrayAccess and isset fun

isset or not isset, thats the question.
$a          = new ArrayObject;
$a[‘fubar’] = NULL;
$null       = NULL;

if (isset($null)) {
        echo 1;
} else {
        echo 2;
}

if (isset($a[‘fubar’])) {
        echo 1;
} else {
        echo 2;
}

if (array_key_exists(‘fubar’, $a)) {
        echo 1;
} else {
        echo 2;
}

Ouput:
——-
211

Typo?

The output might be confusing..
array(4>’a’)

Ouput:
——-
empty value

References

How many notices will be thrown?
error_reporting(E_ALL);
function a( & $array) {}
function b($array) {}
a($array);
b($array);
OR or || – operator precedence

What will the output be?
echo ( 1 ? 0 : 1 or 1 ), ( 1 ? 0 : 1 || 1 );
 
 Ouput:
——-
10

Nesting structures

Will this error, or not?
if (1):
    echo ‘one’;
    if(2) {
        echo ‘two’;
    }
else:
    echo ‘not one’;
endif;

Ouput:
——-
Parse Error

Comment and closing tag

<?php // 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 ); ?>

Ouput:
——-

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’;

Ouput:
——-
 falsefalsetrue

First a cast, then a increment but what is shown?

<?php
  // What number will you get to see?
  $integer = (int) ‘PHP’;
  echo ++$integer;

Ouput:
——-
 1

Float boat

 <?php
        // What number will be output?
        $a = 0.1 + 0.7;
        echo (int) ($a*10) + 1 ;

Ouput:
——-
8

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?

Ouput:
——-
Warning: Cannot use a scalar value as an array

Array count

 <?php

        $one    = range(1, 3);
        $two    = 7;
        echo count($one) + count($two); // How much will the be?

Ouput:
——-
4

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.”;
        }

Ouput:
——-
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?

Ouput:
——-
2

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 ?

Ouput:
——-
voodoo

Advertisements

1 Comment

  1. shakti singh said,

    May 16, 2009 at 9:16 am

    not much better

    takeovers.wordpress.com


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: