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 ?


<?php 
    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 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;
  ?>

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
    $a = array('one'=>'php', 'two'=>'javascript', 'three'=>'python');
    $b = array('python', 'javascript', 'php');

    if(array_values(array_reverse($a)) === $b)
      echo 'true';
    else
      echo 'false';
  

6.Considering the following code which of the statements below is true ?


    class entity {
        public $name;
    }
    $human = new entity();
    $dog = new entity();
    $human->name = 0;
    $dog->name = "";

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

8.How does Opcode Cache improve performance in PHP 5.5+ ?

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

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

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

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

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

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

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

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.What is the primary difference between a method declared as static and a normal method?

19.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.

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

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

22.Identify the best approach to compare two variables in a binary-safe fashion

23.If regular expressions must be used, in general which type of regular expression functions available to PHP is preferred for performance reasons?

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

25.To destroy a PHP session completely, one must which of the following?

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

27.How does one create a cookie which will exist only until the browser session is terminated?

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

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.Which functions would be needed to translate the following string: I love PHP 5 to the following? 5 PHP EVOL I

Finish Quiz