Test HTTP Requests Tools Blog PHP Quiz API Log In With Github
Test HTTP Requests Tools Blog PHP Quiz API Log In With Github
You are seeing the archive version of the website.
Click here to go to the live website

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 happens when the code below is executed ?


  <?php
    class foo
    {
      private $variable;

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

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

2.What happens when the script below is executed ?


  <?php
    namespace CustomArea;
    error_reporting(E_ALL);
    ini_set("display_errors", "on");
    function var_dump($a)
    {
      return str_replace("Weird", $a, "Weird stuff can happen");
    }
    $a = "In programming";
    echo var_dump($a);
  ?>

3.What is the output of the code below ?

    
    $now  = new DateTime();
    $now2 = new DateTime();

    $ago  = new DateInterval('P4Y10M3W');
    $ago2 = new DateInterval('P4Y10M2W7D');

    $then  = $now->sub($ago);
    $date1 = $then->format('Y-m-d');

    $then2 = $now2->sub($ago2);
    $date2 = $then2->format('Y-m-d');

    var_dump ($date1 === $date2);
    

4.Considering the code below ...

  
    <?php 
        
    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)
      {
        if(empty($first_name))
        {
          throw new AppException('First Name is required', 1);
        }

        if(empty($last_name))
        {
          throw new AppException('Last Name is required', 2);
        }
      }
    }

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

... which of these statements are correct ?

5.What is the output of the code below ?

  
    <?php
    namespace animals;

    ini_set('error_reporting', E_ALL);
    ini_set('display_errors', 'on');

    class Cat
    {
      static function Definition()
      {
        return 'Cats are ' . __NAMESPACE__;
      }
    }

    namespace animals\pets;

    class Cat
    {
      static function Definition()
      {
        return 'Cats are ' . __NAMESPACE__;
      }
    }

    echo Cat::Definition();    
  

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

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

8.When dealing with cloned objects in PHP, which of the following statements are true ?

9.What is the best way to store and verify passwords in PHP ?

10.What is the output of the following PHP script?

                    <?php 
                        $a = 1;
                        $b = 2.5;
                        $c = 0xFF;
                        $d = $b + $c;
                        $e = $d * $b;
                        $f = ($d + $e) % $a;
                        print ($f + $e);
                  

11.How do you access standard I/O and error streams ?

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

13.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);

14.Which of the following functions are used with the internal array pointer to accomplish an action?

15.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?

16.Which of the following are valid PHP variables?

17.What is the output of the following? $a = 010; $b = 0xA; $c = 2; print $a + $b + $c;

18.What SimpleXML function is used to parse a file?

19.Which of the following will NOT instantiate a DateTime object with the current timestamp?

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

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

22.The _______ method will be called automatically when an object is represented as a string.

23.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?

24. Which of the following list of potential data sources should be considered trusted?

25.When comparing two strings, which of the following is acceptable?

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

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

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

29.If you would like to store your session in the database, you would do which of the following?

30.Setting a cookie on the client in PHP 5 can be best accomplished by:

Finish Quiz