Beginners PHP Quiz Last updated on: 04-13-2017 This quiz is primarily targeted for beginner PHP developers. It covers basic concepts like variable types, arrays, basic OOP principles, functions MySQL queries and other gotchas related to the PHP programming language.

1.What is the result of the code below ? <?php echo ("0.00") ? "mary" : "angie"; ?> Syntax Error angie mary

2.What is the output of the following code: <?php function myFunc() { $in = "nothing"; return func_get_args(); } $in = "something"; var_dump(myFunc($in)); ?> array(1) { [0]=> string(9) "nothing" } array(1) { [0]=> string(9) "something" } Warning: function myFunc expects 0 parameters, 1 given

3.What are the possible security implications of printing an unfiltered request variable ? No security implications Unfiltered request variables can not be filtered in PHP An XSS atack

4.What is wrong in the following code (assume that $db is an instance of mysqli, mytable exists and has a column called student): <?php if($result = $db->query("SELECT student FROM mytable WHERE student = '$_POST["student"]'")) while($row = $result->fetch_object()) echo $row->student; ?> Nothing is wrong The script is vulnerable to SQL Injection Syntax Error

5.Which PHP function(s) can be used to check if a variable is defined and is not NULL ? With the isset() and is_null() functions With the isset() function With the empty() function

6.How do you obtain the length of a string in PHP ? echo strlen($string); echo $string->length; echo count($string);

7.What is the output of the following snippet ? <?php $a = strlen(NULL); echo $a++; ?> 0 1 Fatal Error - The strlen function expects a string, NULL given

8.What is the output of the code below ? <?php $a = array(0 => "MySQL", 2=>"PHP", 3=>"JAVA", 4 => "JavaScript"); $a = array_values($a); echo $a[1]; ?> Notice undefined offset 1 ... MySQL PHP

9.What is the output of the code below ? <?php $a = array("PHP", "MySQL", 4, "1"); $sum = array_sum($a); echo $sum; ?> 4 5 7

10.Considering the table structure below what MySQL query will load a result set containing all students names enrolled in the programming classes ? students table: | studentid | name | -------------------- | 1 | Mike | | 2 | John | | 3 | Jeff | | 4 | Anne | classes table: | classid | classname ------------------------- | 1 | Math | 2 | Programming | 3 | Biology classes_to_students table: | studentid | classid ------------------------- | 1 | 1 | 2 | 2 | 3 | 2 | 4 | 3 SELECT s.name FROM students as s

WHERE s.studentid IN

( SELECT studentid FROM classes_to_students as cs

INNER JOIN classes as c ON cs.classid = c.classid

WHERE c.classname = 'Programming'

); SELECT s.name FROM students as s

INNER JOIN classes as c ON s.studentid = c.classid

INNER JOIN classes_to_students as cs ON s.studentid = cs.studentid

WHERE c.classname = 'Programming'; SELECT s.name FROM students as s

INNER JOIN classes_to_students as cs ON s.studentid = cs.studentid

INNER JOIN classes as c ON c.classid = cs.classid

WHERE c.classname='Programming';

11.What happens when the script below is executed ? <?php $a = array(1,2,3); $b = array(7,8,9); $counta = count($a); $countb = sizeof($b); if($counta===$countb) { echo "They are equal and have the same type"; } else { echo "They are not equal or they are not of the same type"; } ?> The script returns "They are equal and have the same type" The script returns "They are not equal or they are not of the same type" The script fails because the sizeof() function doesn't exist

12.What is the final value of $i ? <?php $numbers = array(10, "10", 10.5, "10.5", null, true, false); $i = 0; foreach ($numbers as $number) { if(is_int($number)) $i++; else $i--; } echo $i; ?> -3 -4 -5

13.What happens when you run the script below ? <?php define("2MYCONSTANT", "avalue"); if(defined("2MYCONSTANT")) echo "We have a constant: " . 2MYCONSTANT; ?> The script echoes: We have a constant: avalue Fatal Error The script echoes: We have a constant: 2MYCONSTANT

14.What happens when you run the script below ? <?php $language = 'PHP'; function PHP() { return 'This is the ' . __FUNCTION__ . ' function'; } echo $language(); ?> PHP Parse error: syntax error on line 7 The script echoes: "This is the PHP function" Notice: Use of undefined constant _FUNCTION__ - assumed '__FUNCTION__' on line 5

15.What happens when you run the script below ? <?php if(-1) echo 'TRUE'; else echo 'FALSE'; ?> The script echoes: "TRUE" The script echoes: "FALSE" Syntax Error

16.Is there a way to make E_NOTICE type errors behave like fatal errors in PHP and therefor stop script execution when such an error occurs ? There's no way to do that. Notices are exactly what their name states: Notices. By setting a custom error handler, one can alter the way PHP treats E_NOTICE errors and then stop the script. By using the "register_shutdown_function()" a developer can stop script execution at any time, including when notices are thrown.

17.What will happen when you run the code below ? <?php function somefunc( DOMDocument $param ) { if($param instanceof DOMDocument) return 0; else return 1; } echo somefunc('abcd'); ?> Because the parameter passed to the function is not an instance of the DOMDocument class the function will return 1 Because the parameter passed to the function is a string the function will return 0 Because the parameter passed to the function is not an instance of the DOMDocument class the script will throw a Fatal Error

18.What is the output of the script below ? <?php $i = 1; do{ $i++; } while ($i===0); echo $i; ?> 1 0 2

19.Which of the following is the best option to iterate and modify every element of an array ? for($i = 0; $i < count($array); $i++) { /* ... * / } foreach($array as $key => &$val) { /* ... * / } foreach($array as $key => $val) { /* ... * / } while(list($key, $val) = each($array)) { /* ... * / }

20.What is the result of the code below ? <?php define("FOO", 10); $array = [10 => FOO,"FOO" => 20]; print $array[$array[FOO]] * $array["FOO"]; FOO 100 200 20 10