Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
Test HTTP Requests Tools Blog Learn Quizzes Smile API Log In / Sign Up
« Return to the quizzes list
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.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);

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

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.Which of th following statements about object serialization are true ?

7.What is the output of the following PHP script?

                    <?php 
                        $a = 1;
                        $b = 2.5;
                        $c = 0xFF;
                        $d = $b + $c;
                        $e = $d * $b;
                        $f = ($d + $e) % $a;
                        print ($f + $e);
                  

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

10.The ____ construct is particularly useful to assign your own variable names to values within an array.

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 output of the following? $a = 010; $b = 0xA; $c = 2; print $a + $b + $c;

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

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

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

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.How does one access standard input/output and error streams in PHP 5?

18.How can you modify the copy of an object during a clone operation?

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

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.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.For an arbitrary string $mystring, which of the following checks will correctly determine if the string PHP exists within it?

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

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

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

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

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

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.To force a user to redirect to a new URL from within a PHP 5 script, which of the following should be used?

Finish Quiz