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

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);
next($array);
foreach($array
as $v) {
  echo $v;
}
 

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

Typo?

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

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

Nesting structures

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

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;
var_dump($a,
$b);
 

Form fun

What will the output be?
<form method=“post” action=“” enctype=“text/plain”>
    <input name=“search”
type=“text”>
    <input type=“submit”>
</form>
<?php
        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 ];

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

    $c .= $rox[
$i ];

    $c–;
}

var_dump( $b,
$c );

Referenced returning

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

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

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

  function
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”;

var_dump($a,
$b, $c);

For what it’s worth

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

echo
($obj
* 2),
($int
* 3),
($string
* 4);

Global precedence

function multiply($b) {

    $a = 100;
    global $a;

    return $a * $b;
}

echo
multiply(100);
 

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()
);
 

Advertisements

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