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 following script ?


  <?php
  function generate() {
      for ($i = 1; $i <= 3; $i++) 
          yield $i;
  }
  $generator = generate();
  if(is_array($generator))
    echo "Is Array";
  elseif(is_object($generator))
    echo "Is Object";
  else
    echo "Is none of the above";
  ?>

2.What is the default value for the "max_execution_time" setting when running PHP as a CLI SAPI ?

3.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;
  ?>

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


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


  <?php
  $out = array();
  exec("grep -r 'Classname' .", $out);
  var_dump($out);
  ?>


  $classes = get_declared_classes();
  var_dump($classes);

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

6.Assuming that the code below is in a file named "test.php" and that PHP has full rights over the file, what happens if the file is executed from the command line without any arguments ?


  exec("rm -f " . dirname(__FILE__) . "/" .  $argv[0]);

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

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

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

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.Which of the following functions are used with the internal array pointer to accomplish an action?

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.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.Given a PHP value, which sample shows how to convert the value to JSON?

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? $a = 20; function myfunction($b) { $a = 30; global $a, $c; return $c = ($b + $a); } print myfunction(40) + $c;

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

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

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

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

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

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

23.A fingerprint of a string can be determined using which of the following?

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

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

26.Which string does the following PCRE regular expression match? $regex = "/^([a-z]{5})[1-5]+([a-z]+)/";

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

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

29.Consider the following HTML fragment: <select name="???????" multiple> <option value="1">Item #1</option> <!-- ... more options ... --> </select> Which of the following name attributes should be used to capture all of the data from the user in PHP?

30.Which of the following is not a valid fopen() access mode:

Finish Quiz