Show Menu
Cheatography

Mix PHP 1 Cheat Sheet (DRAFT) by

Stream, joins, methods...

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

Stream Context

Which of the following is true about stream contexts? (Choose 2)

A. A context can modify or enhance the behavior of a stream
B. A context indicates what session the stream is part of
C. A context is a set of parameters and stream wrapper specific options
D. Contexts are created with new Stream­­_C­o­n­te­­xt();
Answer: A, C

stream

Which of the following will set a 10 seconds read timeout for a stream?

A. ini_se­­t(­"­d­e­fa­­ult­­_s­o­c­ke­­t_t­­im­e­o­ut­­", 10);
B. stream­­_r­e­a­d_­­tim­­eo­u­t­($­­stream, 10);
C. Specify the timeout as the 5th parameter to the fsocko­­pen() function used to open a stream
D. stream­­_s­e­t­_t­­ime­­ou­t­(­$s­­tream, 10);
E. None of the above
Answer: D

PUT method

When uploading a file to a PHP script using the HTTP PUT method, where would the file
data be found?

A. the $_FILES super-­­global
B. the input stream php://­­input
C. the $_POST super-­­global
D. the global variable scope
Answer: B

Methods

What is the name of the method that can be used to provide read access to virtual properties in a class?
A. __call()
B. __get()
C. __set()
D. __wakeup()
E. __fetch()
B - method __get()

Methods

Which of the following tasks can be achieved by using magic methods? (Choose 3)
A. Initia­­lizing or uninit­­ia­l­izing object data
B. Creating a new stream wrapper
C. Creating an iterable object
D. Processing access to undefined methods or properties
E. Overlo­­ading operators like +, *, etc.
F. Converting objects to string repres­­en­t­ation
Answer : A,D,F

Methods

Which of the following methods are available to limit the amount of resources available to
PHP through php.ini? (Choose 2)

A. Limit the amount of memory a script can consume
B. Limit the total amount of memory PHP uses on the entire server
C. Limit the maximum execution time of a script
D. Limit the maximum number of concurrent PHP processes
E. Limit the maximum number of concurrent PHP threads
Answer: A, C

Methods

Which methods can be used to overload object proper­­ties? (Choose 2)

A. set(), get()
B. __set(), __get()
C. __put(), __rece­­ive(), __exists()
D. set(), get(), isset()
E. __isset(), __unset()
Answer : B,E

key

What is the name of the key in $_FILE­­S[­'­n­ame'] that contains the number of bytes of the uploaded file?
Answer: size

uploads

Your applic­­ation uses PHP to accept and process file uploads. It fails to upload a file that is
5 MB in size, although upload­­_m­a­x­_f­­ilesize is set to "­­10­M­". Which of the following config­­ur­a­tions could be respon­­sible for this outcome? (Choose 2)

A. The PHP config­­ur­ation option post_m­­ax­_size is set to a value that is too small
B. The web server is using an incorrect encoding as part of the HTTP response sent to the client
C. The browser uses an incorrect encoding as part of the HTTP request sent to the server
D. The hidden form field MAX_FI­­LE­_SIZE was set to a value that is too small
E. PHP cannot process file uploads larger than 4 MB
Answer: A, D

object storage

In order to create an object storage where each object would be stored only once, you may use which of the following? (choose 2)

A. SplFix­­ed­Array
B. SplObj­­ec­t­S­torage
C. SplString
D . spl_ob­­je­c­t­_hash
E. spl_sa­­me­_­o­bject
Answer: B, D
 

Joins

Which technique should be used to speed up joins without changing their results?
A. Add indices on joined columns
B. Add a WHERE clause
C. Add a LIMIT clause
D. Use an inner join
Answer: A

conten­t-type

What conten­­t-type is required when sending an HTTP POST using JavaScript to ensure that PHP can access the data?

A. applic­­at­i­o­n/­­x-w­­ww­-­f­or­­m-u­­rl­e­n­coded
B. http/post
C. text/html
D. object­­/m­u­l­ti­­par­­t-­f­o­rmdata
Answer : A

indeti­fiers

Which of the following are valid identi­­fiers?

A. function 4You(){}
B. function _4You(){}
C. function object(){}
D. $1 = "­­He­l­l­o";
E. $_1 = "­­Hello World";
Answer : B, C , E

traits

Which of the following is NOT true about PHP traits? (Choose 2)

A. Multiple traits can be used by a single class.
B. A trait can implement an interface.
C. A trait can declare a private variable.
D. Traits are able to be auto-l­­oaded.
E. Traits automa­­ti­cally resolve conflicts based on definition order.
Answer: B, E

expres­sions

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

$array = array(­­"­S­ue­­"­­,"M­a­r­y",­­"­­Jo­h­n­"­,"A­­nna­­");

A. array_­­ra­n­d­($­­array);
B. array_­­ra­n­d­($­­array, 1);
C. shuffl­­e(­$­a­rray);
D. $array­­[a­r­r­ay­­_ra­­nd­(­$­ar­­ray)];
E. array_­­va­l­u­es­­($a­­rray, ARRAY_­­RA­N­DOM);
Answer: D

exceptions

What exception type will catch the error raised by the expression 2 / 0?
A. LogicE­­xc­e­ption
B. RangeE­­xc­e­ption
C. Divisi­­on­B­y­Ze­­roError
D. Arithm­­et­i­c­Error
Answer: C

exceptions

Which of the following statements about exceptions is correct? (Choose 2)
A. you can only throw classes derived from Exception
B. a try block can have multiple catch blocks
C. a try block must not be followed by a catch block
D. try blocks cannot contain nested try blocks
Answer: A, B

exceptions

Which of the following statements about exceptions is correct? (Choose 2)
A. you can only throw classes derived from Exception
B. a try block can have multiple catch blocks
C. a try block must not be followed by a catch block
D. try blocks cannot contain nested try blocks
Answer: A, B
 

Regex

Which sentence describes the following regular expression match?

preg_m­­at­c­h­('­­/^­­\d+­(­?­:\.[­0­­-9]­­+)­?$/', $test);

A. It matches float numbers with thousand sepera­­tors.
B. It matches float numbers without thousand sepera­­tors.
C. It matches binary integer numbers.
D. It matches any string.
E. It does not match anything
Answer: B

Regex

Which parts of the text are matched in the following regular expres­­sion?

$text = <<<EOT
The big bang bonged under the bung.
EOT;
preg_m­­at­c­h­_a­­ll(­­'@­b.n­?g@', $text, $matches);

A. bang bong bung
B. bang bonged bung
C. big bang bong bung
D. big bang bung
Answer: C

Regex

How many elements does the $matches array contain after the following function call is performed? preg_m­­at­c­h­('­­/^(­­\d­{­1­,2­­}([­­a-­z­]­+)­­)(?­­:­\s­*)\S+ (?=201­­[0­-­9­])/', '21st March 2014', $matches);
A. 1
B. 2
C. 3
D. 4
Answer: C

Headers

Under what condition may HTTP headers be set from PHP if there content echoed prior to the header function being used?

A. header­­s_­s­ent() returns true
B. Output buffering is enabled
C. The client supports local buffering
D. The webserver uses preemptive mode
Answer: B

Headers

Before the headers are sent, how can you remove a previously set header?

A. Use the header­­_r­e­m­ove() function, providing the name of the header
B. Use the die() function to abort the PHP script
C. Not possible
D. Use the header­­s_­l­ist() function, providing the name of the header as the second argument
Answer: A

Headers

From your PHP applic­­ation, how can you send the same header twice, but with different values?

A. Set the second argument of the header() function to false
B. PHP does that automa­­ti­cally
C. You may only send a particular type of header once
D. Use the header­­_add() function
Answer: A

type hinting

Type hinting in PHP allows the identi­­fi­c­ation of the following variable types : (choose 2)

A. String
B. Integer
C. Array
D. Any class or interface type
E. All of the above
Answer : C, D

type hinting

Which of the following statements is NOT correct?

A. Only methods can have type hints
B. Typehints can be optional
C. Typehints can be references
Answer: A

reflection

Which of the following is NOT possible using reflec­­tion?

A. Analysing of nearly any aspect of classes and interfaces
B. Analysing of nearly any aspect of functions
C. Adding class methods
D. Implement dynamic constr­­uction (new with a variable class name)
Answer: C

reflection

Which of the following statements about reflection is correct?

A. Reflection is an extension that can be disabled
B. Reflection is a new extension present only in PHP7.0+
C. Reflection only allows to reflect on built-in classes
D. Built-in classes can be reflected on command line using php --rc &l­­t;­c­l­as­­sna­­me>
Answer : D