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
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
() );
Part 4 of the PHP Quiz series! A few questions to crack your brain about, or perhaps you know them all? Try them and find out! Also do read the idea behind these quizzes, here: The PHP Quiz seriesAs always, think of the answer before you execute the code
Tracked: Nov 02, 08:23