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.Considering the code below, which of these statements are true?

    ini_set("allow_url_fopen", "1");
    $page = file_get_contents("http://www.codepunker.com");
    echo "Successfully fetched website contents";
    echo "An error has occurred";

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.When working with unfamiliar code, what is the best way to find out in which file a class is defined ?

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

  $out = array();
  exec("grep -r 'Classname' .", $out);

  $classes = get_declared_classes();

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

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

9.What is the output of the following code block? $array = array(1 => 0, 2, 3, 4); array_splice($array, 3, count($array), array_merge(array('x'), array_slice($array, 3))); var_dump($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.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));

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

14.Given a PHP value, which sample shows how to convert the value to JSON?

15.Which one of the following XML declarations is NOT valid?

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

17.What does the following function do, when passed two integer values for $p and $q? function magic($p, $q) { return ($q == 0) ? $p: magic($q, $p % $q); }

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

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

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

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 list of potential data sources should be considered trusted?

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

24.Given the two values below, which of the following possibilities will print 10 foos20 bars? $var1 = "10 foos"; $var2 = "20 bars"; print ???????;

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

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

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

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

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

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

Finish Quiz