Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the quizzes list
We have updated the website and our policies to make sure your privacy rights and security are respected.
Click here to learn more about the way our website handles your data.

Remove this message.

Advanced PHP Quiz

Topic: PHP/MySQL Last updated on: 01-22-2018

This is an advanced PHP Quiz. It contains questions for seasoned developers about namespaces, traits, handlers and settings, command line execution, exception handling, OOP and other modern PHP features and functions.

1.What is the output of the following script ?

  function generate() {
      for ($i = 1; $i <= 3; $i++) 
          yield $i;
  $generator = generate();
    echo "Is Array";
    echo "Is Object";
    echo "Is none of the above";

2.When working with unfamiliar code, what is the best way to find out in which file a class is defined ?

  $reflection = new ReflectionClass('ClassName');
  echo $reflection->getFileName();

  $out = array();
  exec("grep -r 'Classname' .", $out);

  $classes = get_declared_classes();

3.Which of the statements about traits below are true ?

4.What happens if you execute the code below ?

    class someclass
        public $someprop;
        function __construct()
            $this->someprop = 1;

    function somefunc(&$instance) {

    $instance = new someclass;

5.Which of the following is not valid syntax for creating a new array key?

6.What is the output of the following code block? $a = "The quick brown fox jumped over the lazy dog."; $b = array_map("strtoupper", explode(" ", $a)); foreach ($b as $value) { print "$value "; }

7.Which from the following list is not an appropriate use of an array?

8.Which of the following functions could be used to break a string into an array?

9.Which key will not be displayed from the following code block? $array = array('a' => 'John', 'b' => 'Coggeshall', 'c' => array('d' => 'John', 'e' => 'Smith')); function display($item, $key) { print "$key => $item\n"; } array_walk_recursive($array, "display");

10.What is the result of the following code snippet? $array = array('a' => 'John', 'b' => 'Coggeshall', 'c' => array('d' => 'John', 'e' => 'Smith')); function something($array) { extract($array); return $c['e']; } print something($array);

11.Which of the following are valid PHP variables?

12.Given a PHP value, which sample shows how to convert the value to JSON?

13.Consider the following table data and PHP code. What is the outcome? Table data (table name "users" with primary key "id"): id name email ------- ----------- ------------------- 1 anna alpha@example.com 2 betty beta@example.org 3 clara gamma@example.net 5 sue sigma@example.info PHP code (assume the PDO connection is correctly established): $dsn = 'mysql:host=localhost;dbname=exam'; $user = 'username'; $pass = '********'; $pdo = new PDO($dsn, $user, $pass); $cmd = "SELECT * FROM users WHERE id = :id"; $stmt = $pdo->prepare($cmd); $id = 3; $stmt->bindParam('id', $id); $stmt->execute(); $stmt->bindColumn(3, $result); $row = $stmt->fetch(PDO::FETCH_BOUND);

14.Which technique should be used to speed up joins without changing their results?

15.What would go in place of ?????? below to make this script execute without a fatal error? $a = 1; $b = 0; /* ?????? */ $c = $a / $b;

16.What is the output of the following function? function &find_variable(&$one, &$two, &$three) { if($one > 10 && $one < 20) return $one; if($two > 10 && $two < 20) return $two; if($three > 10 && $three < 20) return $three; } $one = 2; $two = 20; $three = 15; $var = &find_variable($one, $two, $three); $var++; print "1: $one, 2: $two, 3: $three";

17.When checking to see if two variables contain the same instance of an object, which of the following comparisons should be used?

18.How can you modify the copy of an object during a clone operation?

19.What is the output of the following script? class ClassOne { protected $a = 10; public function changeValue($b) { $this->a = $b; } } class ClassTwo extends ClassOne { protected $b = 10; public function changeValue($b) { $this->b = 10; parent::changeValue($this->a + $this->b); } public function displayValues() { print "a: {$this->a}, b: {$this->b}\n"; } } $obj = new ClassTwo(); $obj->changeValue(20); $obj->changeValue(10); $obj->displayValues();

20.The ______ keyword is used to indicate an incomplete class or method, which must be further extended and/or implemented in order to be used.

21.To ensure that a given object has a particular set of methods, you must provide a method list in the form of an ________ and then attach it as part of your class using the ________ keyword.

22.Type-hinting and the instanceof keyword can be used to check what types of things about variables?

23.For an arbitrary string $mystring, which of the following checks will correctly determine if the string PHP exists within it?

24.Which of the following are not valid ways to embed a variable into a string?

25.What is the output of the following code? $string = "14302"; $string[$string[2]] = "4"; print $string;

26.If you would like to change the session ID generation function, which of the following is the best approach for PHP 5?

27.When uploading a file using HTTP, which variable can be used to locate the file on PHP's local filesystem?

28.Setting a HTTP cookie on the client which is not URL-encoded is done how in PHP 5?

29.During an HTTP authentication, how does one determine the username and password provided by the browser?

30.One can ensure that headers can always be sent from a PHP script by doing what?

Finish Quiz