Show Menu
Cheatography

Mix PHP 2 Cheat Sheet (DRAFT) by

basic

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

bitwise

What is the result of the following bitwise operation in PHP?
1 ^ 2

A. 1
B. 3
C. 2
D. 4
E. -1
Answer: B

date

Which of the following will NOT instan­­tiate a DateTime object with the current timestamp?

A. $date = new DateTi­­me();
B. $date = new DateTi­­me('@' . time());
C. $date = new DateTi­­me­(­'­now');
D. $date = new DateTi­­me­(­t­im­­e());
Answer: D

date

Given the following DateTime object, which sample will NOT alter the date to the value '2014-­­02­-15'?

$date = new DateTi­­me­(­'­20­­14-­­03­-­15');

A. $date-­­>s­u­b(new DateIn­­te­r­v­al­­('P­­1M'));
B. $date-­­>s­e­t­Da­­te(­­2014, 2, 15);
C. $date-­­>m­o­d­if­­y('-1 month');
D. $date-­­>d­i­f­f(new DateIn­­te­r­v­al­­('-­­P1­M'));
Answer: D

pdo

Which of these databases is NOT supported by a PDO driver?

A. Microsoft SQL Server
B. SQLite
C. Microsoft Access
D. Berkeley DB
Answer: D

object

An object can be counted with count() and sizeof() if it...

A. implements ArrayA­­ccess
B. has a public __count() method
C. was cast to an object from an array
D. None of the above
Answer: D

class

Which of the following statements about PHP is false? (Choose 2)
A. A final class can be derived.
B. A final class may be instan­­ti­ated.
C. A class with a final function may be derived.
D. Static functions can be final.
E. Properties can be final.
Answer: A, E

class

When a class is defined as final it:

A. Can no longer be extended by other classes.
B. Means methods in the class are not over-l­­oa­d­able.
C. Cannot be defined as such, the final is only applicable to object methods.
D. Cannot be instan­­ti­ated.
Answer: A

arrays

Which value will be assigned to the key 0 in this example?

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

arrays

Which is the most efficient way to determine if a key is present in an array, assuming the array has no NULL values?

A. in_arr­­ay­(­'­key', array_­­ke­y­s­($a))
B. isset(­­$a­[­'­key'])
C. array_­­ke­y­_­ex­­ist­­s(­'­key', $a)
D. None of the above
Answer: B
 

string

What is the length of a string returned by: md5(ra­­nd(), TRUE);

A. Depends on the value returned by rand() function
B. 32
C. 24
D. 16
E. 64
Answer: D

Namespace

When Would you use classes and when would you use namesp­­ac­e­s>

A. Use classes to encaps­­ulate code and represent objects, and namespaces to avoid symbol name collision
B. Use Classes for perfor­­ma­n­c­e-­­sen­­sitive code, namespaces when readab­­ility maters more
C. Use namespaces for perfor­­ma­n­c­e-­­sen­­sitive code, and classes when readab­­ility matters more
D. Always use them; namespaces are always superior to classes
Answer: A

Namespace

What purpose do namespaces fulfill?
A. Encaps­­ul­ation
B. Altern­­ative to classes
C. Improved perfor­­mance
D. All of the above
Answer: A

Namespace

What would be the output of the following code?

namespace MyFram­­ew­o­r­k\DB;
class MyClass {
static function myName() {
return __METH­­OD__;
print MyClas­­s:­:­m­yN­­ame();

A. MyFram­­ew­o­r­k­­\DB­­\myName
B. MyFram­­ew­o­r­k­­\DB­­\My­C­l­as­­s\m­­yName
C. MyFram­­ew­o­r­k­­\DB­­\My­C­l­as­­s::­­myName
D. MyClas­­s:­:­m­yName
Answer: C

Namespace

You'd like to use the class MyDBCo­­nn­e­ction that's defined in the
MyGrea­­tF­r­a­me­­wor­­k­\M­y­Gr­­eat­­Da­t­a­ba­­seA­­bs­t­r­ac­­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­­tF­r­a­mework namespace
B. Import the MyGrea­­tF­r­a­me­­wor­­k­\M­y­Gr­­eat­­Da­t­a­ba­­seA­­bs­t­r­ac­­tio­­nLayer namespace
C. Alias MyGrea­­tF­r­a­me­­wor­­k­\M­y­Gr­­eat­­Da­t­a­ba­­seA­­bs­t­r­ac­­tio­­nL­a­y­er­­\My­­DB­C­o­nn­­ection to a shorter name
D. Alias MyGrea­­tF­r­a­me­­wor­­k­\M­y­Gr­­eat­­Da­t­a­ba­­seA­­bs­t­r­ac­­tio­­nLayer to a shorter name
Answer: C

Namespace

Which of these elements can be encaps­­ulated by namespaces and made accessible from
the outside?
A. Only classes
B. Classes, functions, and constants
C. Classes, functions, constants, and variables
Answer: B

Class Constants

Which of the following statements is NOT true?

A. Class constants are public
B. Class constants are being inherited
C. Class constants can omit initia­­li­z­ation (default to NULL)
D. Class constants can be initia­­lized by const
Answer: C

conjuction

Which of the following may be used in conjun­­ction with CASE inside a SWITCH statement?

A. A scalar
B. An expression
C. A boolean
D. All of the above
Answer: D
 

php

Given the default PHP config­­ur­a­tion, how can all of the parameters provided via GET be accessed in a form of a string?

A. $_GET[­­'ALL']
B. $_SERV­­ER­[­'­QU­­ERY']
C. $_SERV­­ER­[­'­QU­­ERY­­_S­T­R­ING']
D. $_ENV[­­'Q­U­ERY']
E. $QUERY­­_S­TRING
Answer: C

pdo

Which of these statements about PDO is NOT true?
A. PDO has built-in support for Large Objects (LOBs).
B. Placeh­­olders within PDO prepared statements need to be named.
C. When something goes wrong, PDO can throw an instance of its own exception class.
D. PDO does not emulate missing database features.
Answer: B

PHP

When retrieving data from URLs, what are valid ways to make sure all file_g­­et­_­c­on­­tents calls send a certain user agent string? (Choose 2)

A. $defau­­lt­_opts =array­­('­http' =>a­­rr­a­y­('­­use­­r_­a­gent' => "My Cool Brower­­"­)­);­­$de­­fault= stream­­_c­o­n­te­­xt_­­se­t­_­de­­fau­­lt­(­$­de­­fau­­lt­_­o­pts);
B. stream­­_c­o­n­te­­xt_­­se­t­_­op­­tio­­n(­"­u­s­er­­_ag­­en­t­", "My Cool Browse­­r");
C. ini_se­­t(­'­u­se­­r_a­­gent' , "My Cool Browse­­r");
D. stream­­_c­o­n­te­­xt_­­se­t­_­op­­tio­­n(­$­c­on­­text, "­­ht­t­p­", "­­us­e­r­_a­­gen­­t", "My Cool Browse­­r");
Answer: A, C

PHP

Which of the following PHP values may NOT be encoded to a JavaScript literal using
PHP's ext/json capabi­­li­ties?
A. 'Hello, world!'
B. functi­­on(){ alert(­­"­H­ello, world!­­"); }
C. array(­­'H­ello, world!')
D. array(­­'m­e­s­sage' => 'Hello, world!')
Answer: B

PHP

Please provide the value of the $code variable in the following statement to set an HTTP
status code that signifies that the requested resource was not found.
http_r­­es­p­o­ns­­e_c­­od­e­(­$c­­ode);
Answer: 404, 404 Not Found

PHP

Consider the following two files. When you run test.php , what would the output look like?
test.php:
include "­­My­S­t­ri­­ng.p­­h­p­";
print "­­,";
print strlen­­("Hello World");
MyStri­­ng.php:
namespace MyFram­­ew­o­r­k­­\String:
function strlen­­($str)
return­­\s­t­r­le­­n($str) *2; // return double the string length
print strlen ("Hello World")

A. 12.12
B. 12,24
C. 24.12
D. 24,24
E. PHP Fatal error:­­cannot declare strlen()
Answer: C

PHP

What is the benefit of using persistent database connec­­tions in PHP? (Choose two.)

A. Reduces the connection & authen­­ti­c­ation overhead of connecting to the database
B. Ensures that only a single connection is open to the database from PHP
C. Allows connection settings such as character set encoding to persist
D. Allows for a resumption of transa­­ctions across multiple requests.
Answer: C, D

PHP

Which of the following is used to find all PHP files under a certain directory?

A. PHPIte­­rator
B. Recurs­­iv­e­T­re­­eIt­­erator
C. Recurs­­iv­e­D­ir­­ect­­or­y­I­te­­rator
D. SplTem­­pF­i­l­eO­­bject
Answer: C

PHP

What is the difference between "­­pr­i­n­t" and "­­ec­h­o­"?
A. There is no differ­­ence.
B. Print has a return value, echo does not
C. Echo has a return value, print does not
D. Print buffers the output, while echo does not
E. None of the above
Answer: B

PHP

Which of the following statements is true?
A. All PHP database extensions support prepared statements
B. All PHP database extensions come with their own special helper functions to escape user data to be used in dynamic SQL queries
C. All PHP database extensions provide an OOP interface
D. All PHP database extensions appear in the output of php -m , if installed and enabled
Answer: D

PHP

What is the pattern modifier for handling UTF-8 encoded preg_ *funct­­io­n­a­lity?

A. e
B. u
C. PHP does not support UTF-8 encoded strings
D. a pattern modifier is not needed
Answer: B

PHP

What is "­­in­s­t­an­­ceo­­f" an example of?

A. a boolean
B. an operator
C. a function
D. a language construct
E. a class magic
Answer: B

php.ini

Given a php.ini setting of
defaul­­t_­c­h­arset = utf-8
what will the following code print in the browser?
header­­('­C­o­nt­­ent­­-Type: text/html; charse­­t=­i­s­o-­­885­­9-1');
echo '✂✔✝';
A. Three Unicode charac­­ters, or unreadable text, depending on the browser
B. &#­­99­8­6­;&­#1­­000­­4;­&#­1­0013;
C. A blank line due to charset mismatch
Answer: A

php.ini

Your supervisor wants you to disallow PHP scripts to open remote HTTP and FTP
resources using PHP's file functions. Which php.ini setting should you change accord­­ingly?
Answer : allow_­­ur­l­_­fopen, allow_­­ur­l­_­fo­­pen­­=off, allow_­­ur­l­_­fo­­pen­­=Off, allow_­­ur­l­_­fopen = off, allow_­­ur­l­_­fopen = Off

php.ini

Which php.ini setting is usually required to use an opcode cache?
A. extension
B. zend_e­­xt­e­nsion
C. optimizer
D. dl
Answer: B