Narendra Dhami

My Site

PHP Quizes

Posted by Narendra Dhami on September 25, 2009

PHP Quiz part 1

Simple things of PHP can be just as fun as the advanced things in
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

  // What do you expect from this ?
  echo print( 1 ); ?>

What wil eval to true

  // 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?

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

Float boat

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

Array key fun


        $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


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

Array key existence


        // Will it be isset?

= range(10,20);
$a[11]          = “The one
and only”;
$a[’11’]        = null;

        if (isset($a[11])) {
echo “Yes, it’s isset!”;
        } else {
echo “No, it’s not isset.”;

Array evil sort


$a      = array(0, 1, 0.1=>’a’,

        echo count($a); // Is it what
you expected?

Array reference voodoo


        $a =
        foreach($a as
&$value)  {}
        foreach($a as
$value)   {}

str_rot13( implode(”, $a) ); // how many letters

PHP Quiz part 2

A short quiz this time, but that doesn’t make it less fun. Do you
know the answer to all of them? Get a cup of coffee and kill 10 minutes
with round two…

As always, think of the answer before you execute the code or
look it up. You can find round one here.

Array pointer

What will the output be ?
$array = range(0,5);
as $v) {
  echo $v;

ArrayAccess and isset fun

isset or not isset, thats the question.
$a          = new ArrayObject;
$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;


The output might be confusing..


How many notices will be thrown?
function a( & $array) {}
function b($array) {}

OR or || – operator precedence

What will the output be?
( 1
? 0 : 1
or 1 ),
( 1
? 0 : 1
|| 1 );

Nesting structures

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

PHP Quiz part 3

Welcome to another part of the PHP Quiz series, again some
interesting questions to crack your brain about. If you have some nice
additions or questions, be sure to leave a comment. Enjoy part three!

As always, think of the answer before you execute the code or
look it up. You can find round two here.

Unset cast

What is the type of $a and what is the type of $b
$a = “A”;
$b = (unset) $a;

Form fun

What will the output be?
<form method=“post” action=“” enctype=“text/plain”>
    <input name=“search”
    <input type=“submit”>
        error_reporting( E_ALL );
        echo (string) filter_input(INPUT_POST, ‘search’);

Fun with strings

Strings in PHP are versatile, but how versatile are they… What
will the output be?$juggling = “Itffkhmf”;

$rox = “Spy”;

$b = $c
= “”;
for ($i=0;
$i<strlen($juggling); $i++) {

    $b .= $juggling[
$i ];

for ($i=0;
$i<strlen($rox); $i++) {

    $c .= $rox[
$i ];


var_dump( $b,
$c );

Referenced returning

Do you know the referencing details? What will the output be?class RR {
  public $varA = “z”,
= “z”,
= “z”;

& getA() {
    return $this->varA;

& getB() {
    return $this->varB;

getC() {
    return $this->varC;

$rr = new RR;

// Option A,
method only

$a = $rr->getA();

$rr->varA = “a”;

// Option B,
both method and assign

$b = & $rr->getB();
$rr->varB = “b”;

// Option C,
assign only

$c = & $rr->getC();
$rr->varC = “c”;

$b, $c);

For what it’s worth

$obj    = new stdClass;
$int    = 1;
$string = “a”;

* 2),
* 3),
* 4);

Global precedence

function multiply($b) {

    $a = 100;
    global $a;

    return $a * $b;


Making the (seemingly) impossible, possible

Don’t use codepad to figure out whats happening, since that gives
a big hint right away. Try it on the CLI and think about what is
happening first.class A {
  private $A;

class B extends A {
  public $AA;

// How many
keys, and what are the keys?

var_dump( (array) new B()

Leave a Reply

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

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

Google photo

You are commenting using your Google 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 )

Connecting to %s