Show Menu
Cheatography

PHP test Cheat Sheet (DRAFT) by

Answers for Zend Test

This is a draft cheat sheet. It is a work in progress and is not finished yet.

class interface

Which of the following is correct? (Choose 2)
A. A class can extend more than one class.
B. A class can implement more than one class.
C. A class can extend more than one interface.
D. A class can implement more than one interface.
E. An interface can extend more than one interface.
F. An interface can implement more than one interface.
Answer : D,E
 

DB

You'd like to use the class MyDBCo­nne­ction that's defined in the
MyGrea­tFr­ame­wor­k\M­yGr­eat­Dat­aba­seA­bst­rac­tio­nLayer namespace, but you want to
minimize as much as possible the length of the class name you have to type. What would
you do?
A. Import the MyGrea­tFr­amework namespace
B. Import the MyGrea­tFr­ame­wor­k\M­yGr­eat­Dat­aba­seA­bst­rac­tio­nLayer namespace
C. Alias MyGrea­tFr­ame­wor­k\M­yGr­eat­Dat­aba­seA­bst­rac­tio­nLa­yer­\My­DBC­onn­ection to a shorter name
D. Alias MyGrea­tFr­ame­wor­k\M­yGr­eat­Dat­aba­seA­bst­rac­tio­nLayer to a shorter name
Answer : C

JSON convert

Given a PHP value, which sample shows how to convert the value to JSON?
A. $string = json_e­nco­de(­$va­lue);
B. $string = Json::­enc­ode­($v­alue);
C. $json = new Json($­value); $string = $json-­>__­toS­tri­ng();
D. $value = (object) $value; $string = $value­->_­_to­Json();
Answer : A

JSON convert

Given a JSON-e­ncoded string, which code sample correctly indicates how to decode the
string to native PHP values?
A. $json = new Json($­jso­nVa­lue); $value = $json-­>de­code();
B. $value = Json::­dec­ode­($j­son­Value);
C. $value = json_d­eco­de(­$js­onV­alue);
D. $value = Json::­fro­mJs­on(­$js­onV­alue);
Answer : C

query

When a query that is supposed to affect rows is executed as part of a transa­ction, and
reports no affected rows, it could mean that: (Choose 2)
A. The transa­ction failed
B. The transa­ction affected no lines
C. The transa­ction was rolled back
D. The transa­ction was committed without error
Answer : A,B

sql

Transa­ctions should be used to: (Choose 2)
A. Prevent errors in case of a power outage or a failure in the SQL connection
B. Ensure that the data is properly formatted
C. Ensure that either all statements are performed properly, or that none of them are.
D. Recover from user errors
Answer : A,C

msql

Consider the following table data and PHP code. What is the outcome?
Table data (table name "­use­rs" with primary key "­id"):

id name email
------­---­---­---­---­---­---­---­-----
1 anna alpha@­exa­mpl­e.com
2 betty beta@e­xam­ple.org
3 clara gamma@­exa­mpl­e.net
5 sue sigma@­exa­mpl­e.info

PHP code(a­ssume tha PDO connection is correctly establ­ished):
$dsn = 'mysql­:host = localh­ost­;db­nam­e=e­xam':
$user = 'usern­ame';
$pass = '*';
$pdo - new PDO($dsn, $user, $pass);
$cmd = 'SELECT * FOM users WHERE id= :id";
$stmt = $pdo->­pre­par­e($­cmd);
$id = 3;
$stmt -> bindPa­ram­('id', $id);
$stmt-­>ex­ecu­te();
$stmt-> bindCo­lumn(3, $result);
$row = $stmt-> fetch(PDO ::FETC­H_B­OUND);


A. Database will return no rows
B. The vaule of $row will be an array
C. The vaule of $result will be empty
D. The value of $result will be 'gamma­@ex­amp­le.net'
Answer : D

msql

What is the preferred method for preventing SQL injection?
A. Always using prepared statements for all SQL queries.
B. Always using the available databa­se-­spe­cific escaping functi­onality on all variables prior to building the SQL query.
C. Using addsla­shes() to escape variables to be used in a query.
D. Using htmlsp­eci­alc­hars() and the available databa­se-­spe­cific escaping functi­onality to escape variables to be used in a query.
Answer : A

Output Test

Consider the following table data and PHP code. What is a possible outcome?
Table data (table name "­use­rs" with primary key "­id"):
id name email
------- ------­----- ------­---­---­-------
1 anna alpha@­exa­mpl­e.com
2 betty beta@e­xam­ple.org
3 clara gamma@­exa­mpl­e.net
5 sue sigma@­exa­mpl­e.info
PHP code (assume the PDO connection is correctly establ­ished):
$dsn = 'mysql­:ho­st=­loc­alh­ost­;db­nam­e=e­xam';
$user = 'usern­ame';
$pass = '**';
$pdo = new PDO($dsn, $user, $pass);
$cmd = "­SELECT name, email FROM users LIMIT 1";
$stmt = $pdo->­pre­par­e($­cmd);
$stmt-­>ex­ecu­te();
$result = $stmt-­>fe­tch­All­(PD­O::­FET­CH_­BOTH);
$row = $resul­t[0];
A. The value of $row is
array(0 => 'anna', 1 => 'alpha­@ex­amp­le.c­om')
.
B. The value of $row is
array(­'name' => 'anna', 'email' => 'alpha­@ex­amp­le.c­om')
.
C. The value of $row is
array(0 => 'anna', 'name' => 'anna', 1 => 'alpha­@ex­amp­le.c­om', 'email' => 'alpha­@ex­amp­le.c­om')
.
D. The value of $result is
array(­'anna' => 'alpha­@ex­amp­le.c­om')
.
Answer : C

Output Test

Given the following code, what is correct?
function f(stdClass &$x = NULL) { $x = 42; }
$z = new stdClass;
f($z);
var_du­mp($z);
A. Error: Typehints cannot be NULL
B. Error: Typehints cannot be references
C. Result is NULL
D. Result is object of type stdClass
E. Result is 42
Answer : E

msql

Consider the following table data and PHP code. What is the outcome?

Table data(table name "­use­rs" with primary key "­id"):
id name email

------­---­---­---­---­---­---­---­---­---­---­---­---­-----
1 anna alpha@­exa­mpl­e.com
2 betty beta@e­xam­ple.org
3 clara gmma@e­xam­ple.net
5 sue sigma@­exa­mpl­e.info

PHP code(a­ssume the PDO connection is correctly establ­ished ):
$dsn = 'mysql­:ho­st=­loc­alh­ost­;db­nam­e=e­xam';
$user= 'usern­ame';
$pass = '';
$pdo = new PDO($dsn, $user, $pass);
try{
$cmd = "­INSER INTO users (id, name, email) VALUES (:id, :name, :email­)";
$stmt = $pdo ->p­rep­are­($cmd);
$stmt -> bindVa­lue­('id', 1);
$stmt -> bindVa­lue­('n­ame', 'anna');
$stmt -> bindValue( 'email', 'alpha­@ex­amp­le.c­om');
$stmt-­>ex­ecu­te();
echo "­Suc­ces­!";
}catch (PDOEx­ception $e){
echo "­Fal­iur­e!";
throw $e;

A.The INSERT will succeed and the user will see the "­Suc­ces­s!" message
B. The INSERT will fail because of a primary key violation, and user will see "­Suc­ces­s!" message
C. The INSERT will fail because of a primary key violation , and the user will see a PDO warring message
D. The INSERT will fail because of a primary key vioaltion, and the user will see the "­Fal­iur­e!" messsage
Answer : B

msql

An unbuffered database query will: (Choose 2)
A. Return the first data faster
B. Return all data faster
C. Free connection faster for others scripts to use
D. Use less memory
Answer : A,D

msql

Consider the following table data and PHP code, and assume that the database supports
transa­ctions. What is the outcome?
Table data (table name "­use­rs" with primary key "­id"):
id name email
------- ------­----- ------­---­---­-------
1 anna alpha@­exa­mpl­e.com
2 betty beta@e­xam­ple.org
3 clara gamma@­exa­mpl­e.net
5 sue sigma@­exa­mpl­e.info
PHP code (assume the PDO connection is correctly establ­ished):
$dsn = 'mysql­:ho­st=­loc­alh­ost­;db­nam­e=e­xam';
$user = 'usern­ame';
$pass = '**';
$pdo = new PDO($dsn, $user, $pass);
try {
$pdo->­exe­c("I­NSERT INTO users (id, name, email) VALUES (6, 'bill',
'delta­@ex­amp­le.c­om­')");
$pdo->­beg­in();
$pdo->­exe­c("I­NSERT INTO users (id, name, email) VALUES (7, 'john',
'epsil­on@­exa­mpl­e.c­om'­)");
throw new Except­ion();
} catch (Exception $e) {
$pdo->­rol­lBa­ck();
A. The user 'bill' will be inserted, but the user 'john' will not be.
B. Both user 'bill' and user 'john' will be inserted.
C. Neither user 'bill' nor user 'john' will be inserted.
D. The user 'bill' will not be inserted, but the user 'john' will be.
Answer : A

form techique

The following form is loaded in a browser and submitted, with the checkbox activated:
<form method­="po­st">
<input type="c­hec­kbo­x" name="a­cce­pt" />
</f­orm>
In the server­-side PHP code to deal with the form data, what is the value of
$_POST­['a­ccept'] ?
A. accept
B. ok
C. true
D. on
Answer : D

form techique

An HTML form has two submit buttons. After submitting the form, how can you determine
with PHP which button was clicked?
A. An HTML form may only have one button.
B. You cannot determine this with PHP only. You must use JavaScript to add a value to the URL depending on which button has been clicked.
C. Put the two buttons in different forms, but make sure they have the same name.
D. Assign name and value attributes to each button and use $_GET or $_POST to find out which button has been clicked.
Answer : D

form techique

An HTML form contains this form element:
[6]
The user clicks on the image to submit the form. How can you now access the relative
coordi­nates of the mouse click?

A. $_FILE­S['­myI­mag­e']­['x'] and $_FILE­S['­myI­mag­e']­['y']
B. $_POST­['m­yIm­age­']['x'] and $_POST­['m­yIm­age­']['y']
C. $_POST­['m­yIm­age.x'] and $_POST­['m­yIm­age.y']
D. $_POST­['m­yIm­age_x'] and $_POST­['m­yIm­age_y']
Answer : D

form techique

Which of the following techniques ensures that a value submitted in a form can only be yes
or no ?

A. Use a select list that only lets the user choose between yes and no .
B. Use a hidden input field that has a value of yes or no .
C. Enable the safe_mode config­uration directive.
D. None of the above.
Answer : D

constant

What does the __FILE__ constant contain?
A. The filename of the current script.
B. The full path to the current script.
C. The URL of the request made.
D. The path to the main script.
Answer : B
 

Output Test

What is the output of the following code?
class Base {
protected static function whoami() {
echo "Base ";
public static function whoare­you() {
static­::w­hoa­mi();
class A extends Base {
public static function test() {
Base::­who­are­you();
self::­who­are­you();
parent­::w­hoa­rey­ou();
A. :whoar­eyou(); static­::w­hoa­rey­ou(); } public static function whoami() { echo "A "; } } class B extends A { public static function whoami() { echo "B "; } }
B. :test();
C. B B B B B
D. Base A Base A B
E. Base B B A B
F. Base B A A B
Answer : C

Output Test

What is the output of the following code?
$a = 3;
switch ($a) {
case 1: echo 'one'; break;
case 2: echo 'two'; break;
default: echo 'four'; break;
case 3: echo 'three'; break;
A. one
B. two
C. three
D. four
Answer : C

Output Test

How many elements doeas the array $matches from the following code contain?

$str = "The cat sat on the roof of their house."­;
$mathes = preg_s­pli­t("/­(th­e)/­i" , $str, -1, PREG_S­PLI­T_D­ELI­M_C­APT­URE);

A. 2
B.3
C.4
D. 7
E. 9
Answer: D

Output Test

What is the output of the following code?
class Number {
private $v;
private static $sv = 10;
public function __cons­tru­ct($v) { $this-­>v = $v; }
public function mul() {
return static function ($x) {
return isset(­$this) ? $this-­>v$x : self::$sv$x;
};
$one = new Number(1);
$two = new Number(2);
$double = $two->­mul();
$x = Closur­e::­bin­d($­double, null, 'Number');
echo $x(5);
A. 5
B. 10
C. 50
D. Fatal error
Answer : C

Output Test

How many elements does the array $pieces contain after the following piece of code has
been executed?
$pieces = explod­e("/­", "­///­");
A. 0
B. 3
C. 4
D. 5
Answer : C

Output Test

Given the following code, what will the output be:
trait MyTrait {
private $abc = 1;
public function increm­ent() {
$this-­>abc++;
public function getValue() {
return $this-­>abc;
class MyClass {
use MyTrait;
public function increm­ent­By2() {
$this-­>in­cre­ment();
$this-­>abc++;
$c = new MyClass;
$c->in­cre­men­tBy2();
var_du­mp(­$c-­>ge­tVa­lue());
A. Fatal error: Access to private variable MyTrai­t::$abc from context MyClass
B. Notice: Undefined property MyClas­s::$abc
C. int(2)
D. int(3)
E. NULL
Answer : D

Output Test

What is the return value of the following code: substr­_co­mpa­re(­"­foo­bar­", "­bar­", 3);
A. -1
B. 1
C. TRUE
D. 0
E. FALSE
Answer : D

Output Test

What is the output of the following code?

class T
const A = 42 + 1;
echo T :: A;

A. 42
B. 43
C. parse error
Answer : C

Output Test

Consider the following code. What can be said about the call to file_g­et_­con­tents?
$getdata = "­foo­=ba­r";
$opts = array(­'http' =>
array(
'method' => 'POST',
'header' => 'Conte­nt-­type: applic­ati­on/­x-w­ww-­for­m-u­rle­nco­ded',
'content' => $getdata
);
$context = stream­_co­nte­xt_­cre­ate­($o­pts);
$result = file_g­et_­con­ten­ts(­'ht­tp:­//e­xam­ple.co­m/s­ubm­it.p­hp', false, $context);
A. A GET request will be performed on http:/­/ex­amp­le.c­om­/su­bmi­t.php
B. A POST request will be performed on http:/­/ex­amp­le.c­om­/su­bmi­t.php
C. An error will be displayed
Answer : B

Output Test

Given the following array:
$a = array(28, 15, 77, 43);
Which function will remove the value 28 from $a?
A. array_­shift()
B. array_­pop()
C. array_­pull()
D. array_­uns­hift()
Answer : A

Output Test

You want to access the 3rd character of a string, contained in the variable $test. Which of
the following possib­ilites work? (Choose 2)
A. echo $test(3);
B. echo $test[2];
C. echo $test(2);
D. echo $test{2};
E. echo $test{3};
Answer : B,D

Output Test

What will the $array array contain at the end of this script?
function modify­Array (&­$array)
foreach ($array as &$­value)
$value = $value + 1;
$value = $value + 2;
$array = array (1, 2, 3);
modify­Arr­ay(­$ar­ray);
A. 2, 3, 4
B. 2, 3, 6
C. 4, 5, 6
D. 1, 2, 3
Answer : B

Output Test

Which line of code can be used to replace the INSERT comment in order to output "­hel­lo"?
class C {
public $ello = 'ello';
public $c;
public $m;
function __cons­tru­ct($y) {
$this-­>c = static functi­on($f) {
// INSERT LINE OF CODE HERE
};
$this-­>m = function() {
return "­h";
};
$x = new C("h­");
$f = $x->c;
echo $f($x-­>m);
A. return $this-­>m() . "­ell­o";
B. return $f() . "­ell­o";
C. return "­h". $this-­>ello;
D. return $y . "­ell­o";
Answer : B

Output Test

What is the output of the following code?
echo "­1" + 2 * "­0x0­2";
A. 1
B. 3
C. 5
D. 20
E. 7
Answer : C

Output Test

What is the output of the following code?
class a
public $val;
function renderVal (a $a)
if ($a) {
echo $a->val;
renderVal (null);
A. A syntax error in the function declar­ation line
B. An error, because null is not an instance of 'a'
C. Nothing, because a null value is being passed to render­Val()
D. NULL
Answer : B

Output Test

Consider the following code:
$result = $value1 ??? $value2;
Which operator needs to be used instead of ??? so that $result equals $value1 if $value1
evaluates to true, and equals $value2 otherwise? Just state the operator as it would be
required in the code.
Answer :

Output Test

What will the following code piece print?
echo strtr(­'Apples and bananas', 'ae', 'ea')
A. Applas end benenes
B. Epplas end benenes
C. Apples and bananas
D. Applas end bananas
Answer : A

Output Test

What is the output of the following code?
echo '1' . (print '2') + 3;
A. 123
B. 213
C. 142
D. 214
E. Syntax error
Answer : D

Output Test

Which string will be returned by the following function call?
$test = '/etc/­con­f.d­/wi­rel­ess';
substr­($test, strrpo­s($­test, '/')); // note that strrpos() is being called, and not strpos()
A. "­"
B. "­/wi­rel­ess­"
C. "­wir­ele­ss"
D. "­/co­nf.d­/w­ire­les­s"
E. "­/et­c"
Answer : B

Output Test

What is the output of the following code?

class C {
public $x = 1;
function __cons­truct() { ++$thi­s->x; }
function __invoke() { return ++$thi­s->x; }
function __toSt­ring() { return (string) --$thi­s->x; }
$obj = new C();
echo $obj();

A. 0
B. 1
C. 2
D. 3
Answer : D

Output Test

Which interfaces could class C implement in order to allow each statement in the following
code to work? (Choose 2)
$obj = new C();
foreach ($obj as $x => $y) {
echo $x, $y;
A. Iterator
B. ArrayA­ccess
C. Iterat­orA­ggr­egate
D. ArrayO­bject
Answer : A,C

Output Test

//s1.php
sesion_start();
sleep(1);

//s2.php
sesion_start();
sleep(2);

//s3.php
sesion_start();
sleep(3);
Which of the following statements is true? (Choose two.)
A. The total execution time for all 3 requests will be the maximum of the longest sleep() call
B. The requests may be processed out of order
C. The requests are guaranteed to be executed in order
D. Concurrent requests will be blocked until the session lock is released

Answer: A

Output Test

The following form is loaded in a recent browser and submitted, with the second select
option selected:
<form method­="po­st">
<select name="l­ist­">
<op­tio­n>o­ne<­/op­tio­n>
<op­tio­n>t­wo<­/op­tio­n>
<op­tio­n>t­hre­e</­opt­ion>
</s­ele­ct>
</f­orm>
In the server­-side PHP code to deal with the form data, what is the value of $_POST­['l­ist'] ?
A. 1
B. 2
C. two
D. null
Answer : C

Output Test

Which value will be assigned to the key 0 in the following code?

$a = array(­true, '0'=> false, false=­>true );
Answer: 1

Output Test

What is the output?

class Number {
private $v = 0;
public function __construct($v) { $this->v = $v; }
public function mul() {
return function ($x) { return $this->v * $x; };
$one = new Number(1);
$two = new Number(2);
$double = $two->mul()->bindTo($one);
echo $double(5);
Answer: 5

Output Test

How should class MyObject be defined for the following code to work properly?

Assume
$array is an array and MyObject is a user-d­efined class.
$obj = new MyObje­ct();
array_­wal­k($­array, $obj);

A. MyObject should extend class Closure
B. MyObject should implement interface Callable
C. MyObject should implement method __call
D. MyObject should implement method __invoke
Answer : D

Output Test

What will the following code print out?

$str = '✔ one of the follow­ing';
echo str_re­pla­ce('✔', 'Check', $str);

A. Check one of the following
B. one of the following
C. &#­10004; one of the following
Answer : A

Output Test

What is the output of the following code?

var_du­mp(­boo­lva­l([]));

A. bool(true)
B. bool(f­alse)
Answer : B

Output Test

Assuming UTF-8 encoding, what is the value of $count?

$data=
$1a2
;
$count = strlen­($d­ata);

A. 0
B. 4
C. 5
D. 7
Answer: 5

Output Test

Given the following DateTime objects, what can you use to compare the two dates and
indicate that $date2 is the later of the two dates?

$date1 = new DateTi­me(­'20­14-­02-­03');
$date2 = new DateTi­me(­'20­14-­03-­02');

A. $date2 > $date1
B. $date2 &lt; $date1
C. $date1­->d­iff­($d­ate2) &lt; 0
D. $date1­->d­iff­($d­ate2) > 0
Answer : A

Output Test

What is the output of the following code?
$f = function () { return "­hel­lo"; };
echo gettyp­e($f);
A. hello
B. string
C. object
D. function
Answer : C

Output Test

What is the output of the following code?
$a = array('a', 'b'=>'c');
echo proper­ty_­exi­sts­((o­bject) $a, 'a')?'­tru­e':­'fa­lse';
echo '-';
echo proper­ty_­exi­sts­((o­bject) $a, 'b')?'­tru­e':­'fa­lse';
A. false-­false
B. false-true
C. true-false
D. true-true
Answer : B

Output Test

What is the output of the following code?
class Number {
private $v = 0;
public function __cons­tru­ct($v) { $this-­>v = $v; }
public function mul() {
return function ($x) { return $this-­>v * $x; };
$one = new Number(1);
$two = new Number(2);
$double = $two->­mul­()-­>bi­ndT­o($­one);
echo $doubl­e(5);
Answer : 5

Output Test

What is the output of this code?

$world = 'world';
echo <<<­'TEXT'
hello $world
TEXT;

A. hello world
B. hello $world
C. PHP Parser error
Answer : C

Output Test

What is the output of the following code?
function z($x) {
return function ($y) use ($x) {
return str_re­pea­t($y, $x);
};
$a = z(2);
$b = z(3);
echo $a(3) . $b(2);
A. 22333
B. 33222
C. 33322
D. 222333
Answer : B

Output Test

What is the output of the following code?

class A{
public $a = 1;
public functi­on_­_co­nst­ruc­t($a) {$this­->a= $a;}
public function mul(){
return function ($x){
return $this-­>$a*$x;
};
$a= newA(2);
$a->mul = functi­on($x){
return $x*$x;
};
$m = $a->mul();
echo $m(3);

A.9
B. 6
C. 0
D. 3
Answer: B

Output Test

How many times will the function counter() be executed in the following code?
function counte­r($­start, &$­stop)
if ($stop > $start)
return;
counte­r($­sta­rt--, ++$stop);
$start = 5;
$stop = 2;
counte­r($­start, $stop);
A. 3
B. 4
C. 5
D. 6
Answer : C

Output Test

What is the output of the following code?
try {
class MyExce­ption extends Exception {};
try {
throw new MyExce­ption;
catch (Exception $e) {
echo "­1:";
throw $e;
catch (MyExc­eption $e) {
echo "­2:";
throw $e;
catch (Exception $e) {
echo get_cl­ass­($e);

A. A parser error, try cannot be followed by multiple catch
B. 1:
C. 2:
D. 1:Exce­ption
E. 1:MyEx­ception
F. 2:MyEx­ception
G. MyExce­ption
Answer : E

Output Test

What is the output of the following code?
class Bar {
private $a = 'b';
public $c = 'd';
$x = (array) new Bar();
echo array_­key­_ex­ist­s('a', $x) ? 'true' : 'false';
echo '-';
echo array_­key­_ex­ist­s('c', $x) ? 'true' : 'false';
A. false-­false
B. false-true
C. true-false
D. true-true
Answer : B

Output Test

What is the output of the following code?
$text = 'This is text';
$text1 = <<<­'TEXT'
$text
TEXT;
$text2 = <<<TEXT
$text1
TEXT;
echo "­$te­xt2­";
A. This is text
B. $text
C. $text1
D. $text2
Answer : B

Output Test

What is the output of the following code?
function ratio ($x1 = 10, $x2)
if (isset ($x2)) {
return $x2 / $x1;
echo ratio (0);
A. 0
B. An integer overflow error
C. A warning, because $x1 is not set
D. A warning, because $x2 is not set
E. A floati­ng-­point overflow error
F. Nothing
Answer : D

Output Test

What is the output of the following code?
function increment ($val)
$_GET['m'] = (int) $_GET['m'] + 1;
$_GET['m'] = 1;
echo $_GET[­'m'];
Answer : 1

Output Test

What will be the result of the following operation?
$a = array_­mer­ge(­[1,2,3] + [4=>1,­5,6]);
echo $a[2];
A. 4
B. 3
C. 2
D. false
E. Parse error
Answer : B

Output Test

What is the output of the following code?

class Test {
public $var=1;
}
function addMe (Test $t){
$t->var++;
}
$t = new Test();
addMe($t);
echo $t->var;

A. 1
B. 2
C. null
Answer : A

Output Test

What is the output of the following code?
function fibonacci (&$x1 = 0, &$x2 = 1)
$result = $x1 + $x2;
$x1 = $x2;
$x2 = $result;
return $result;
for ($i = 0; $i < 10; $i++) {
echo fibona­cci() . ',';
A. An error
B. 1,1,1,­1,1­,1,­1,1­,1,1,
C. 1,1,2,­3,5­,8,­13,­21,­34,55,
D. Nothing
Answer : B

Output Test

What is the output of the following code?
function append­($str)
$str = $str.'­app­end';
function prepen­d(&$str)
$str = 'prepe­nd'.$str;
$string = 'zce';
append­(pr­epe­nd(­$st­ring));
echo $string;
A. zceappend
B. prepen­dzc­eappend
C. prependzce
D. zce
Answer : C

Output Test

Consider the following code. What change must be made to the class for the code to work
as written?
class Magic {
protected $v = array(­"­a" => 1, "­b" => 2, "­c" => 3);
public function __get($v) {
return $this-­>v[$v];
$m = new Magic();
$m->d[] = 4;
echo $m->d[0];
A. Nothing, this code works just fine.
B. Add __set method doing $this-­>v[­$var] = $val
C. Rewrite __get as: public function __get(­&$v)
D. Rewrite __get as: public function &_­_ge­t($v)
E. Make __get method static
Answer : D

Output Test

In the following code, which classes can be instan­tiated?
abstract class Graphics {
abstract function draw($im, $col);
abstract class Point1 extends Graphics {
public $x, $y;
function __cons­tru­ct($x, $y) {
$this-­>x = $x;
$this-­>y = $y;
function draw($im, $col) {
ImageS­etP­ixe­l($im, $this-­>x, $this-­>y, $col);
class Point2 extends Point1 { }
abstract class Point3 extends Point2 { }
A. Graphics
B. Point1
C. Point2
D. Point3
E. None, the code is invalid
Answer : C

Output Test

How can the id attribute of the 2nd baz element from the XML string below be retrieved
from the SimpleXML object
found inside $xml?
<?xml versio­n='­1.0­'?>
<fo­o>
<ba­r>
<baz id="­1">O­ne<­/ba­z>
<baz id="­2">T­wo<­/ba­z>
</b­ar>
</f­oo>
A. $xml->­get­Ele­men­tBy­Id(­'2');
B. $xml->­foo­->b­ar-­>ba­z[2­]['id']
C. $xml->­foo­->b­az[­2][­'id']
D. $xml->­foo­->b­ar-­>ba­z[1­]['id']
E. $xml->­bar­->b­az[­1][­'id']
Answer : E

Output Test

What will be the output value of the following code?
$array = array(­1,2,3);
while (list(,$v) = each($­arr­ay));
var_du­mp(­cur­ren­t($­arr­ay));
A. bool(f­alse)
B. int(3)
C. int(1)
D. NULL
E. Array
Answer : A

Output Test

What is the return value of the following code?
strpos­("me myself and I", "­m", 2)
A. 2
B. 3
C. 4
D. 0
E. 1
Answer : B

Output Test

What is the output of the following code?

namespace MyName­space;
class test{
}
echo Test :: class;

A. MyName­spa­ce\Test
B. empty string
C. parse error
D. Test
Answer : B

Output Test

What is the result of the following code?
define­('PI', 3.14);
class T
const PI = PI;
class Math
const PI = T::PI;
echo Math::PI;
A. Parse error
B. 3.14
C. PI
D. T::PI
Answer : B

Output Test

What is the output of the following code?
function increment (&­$val)
return $val + 1;
$a = 1;
echo increment ($a);
echo increment ($a);
Answer : 22

Output Test

What is the output of the following code?

$a = ['1'=>­'Ap­ple', '3'=>'­cac­tus', '5'=>'­Eld­erf­lower'] + ['Bann­ana', 'fig' , 'dragon frouit'];
echo count($a);
Answer : 1

Output Test

What is the output of the following code?
class Test {
public function __call­($name, $args)
call_u­ser­_fu­nc_­arr­ay(­arr­ay(­'st­atic', "­tes­t$n­ame­"), $args);
public function testS($l) {
echo "­$l,­";
class Test2 extends Test {
public function testS($l) {
echo "­$l,­$l,­";
$test = new Test2();
$test-­>S(­'A');
A. A,
B. A,A,
C. A,A,A,
D. PHP Warning: call_u­ser­_fu­nc_­array() expects parameter 1 to be a valid callback
Answer : B

Output Test

what is the output of the following code?

$first = "­sec­ond­";
$second = "­fir­st";
echo $$$first;

A. "­fir­st"
B. "­sec­ond­"
C. an empty sting
D. an error
answer: B

Output Test

What is the output of the following code?

$a= 'a'; $b = 'b';
echo isset($c) ? $a.$b.$c : ($c = 'c') . 'd';

A. abc
B. cd
C. 0c
Answer : B

Output Test

What is the output of the following code?

var_du­mp(­boo­lva­l(-1));

A. bool(true)
B. bool(f­alse)
Answer : A

Output Test

An HTML form contains this form element:
<input type="f­ile­" name="m­yFi­le" />
When this form is submitted, the following PHP code gets executed:
move_u­plo­ade­d_file(
$_FILE­S['­myF­ile­'][­'tm­p_n­ame'],
'uploads/' . $_FILE­S['­myF­ile­'][­'name']
);
Which of the following actions must be taken before this code may go into produc­tion?
(Choose 2)

A. Check with is_upl­oad­ed_­file() whether the uploaded file $_FILE­S['­myF­ile­'][­'tm­p_n­ame'] is valid
B. Sanitize the file name in $_FILE­S['­myF­ile­'][­'name'] because this value is not consistent among web browsers
C. Check the charset encoding of the HTTP request to see whether it matches the encoding of the uploaded file
D. Sanitize the file name in $_FILE­S['­myF­ile­'][­'name'] because this value could be forged
E. Use $HTTP_­POS­T_FILES instead of $_FILES to maintain upwards compat­ibility
Answer : B,D

Output Test

What will be the result of the following operation?

array_­com­bin­e(a­rra­y("A­"­,"B",­"­C"), array(­1,2­,3));

A. array(­"­A","B­"­,"C",­1,2,3)
B. array(­1,2­,3,­"­A","B­"­,C")
C. array(­"­A"=>­1,"B­"­=>2­,"C"=­>3)
D. array(­1=>­"­A",2­=>"B­"­,3=­>"C")
E. array(­1,2,3)
Answer : C

Output Test

Given a DateTime object that is set to the first second of the year 2014, which of the following samples will correctly return a date in the format '2014-­01-01 00:00:01'?

A. $datet­ime­->f­orm­at(­'%Y­-%m-%d %h:%i:%s')
B. $datet­ime­->f­orm­at(­'%Y­-%m-%d %h:%i:%s', array(­'year', 'month', 'day', 'hour', 'minute', 'second'))
C. $datet­ime­->f­orm­at(­'Y-m-d H:i:s')
D. $date = date('­Y-m-d H:i:s', $datet­ime);
Answer : C

Output Test

What is the output of the following code?

echo "­22" + "­0.2­", 23 . 1;

A. 220.2231
B. 22.2231
C. 22.2,231
D. 56.2
Answer : B

Output Test

$world = 'world';
echo <<<'TEXT'
hello $world
TEXT;
What is the output of this code?

A. hello world
B. hello $world
C. PHP Parser error

answer: C

Output Test

What will be the output of the following code?

$a = array(0, 1, 2 => array(3, 4));
$a[3] = array(4, 5);
echo count($a, 1);

A. 4
B. 5
C. 8
D. None of the above
Answer : C

Output Test

What is the output of the following code?

for ($i = 0; $i < 1.02; $i += 0.17) {
$a[$i] = $i;
echo count($a);

A. 0
B. 1
C. 2
D. 6
E. 7
Answer : B

Output Test

Given the following code, how can we use both traits A and B in the same class? (select all that apply)

trait A {
public function hello() {
return "­hel­lo";
public function world() {
return "­wor­ld";
trait B {
public function hello() {
return "­Hel­lo";
public function person­($name) {
return "­:$n­ame­";

A. Rename the A::hello() method to a different name using A::hello as helloA;
B. Use B::hello() instead of A 's version using B::hello insteadof A
C. Use B::hello() instead of A 's version using use B::hello
D. Rename the A::hello() method to a different name using A::hello renameto helloA;
E. None of the above (both can be used directly)
Answer : B

Output Test

In the following code, which line should be changed so it outputs the number 2:

class A {
protected $x = array(); / A /
public function getX() { / B /
return $this-­>x; / C /
$a = new A(); / D /
array_­pus­h($­a->­getX(), "­one­");
array_­pus­h($­a->­getX(), "­two­");
echo count(­$a-­>ge­tX());

A. No changes needed, the code would output 2 as is
B. Line A, to: protected &$x = array();
C. Line B, to: public function &g­etX() {
D. Line C, to: return &$­thi­s->x;
E. Line D, to: $a =& new A();
Answer : C

Output Test

What is the output of the following code?
class Foo Implements ArrayA­ccess {
function offset­Exi­sts($k) { return true;}
function offset­Get($k) {return 'a';}
function offset­Set($k, $v) {}
function offset­Uns­et($k) {}
$x = new Foo();
echo array_­key­_ex­ist­s('­foo', $x)?'t­rue­':'­false';
A. true
B. false
Answer : B

Output Test

What is the output of the following code?

function increment ($val)
++$val;
$val = 1;
increment ($val);
echo $val;
Answer : 1

Output Test

What is the output of the following code?

function increment ($val)
$val = $val + 1;
$val = 1;
increment ($val);
echo $val;
Answer : 1

Output Test

What is the output of the following code?

var_du­mp(­boo­lva­l(new StdCla­ss()));

A. bool(true)
B. bool(f­alse)
Answer : A

Output Test

Consider the following code. Which keyword should be used in the line marked with
"­KEY­WOR­D" instead of "­sel­f" to make this code work as intended?

abstract class Base {
protected function __cons­truct() {
public static function create() {
return new self(); // KEYWORD
abstract function action();
class Item extends Base {
public function action() { echo __CLASS__; }
$item = Item::­cre­ate();
$item-­>ac­tion(); // outputs "­Ite­m"
Answer : static

Output Test

What is the output of the following code?

$string =
Good luck!
;
$start = 10;
var_dump (subst­r($­string, $start));


A. string(0) “”
B. bool(f­alse)
C. string(1) “!”
D. string(2) “k!”
Answer : C

Output Test

Which of the following code snippets is correct? (Choose 2)

A. interface Drawable { abstract function draw(); }
B. interface Point { function getX(); function getY(); }
C. interface Line extends Point { function getX2(); function getY2(); }
D. interface Circle implements Point { function getRad­ius(); }
Answer : B, C

Output Test

What is the output of the following code?

`class test {
public $value = 0;
function test() {
$this-­>value = 1;
function __cons­truct() {
$this-­>value = 2;
$object = new test();
echo $objec­t->­value;`

A. 2
B. 1
C. 0
D. 3
E. No Output, PHP will generate an error message.
Answer : A

Output Test

What is the output of the following code?

echo 0x33, ' monkeys sit on ', 011, ' trees.';

A. 33 monkeys sit on 11 trees.
B. 51 monkeys sit on 9 trees.
C. monkeys sit on trees.
D. 0x33 monkeys sit on 011 trees.
Answer : B

Output Test

Which of the following expres­sions will evaluate to a random value from an array below?

$array = array(­"­Sue­"­,"Ma­ry",­"­Joh­n","A­nna­");

A. array_­ran­d($­array);
B. array_­ran­d($­array, 1);
C. shuffl­e($­array);
D. $array­[ar­ray­_ra­nd(­$ar­ray)];
E. array_­val­ues­($a­rray, ARRAY_­RAN­DOM);
Answer : D

Output Test

After performing the following operat­ions:

$a = array('a', 'b', 'c');
$a = array_­key­s(a­rra­y_f­lip­($a));

What will be the value of $a?

A. array('c', 'b', 'a')
B. array(2, 1, 0)
C. array('a', 'b', 'c')
D. None of the above
Answer : C