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?


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


2.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";
  ?>

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.How does Opcode Cache improve performance in PHP 5.5+ ?

6.What happens if you execute the code below ?


<?php
    class someclass
    {
        public $someprop;
        
        function __construct()
        {
            $this->someprop = 1;
        }
    }

    function somefunc(&$instance) {
        unset($instance);
    }

    $instance = new someclass;
    somefunc($instance);
    var_dump($instance);


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

8.Which of the following functions will sort an array in ascending order by value, while preserving key associations?

9.What is the output of this code snippet? $a = array(0.001 => 'b', .1 => 'c'); var_dump($a);

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

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

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

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

15.Which of the following tags are an acceptable way to begin a PHP Code block?

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

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

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

19.What is the difference between the ``include`` and ``require`` language constructs?

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

21.How does one access standard input/output and error streams in PHP 5?

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

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

24. Which of the following list of potential data sources should be considered trusted?

25.When comparing two strings, which of the following is acceptable?

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

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

28.Setting a cookie on the client in PHP 5 can be best accomplished by:

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

30.During an HTTP authentication, how does one determine the username and password provided by the browser?

Finish Quiz