PHP Beginner Quiz
30 questions on PHP for Beginners.
Question 1: Which symbol is used before variables in PHP?
- A. #
- B. @
- C. $ β (correct answer)
- D. %
Explanation: All variables in PHP must begin with a dollar sign $ followed by the variable name.
Question 2: What are the standard script delimiters used to wrap PHP code blocks?
- A.
<?php ... ?> β (correct answer)
- B.
<script ... >
- C.
<? ... ?>
- D.
<php> ... </php>
Explanation: Standard PHP tags are <?php to start a block, and ?> to end it. Short open tags (<? ... ?>) are also supported but discouraged in modern environments.
Question 3: Which PHP statement is commonly used to display text output to the screen?
- A. print_screen()
- B. write()
- C. echo β (correct answer)
- D. log()
Explanation: echo is a language construct in PHP used to output one or more strings to the browser. print is similar but returns 1.
Question 4: What will be the output of the following PHP code?
``php
<?php
$a = "5";
$b = 5;
echo ($a === $b) ? "Yes" : "No";
?>
``
- A. Yes
- B. No β (correct answer)
- C. Compiler Error
- D. Garbage Value
Explanation: The strict comparison operator === checks both value and type. $a is a string "5", and $b is an integer 5. Since the types are different, it returns No.
Question 5: Which superglobal array in PHP holds form data sent via the HTTP POST method?
- A. $_GET
- B. $_POST β (correct answer)
- C. $_REQUEST
- D. $_SERVER
Explanation: $_POST is a PHP superglobal array used to collect form values after submitting an HTML form with method="post".
Question 6: What is the correct way to concatenate two string variables in PHP?
- A. $s1 + $s2
- B. $s1 . $s2 β (correct answer)
- C. $s1 & $s2
- D. $s1 , $s2
Explanation: PHP uses the dot operator . for string concatenation. The plus operator + is used for arithmetic addition.
Question 7: How do you declare an associative array (key-value store) in modern PHP?
- A.
$arr = ["key" => "value"]; β (correct answer)
- B.
$arr = ["key" = "value"];
- C.
$arr = ["key" : "value"];
- D.
$arr = ["key" -> "value"];
Explanation: Associative arrays map keys to values using the double arrow operator =>. Square brackets [] are standard short array syntax in PHP 5.4+.
Question 8: Which PHP superglobal is used to store persistent variables across multiple page requests for a user session?
- A. $_COOKIE
- B. $_SESSION β (correct answer)
- C. $_SERVER
- D. $_ENV
Explanation: The $_SESSION superglobal array stores session variables, allowing persistent user states across multiple pages after calling session_start().
Question 9: Which built-in function checks if a variable has been declared and is not null in PHP?
- A. isset() β (correct answer)
- B. empty()
- C. is_null()
- D. defined()
Explanation: isset() returns true if the variable exists and has a value other than null. empty() checks if a variable is empty (e.g. false, 0, empty string, or null).
Question 10: Which keyword is used to declare a class constructor in PHP?
- A.
function ClassName()
- B.
function __init()
- C.
function __construct() β (correct answer)
- D.
function create()
Explanation: PHP uses double-underscore magic methods. __construct() declares the constructor of a class, which runs automatically upon instantiation.
Question 11: What will be the output of the following PHP code?
``php
<?php
$x = 10;
function test() {
// echo $x;
}
test();
?>
``
- A. 10
- B. Null
- C. Compiler Error
- D. No Output β (correct answer)
Explanation: Because the echo inside test() is commented out, nothing is printed. Furthermore, in PHP, global variables must be explicitly declared inside functions using the global keyword to be accessible inside the function scope.
Question 12: Which function is used to establish a connection to a MySQL database using PHP's PDO extension?
- A. mysqli_connect()
- B. pdo_connect()
- C. new PDO() β (correct answer)
- D. connect_db()
Explanation: PHP Data Objects (PDO) are initialized by instantiating the class using new PDO($dsn, $username, $password).
Question 13: What is the default index of the first element in a indexed PHP array?
- A. 1
- B. -1
- C. 0 β (correct answer)
- D. It depends on declaration
Explanation: By default, indexed arrays in PHP are zero-indexed. The index of the first item is 0.
Question 14: Which PHP function is used to include and evaluate a specified file during script execution, throwing a fatal error if the file is not found?
- A. include
- B. require β (correct answer)
- C. include_once
- D. import
Explanation: require stops execution and throws a fatal error if the target file cannot be opened or does not exist. include prints a warning but allows the script to continue.
Question 15: What does the count() function do in PHP?
- A. Counts words in a string
- B. Counts characters in a string
- C. Counts elements in an array or countable object β (correct answer)
- D. Counts rows in a SQL database
Explanation: The built-in count() function returns the number of elements in an array or fields inside countable objects.
Question 16: What is the difference between single-quoted and double-quoted strings in PHP?
- A. Double-quoted strings evaluate variables inside them; single-quoted strings treat variables as literal text β (correct answer)
- B. Single-quoted strings are slower
- C. They are identical
- D. Single-quoted strings allow string interpolation
Explanation: Double quotes " " automatically interpolate variables ("Hello $name") and parse escape sequences. Single quotes ' ' output text literally without parsing variables or escape sequences.
Question 17: What will be the output of the following PHP code?
``php
<?php
$a = [1, 2, 3];
$b = $a;
$b[0] = 99;
echo $a[0];
?>
``
- A. 1 β (correct answer)
- B. 99
- C. Compiler Error
- D. Garbage Value
Explanation: In PHP, arrays are passed and assigned **by value** by default. $b = $a creates a copy of the array. Changing $b[0] does not affect $a[0].
Question 18: Which function starts a new or resumes an existing session in PHP?
- A. start_session()
- B. session_start() β (correct answer)
- C. session_init()
- D. open_session()
Explanation: session_start() initializes session data. It must be called at the very top of the page before any output (headers, HTML, or spaces) is sent.
Question 19: What is the correct syntax to inherit a base class in PHP?
- A.
class Child extends Parent {} β (correct answer)
- B.
class Child : public Parent {}
- C.
class Child implements Parent {}
- D.
class Child inherits Parent {}
Explanation: PHP uses the extends keyword for class inheritance. The implements keyword is used for interface implementation.
Question 20: Which function sends a cookie to the user's browser in PHP?
- A. setcookie() β (correct answer)
- B. cookie()
- C. $_COOKIE['name'] = 'val'
- D. send_cookie()
Explanation: setcookie() defines a cookie to be sent along with HTTP headers. It must be sent before any other output is output by the script.
Question 21: What is the purpose of the header() function in PHP?
- A. To add HTML head tags
- B. To send raw HTTP headers to the browser (e.g., redirecting pages) β (correct answer)
- C. To print class descriptions
- D. To start database execution
Explanation: header() sends raw HTTP headers. A common use case is page redirection: header("Location: dashboard.php");.
Question 22: What is the output?
``php
<?php
$x = 5;
function add() {
global $x;
$x += 10;
}
add();
echo $x;
?>
``
- A. 5
- B. 15 β (correct answer)
- C. Compiler Error
- D. No Output
Explanation: Declaring global $x pulls the variable $x from global scope into the function scope, allowing the function to increment the global $x to 15.
Question 23: Which comparison operator is called the "Spaceship Operator" in PHP 7+?
- A. <=> β (correct answer)
- B. =>
- C. ??
- D. ?:
Explanation: The spaceship operator <=> compares two expressions. It returns -1 if left is less than right, 0 if equal, and 1 if left is greater than right.
Question 24: Which superglobal holds information about headers, paths, and script locations?
- A. $_ENV
- B. $_REQUEST
- C. $_SERVER β (correct answer)
- D. $_GLOBALS
Explanation: $_SERVER contains headers, paths, server configurations, and current script URLs.
Question 25: Which keyword is used to access parent class constructor or methods from within a subclass in PHP?
- A. parent:: β (correct answer)
- B. super::
- C. base::
- D. parent->
Explanation: The parent:: scope resolution prefix calls methods or constructors defined in the direct base class of the current class.
Question 26: What is the difference between require and require_once in PHP?
- A.
require_once verifies if the file has already been loaded, avoiding duplicate function declarations and errors β (correct answer)
- B.
require is faster
- C. They are identical
- D.
require_once only loads files in the current folder
Explanation: require_once functions exactly like require, but ignores loading if the specified file was already included or required previously, preventing redeclaration errors.
Question 27: What will be the output of the following PHP code?
``php
<?php
$a = "hello";
$$a = "world";
echo $hello;
?>
``
- A. hello
- B. world β (correct answer)
- C. Compiler Error
- D. hello world
Explanation: This is a **variable variable** in PHP. $$a substitutes $a's value, expanding it as a variable named $hello. Therefore, assigning "world" to $$a creates a variable $hello holding "world".
Question 28: What does the null coalescing operator ?? do in PHP?
- A. Performs strict arithmetic
- B. Returns its left operand if it exists and is not null; otherwise returns right operand β (correct answer)
- C. Empties session data
- D. Compares string sizes
Explanation: The null coalescing operator ?? is a shorthand replacing isset($_GET['x']) ? $_GET['x'] : 'default'.
Question 29: Which built-in PHP function parses an associative array to a JSON string?
- A. json_encode() β (correct answer)
- B. json_decode()
- C. serialize()
- D. unserialize()
Explanation: json_encode() parses arrays or objects into valid JSON strings. json_decode() does the opposite, converting JSON back to PHP structures.
Question 30: What is SQL Injection, and how does PHP's PDO prevent it?
- A. It is a styling error; PDO speeds up page loading
- B. It is a hacking attack where malicious SQL is injected into fields; PDO prevents it using prepared statements that separate query logic from input data β (correct answer)
- C. It is an index crash
- D. It is a cookie theft
Explanation: Prepared statements in PDO parameterize queries, ensuring user input is sanitized and evaluated strictly as data, never as executable SQL commands.