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

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

3.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();    
  

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

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

6.What happens if you execute the code below ?


<?php
    class someclass
    {
        public $someprop;
        
        function __construct()
        {
            $this->someprop = 1;
        }
    }

    function somefunc(&$instance) {
        unset($instance);
    }

    $instance = new someclass;
    somefunc($instance);
    var_dump($instance);


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

8.What is the output of this code snippet? $a = array(0.001 => 'b', .1 => 'c'); var_dump($a);

9.If you wanted a variable containing the letters A through Z, that allowed you to access each letter independently, which of the following approaches could you use?

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

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

13.The following code snippet displays what for the resultant array? $a = array(1 => 0, 3 => 2, 4 => 6); $b = array(3 => 1, 4 => 3, 6 => 4); print_r(array_intersect($a, $b));

14.What is the best way to iterate and modify every element of an array using PHP 5?

15.What is the best way to ensure that a user-defined function is always passed an object as its single parameter?

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

17.What would you replace ??????? with, below, to make the string Hello, World! be displayed? function myfunction() { /* ??????? */ print $string; } myfunction("Hello, World!");

18.What are the three access modifiers that you can use in PHP objects?

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

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.Type-hinting and the instanceof keyword can be used to check what types of things about variables?

22.What three special methods can be used to perform special logic in the event a particular accessed method or member variable is not found?

23.What is the best measure one can take to prevent a cross-site request forgery?

24.What variable reference would go in the spots indicated by ????? in the code segment below? $msg = "The Quick Brown Foxed Jumped Over the Lazy Dog"; $state = true; $retval = ""; for ($i = 0; (isset(??????)); $i++) { if($state) { $retval .= strtolower(?????); } else { $retval .= strtoupper(?????); } $state = !$state; } print $retval;

25.Which of the following is the best way to split a string on the "-=-" pattern?

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

27.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"

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

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

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

Finish Quiz