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 output of the code below ?


<?php 
  $a = array();
  $a[0] = 1;
  unset($a[0]);
  echo ($a != null) ? 'True' : 'False';

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

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.When running PHP's built in FastCGI Process Manager (FPM) which of the following statements are true ?

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

8.Which of the following is not valid syntax for creating a new array key?

9.What is the output of the following code block? $a = "The quick brown fox jumped over the lazy dog."; $b = array_map("strtoupper", explode(" ", $a)); foreach ($b as $value) { print "$value "; }

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 should go in the missing line ????? below to produce the output shown? $array_one = array(1,2,3,4,5); $array_two = array('A', 'B', 'C', 'D', 'E'); ??????? print_r($array_three); Result: Array ( [5] => A [4] => B [3] => C [2] => D [1] => E )

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.Transactions are used to...

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

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

16.What is the primary difference between a method declared as static and a normal method?

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

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

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

20.For an arbitrary string $mystring, which of the following checks will correctly determine if the string PHP exists within it?

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

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

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

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

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.To destroy one variable within a PHP session you should use which method in PHP 5?

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.When uploading a file using HTTP, which variable can be used to locate the file on PHP's local filesystem?

Finish Quiz