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.

Beginners PHP Quiz

Topic: PHP/MySQL Last updated on: 04-13-2017

This quiz is primarily targeted for beginner PHP developers. It covers basic concepts like variable types, arrays, basic OOP principles, functions MySQL queries and other gotchas related to the PHP programming language.

1.What is the result of the code below ?

                    <?php echo ("0.00") ? "mary" : "angie"; ?>
                  

2.What is the output of the following code:

                    <?php
                      function myFunc()
                      {
                        $in = "nothing";
                        return func_get_args();
                      }
                      $in = "something";
                      var_dump(myFunc($in));
                    ?>
                  

3.What are the possible security implications of printing an unfiltered request variable ?

4.What is wrong in the following code (assume that $db is an instance of mysqli, mytable exists and has a column called student):

                    <?php 
                      if($result = $db->query("SELECT student FROM mytable WHERE student = '$_POST["student"]'"))
                        while($row = $result->fetch_object())
                          echo $row->student;
                    ?>
                  

5.Which PHP function(s) can be used to check if a variable is defined and is not NULL ?

6.How do you obtain the length of a string in PHP ?

7.What is the output of the following snippet ?

  
    <?php
      $a = strlen(NULL);
      echo $a++;
    ?>
  

8.What is the output of the code below ?

  
    <?php
    $a = array(0 => "MySQL", 2=>"PHP", 3=>"JAVA", 4 => "JavaScript");
    $a = array_values($a);
    echo $a[1];
    ?>
  

9.What is the output of the code below ?

  
    <?php
    $a = array("PHP", "MySQL", 4, "1");
    $sum = array_sum($a);
    echo $sum;
    ?>
  

10.Considering the table structure below what MySQL query will load a result set containing all students names enrolled in the programming classes ?

  
    students table:
    | studentid | name |
    --------------------
    | 1         | Mike |
    | 2         | John |
    | 3         | Jeff |
    | 4         | Anne |

    classes table:
    | classid | classname 
    -------------------------
    | 1       | Math
    | 2       | Programming
    | 3       | Biology

    classes_to_students table:
    | studentid | classid
    -------------------------
    | 1       | 1
    | 2       | 2
    | 3       | 2
    | 4       | 3
  

11.What happens when the script below is executed ?

  
    <?php
    $a = array(1,2,3);
    $b = array(7,8,9);

    $counta = count($a);
    $countb = sizeof($b);

    if($counta===$countb) {
      echo "They are equal and have the same type";
    } else {
      echo "They are not equal or they are not of the same type";
    }
    ?>
  

12.What is the final value of $i ?

  
    <?php
    $numbers = array(10, "10", 10.5, "10.5", null, true, false);
    $i = 0;
    foreach ($numbers as $number)
    {
      if(is_int($number))
        $i++;
      else
        $i--;
    }
    echo $i;
    ?>
  

13.What happens when you run the script below ?

  
    <?php
    define("2MYCONSTANT", "avalue");
    if(defined("2MYCONSTANT"))
      echo "We have a constant: " . 2MYCONSTANT;
    ?>
  

14.What happens when you run the script below ?

  
    <?php
      $language = 'PHP';
      function PHP()
      {
        return 'This is the ' . __FUNCTION__ . ' function';
      }
      echo $language();
    ?>
  

15.What happens when you run the script below ?

  
    <?php
      if(-1)
        echo 'TRUE';
      else
        echo 'FALSE';
    ?>
  

16.Is there a way to make E_NOTICE type errors behave like fatal errors in PHP and therefor stop script execution when such an error occurs ?

17.What will happen when you run the code below ?


<?php 
function somefunc( DOMDocument $param )
{
  if($param instanceof DOMDocument)
    return 0;
  else
    return 1;
}
echo somefunc('abcd');
?>

18.What is the output of the script below ?

  
    <?php
      $i = 1;
      do{
        $i++;
      } while ($i===0);
      echo $i;
    ?>
  

19.Which of the following is the best option to iterate and modify every element of an array ?

20.What is the result of the code below ?

                    <?php 
                       define("FOO", 10);
                       $array = [10 => FOO,"FOO" => 20];
                       print $array[$array[FOO]] * $array["FOO"];
                  

21.What is the best way to ensure that a user-defined function is always passed an object of type `Cat` as its only parameter?

Finish Quiz