Don't forget to hit the ⭐ if you like this repo.
Please answer the following 50 multiple-choice questions to test your knowledge in PHP fundamentals. You must place your answer file in the submission folder. Within the submission/Q4/set_a
folder, create a folder called your github_id
.
-
Which of the following is the correct opening tag for PHP code?
- a)
<?php
- b)
<php>
- c)
<script>
- d)
<?=
- a)
-
What does PHP stand for?
- a) Personal Home Page
- b) Pretext Hypertext Processor
- c) PHP: Hypertext Preprocessor
- d) Preprocessor Hyperlink Pages
-
Which PHP function is used to output text to the browser?
- a)
echo
- b)
print
- c)
display
- d)
output
- a)
-
How do you assign a value to a variable in PHP?
- a) Using the assignment operator (
=
) - b) Using the
set
keyword - c) Using the
value
keyword - d) Using the
assign
function
- a) Using the assignment operator (
-
What is the correct way to end a PHP statement?
- a) With a semicolon (
;
) - b) With a colon (
:
) - c) With a comma (
,
) - d) With a period (
.
)
- a) With a semicolon (
-
Which operator is used for concatenation in PHP?
- a)
.
- b)
+
- c)
-
- d)
*
- a)
-
What is the correct way to comment a single line in PHP?
- a)
//
- b)
#
- c)
/* ... */
- d)
<!-- -->
- a)
-
Which function is used to determine the length of a string in PHP?
- a)
strlen()
- b)
strlength()
- c)
stringlength()
- d)
length()
- a)
-
What is the output of the following code?
$x = 10; $y = 5; $result = $x + $y; echo $result;
- a) 15
- b) 10 + 5
- c) $result
- d) Error
-
What is the correct way to declare an array in PHP?
- a)
$fruits = array("Apple", "Banana", "Orange");
- b)
$fruits = ["Apple", "Banana", "Orange"];
- c)
$fruits = "Apple", "Banana", "Orange";
- d)
$fruits = ("Apple", "Banana", "Orange");
- a)
-
How do you access the value at a specific index in an array?
- a) By using square brackets
[]
and the index number - b) By using parentheses
()
and the index number - c) By using curly braces
{}
and the index number - d) By using a period
.
and the index number
- a) By using square brackets
-
What is the output of the following code?
$num = 7; if ($num % 2 == 0) { echo "Even"; } else { echo "Odd"; }
- a) Even
- b) Odd
- c) 7
- d) Error
-
Which function is used to include an external PHP file?
- a)
include()
- b)
import()
- c)
require()
- d)
load()
- a)
-
What is the correct way to define a constant in PHP?
- a)
define("PI", 3.14);
- b)
constant("PI", 3.14);
- c)
$PI = 3.14;
- d)
const PI = 3.14;
- a)
-
How do you start a session in PHP?
- a)
session_start()
- b)
start_session()
- c)
new Session()
- d)
begin_session()
- a)
-
What is the correct way to retrieve a value from a query string in PHP?
- a)
$_GET["param"]
- b)
$_POST["param"]
- c)
$_REQUEST["param"]
- d)
$_SERVER["param"]
- a)
-
Which PHP superglobal variable stores cookies?
- a)
$_SESSION
- b)
$_GET
- c)
$_POST
- d)
$_COOKIE
- a)
-
What is the correct way to redirect a user to a different page in PHP?
- a)
header("Location: newpage.php");
- b)
redirect("newpage.php");
- c)
load_page("newpage.php");
- d)
goto("newpage.php");
- a)
-
What is the output of the following code?
$num1 = 5; $num2 = "10"; $result = $num1 + $num2; echo $result;
- a) 15
- b) 510
- c) Error
- d) 510 (as a string)
-
What is the correct way to connect to a MySQL database in PHP?
- a)
mysqli_connect()
- b)
mysql_connect()
- c)
pdo_connect()
- d)
db_connect()
- a)
-
What is the purpose of prepared statements in PHP?
- a) To prevent SQL injection attacks
- b) To optimize database queries
- c) To execute complex SQL queries
- d) To store frequently used SQL queries
-
What is the correct way to retrieve the number of rows returned from a MySQL query in PHP?
- a)
mysqli_num_rows()
- b)
mysql_num_rows()
- c)
pdo_num_rows()
- d)
db_num_rows()
- a)
-
How do you close a MySQL database connection in PHP?
- a)
mysqli_close()
- b)
mysql_close()
- c)
pdo_close()
- d)
db_close()
- a)
-
What is the output of the following code?
$fruit = "Apple"; switch ($fruit) { case "Apple": echo "It's a fruit."; break; case "Banana": echo "It's a fruit."; break; default: echo "Unknown fruit."; }
- a) It's a fruit.
- b) Unknown fruit.
- c) Apple
- d) Banana
-
What is the correct way to handle errors in PHP?
- a) Using
try...catch
blocks - b) Using
if...else
statements - c) Using
error_reporting()
- d) Using
print_error()
- a) Using
-
What is the purpose of the
__construct()
function in PHP classes?- a) To create a new object instance of the class
- b) To destroy an object instance of the class
- c) To initialize the object's properties and perform setup tasks
- d) To define the behavior of the class methods
-
What is the correct way to create an object of a class in PHP?
- a)
$object = new ClassName();
- b)
$object = createObject(ClassName);
- c)
$object = instantiate(ClassName);
- d)
$object = object(ClassName);
- a)
-
What is the output of the following code?
class Person { public $name; public function sayHello() { echo "Hello, " . $this->name; } } $person = new Person(); $person->name = "John"; $person->sayHello();
- a) Hello, John
- b) Hello, Person
- c) Error
- d) John
-
How do you access a static property or method of a class in PHP?
- a) By using the
::
operator - b) By using the
.
operator - c) By using the
->
operator - d) By using the
=>
operator
- a) By using the
-
What is the purpose of the
public
access modifier in PHP class properties and methods?- a) It allows access to the property or method from anywhere
- b) It allows access to the property or method only within the class
- c) It allows access to the property or method within the class and its subclasses
- d) It restricts access to the property or method
-
What is the output of the following code?
function add(&$num) { $num += 10; } $number = 5; add($number); echo $number;
- a) 5
- b) 10
- c) 15
- d) Error
-
What is the correct way to read a file line by line in PHP?
- a) Using the
fread()
function - b) Using the
file_get_contents()
function - c) Using the
fgets()
function - d) Using the
file()
function
- a) Using the
-
What is the purpose of the
file_exists()
function in PHP?- a) To check if a file or directory exists
- b) To read the contents of a file
- c) To write data to a file
- d) To delete a file or directory
-
What is the output of the following code?
function multiply($num1, $num2 = 2) { return $num1 * $num2; } $result1 = multiply(5); $result2 = multiply(5, 3); echo $result1 . ", " . $result2;
- a) 5, 15
- b) 10, 15
- c) 5, 6
- d) Error
-
What is the correct way to remove leading and trailing whitespace from a string in PHP?
- a)
trim()
- b)
strip()
- c)
removeWhitespace()
- d)
cleanWhitespace()
- a)
-
What is the output of the following code?
$x = 10; echo ++$x;
- a) 10
- b) 11
- c) Error
- d) ++10
-
What is the purpose of the
foreach
loop in PHP?- a) To iterate over elements in an array or collection
- b) To execute a block of code repeatedly
- c) To conditionally execute code based on a condition
- d) To perform a specific task a fixed number of times
-
What is the correct way to check if a variable is empty in PHP?
- a)
empty()
- b)
isNull()
- c)
isBlank()
- d)
isEmpty()
- a)
-
What is the output of the following code?
$str = "Hello World"; echo str_replace("World", "PHP", $str);
- a) Hello World
- b) Hello PHP
- c) World PHP
- d) Error
-
What is the purpose of the
array_push()
function in PHP?- a) To add elements to the beginning of an array
- b) To remove elements from an array
- c) To merge two or more arrays
- d) To add elements to the end of an array
-
What is the output of the following code?
$numbers = [1, 2, 3, 4, 5]; $sum = array_sum($numbers); echo $sum;
- a) 1
- b) 15
- c) 5
- d) Error
-
What is the purpose of the
explode()
function in PHP?- a) To split a string into an array
- b) To combine multiple strings into one string
- c) To reverse the characters in a string
- d) To convert a string to lowercase
-
What is the output of the following code?
$number = 3; echo pow($number, 2);
- a) 3
- b) 9
- c) 6
- d) Error
-
What is the purpose of the
date()
function in PHP?- a) To retrieve the current date and time
- b) To format a date and time string
- c) To compare two dates
- d) To add or subtract days from a date
-
What is the output of the following code?
$age = 25; echo ($age >= 18) ? "Adult" : "Minor";
- a) Adult
- b) Minor
- c) 25
- d) Error
-
What is the purpose of the
header()
function in PHP?- a) To set HTTP headers for the response
- b) To include an external PHP file
- c) To redirect the user to a different page
- d) To retrieve the value of a request header
-
What is the output of the following code?
$str = "Hello"; echo strlen($str);
- a) 5
- b) 6
- c) 4
- d) Error
-
What is the purpose of the
json_encode()
function in PHP?- a) To convert a PHP array or object into a JSON string
- b) To convert a JSON string into a PHP array or object
- c) To encode a URL string
- d) To encrypt data
-
What is the output of the following code?
$colors = ["Red", "Green", "Blue"]; $color = "Red"; if (in_array($color, $colors)) { echo "Color found!"; } else { echo "Color not found!"; }
- a) Color found!
- b) Color not found!
- c) Red
- d) Error
-
What is the purpose of the
filter_input()
function in PHP?- a) To filter and sanitize user input data
- b) To retrieve the value of a request header
- c) To validate email addresses
- d) To decode a JSON string
Please create an Issue for any improvements, suggestions or errors in the content.
You can also contact me using Linkedin for any other queries or feedback.