Test HTTP Requests Tools Blog PHP Quiz API Log In With Github
Test HTTP Requests Tools Blog PHP Quiz API Log In With Github
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 output will this code produce ?

    class Disney
        public $cartoon;

        function __construct($cartoon)
            $this->cartoon = $cartoon;

    $disney = new Disney("The Beauty and The Beast");
    $waltDisney = $disney;
    $waltDisney->cartoon = "Pinocchio";
    echo $disney->cartoon;

2.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";

3.What happens when the code below is executed ?

    class foo
      private $variable;

      function __construct()
        $this->variable = 1;

      function __get($name)
        return $this->$name;
    $a = new foo;
    echo $a->variable;

4.Considering the code below ...

    class AppException extends Exception
      function __toString()
        return "Your code has just thrown an exception: {$this->message}\n";

    class Students
      public $first_name;
      public $last_name;

      public function __construct($first_name, $last_name)
          throw new AppException('First Name is required', 1);

          throw new AppException('Last Name is required', 2);

    try {
     new Students('', ''); 
    } catch (Exception $e) {
      echo $e;

... which of these statements are correct ?

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

6.When running PHP's built in FastCGI Process Manager (FPM) which of the following statements are true ?

7.Which of th following statements about object serialization are true ?

8.Which of the following functions will sort an array in ascending order by value, while preserving key associations?

9.Which function would you use to add an element to the beginning of an array?

10.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");

11.Given the following array: $array = array(1,1,2,3,4,4,5,6,6,6,6,3,2,2,2); The fastest way to determine the total number a particular value appears in the array is to use which function?

12.Which of the following tags are an acceptable way to begin a PHP Code block?

13.What is the output of the following PHP script? $a = 1; $b = 2.5; $c = 0xFF; $d = $b + $c; $e = $d * $b; $f = ($d + $e) % $a; print ($f + $e);

14.In PHP 5 you can use the ______ operator to ensure that an object is of a particular type. You can also use _______ in the function declaration.

15.What is wrong with the following code? function duplicate($obj) { $newObj = $obj; return $newObj; } $a = new MyClass(); $a_copy = duplicate($a); $a->setValue(10); $a_copy->setValue(20);

16.What is the primary difference between a method declared as static and a normal method?

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

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

19.When an object is serialized, which method will be called, automatically, providing your object with an opportunity to close any resources or otherwise prepare to be serialized?

20.Which php.ini directive should be disabled to prevent the execution of a remote PHP script via an include or require construct?

21.Consider the following code: session_start(); if(!empty($_REQUEST['id']) && !empty($_REQUEST['quantity'])) { $id = scrub_id($_REQUEST['id']); $quantity = scrub_quantity($_REQUEST['quantity']) $_SESSION['cart'][] = array('id' => $id, 'quantity' => $quantity) } /* .... */ What potential security hole would this code snippet produce?

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

23.Which function is best suited for removing markup tags from a string?

24.Consider the following script: $oranges = 10; $apples = 5; $string = "I have %d apples and %d oranges"; ??????? What could be placed in place of ?????? to output the string: "I have 5 apples and 10 oranges"

25.Which PCRE regular expression will match the string PhP5-rocks

26.To destroy one variable within a PHP session you should use which method in PHP 5?

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

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

29.Consider the following function: function redirect($url) { // Check to make sure we haven't already sent // the header: if(/*???????*/) { header("Location: $url"); } } What conditional should replace the ????? above?

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

Finish Quiz