PHP Practice Quiz
Question 1 1 pts
What does PHP stand for?
Group of answer choices
Private Home Page
Personal Hypertext Processor
PHP: Hypertext Preprocessor x
Question 2 1 pts
PHP server scripts are surrounded by delimiters, which?
Group of answer choices
...?>
<&>...&>
x
Question 3 1 pts
How do you write "Hello World" in PHP
Group of answer choices
"Hello World";
Document.Write("Hello World");
echo "Hello World"; x
Question 4 1 pts
All variables in PHP start with which symbol?
Group of answer choices
!
&
$ x
Question 5 1 pts
What is the correct way to end a PHP statement?
Group of answer choices
New line
.
; x
Question 6 1 pts
The PHP syntax is most similar to:
Group of answer choices
VBScript
JavaScript
Perl and C x
Question 7 1 pts
How do you get information from a form that is submitted using the "get" method?
Group of answer choices
Request.QueryString;
Request.Form;
$_GET[]; x
Question 8 1 pts
When using the POST method, variables are displayed in the URL:
Group of answer choices
True
False x
Question 9 1 pts
In PHP you can use both single quotes ( ' ' ) and double quotes ( " " ) for strings:
Group of answer choices
True x
False
Question 10 1 pts
Include files must have the file extension ".inc"
Group of answer choices
True
False x
Question 11 1 pts
What is the correct way to include the file "time.inc" ?
Group of answer choices
x
Question 12 1 pts
What is the correct way to create a function in PHP?
Group of answer choices
function myFunction() x
new_function myFunction()
create myFunction()
Question 12 1 pts
What is the correct way to create a function in PHP?
Group of answer choices
function myFunction() x
new_function myFunction()
create myFunction()
Question 14 1 pts
PHP allows you to send emails directly from a script
Group of answer choices
True x
False
Question 15 1 pts
Which superglobal variable holds information about headers, paths, and script locations?
Group of answer choices
$GLOBALS
$_SERVER x
$_GET
$_SESSION
Question 16 1 pts
What is the correct way to add 1 to the $count variable?
Group of answer choices
$count++; x
count++;
++count
$count =+1
Question 17 1 pts
What is a correct way to add a comment in PHP?
Group of answer choices
*\...\*
...
/*...*/ x
Question 18 1 pts
PHP can be run on Microsoft Windows IIS(Internet Information Server):
Group of answer choices
True x
False
Question 19 1 pts
The die() and exit() functions do the exact same thing.
Group of answer choices
True x
False
Question 20 1 pts
Which one of these variables has an illegal name?
Group of answer choices
$my-Var x
$my_Var
$myVar
Question 21 1 pts
How do you create a cookie in PHP?
Group of answer choices
setcookie() x
makecookie()
createcookie
Question 22 1 pts
In PHP, the only way to output text is with echo.
Group of answer choices
True
False x
Question 23 1 pts
How do you create an array in PHP?
Group of answer choices
$cars = "Volvo", "BMW", "Toyota";
$cars = array("Volvo", "BMW", "Toyota"); x
$cars = array["Volvo", "BMW", "Toyota"];
Question 24 1 pts
The if statement is used to execute some code only if a specified condition is true
Group of answer choices
True x
False
Question 25 1 pts
Which operator is used to check if two values are equal and of same data type?
Group of answer choices
!=
==
=
=== x
Code Quiz Challenge
Question 26
In PHP, which operator is used for type comparison?
==
=== x
!=
<>
Question 27
Which PHP superglobal variables are available inside all scripts throughout the running script's lifecycle, without needing to do any special require or include?
$GLOBALS x
$_SERVER
$CLIENT
$SYSTEM
Question 28
What does the isset() function in PHP check?
Checks if a variable is an array
Checks if a variable has been set and is not NULL x
Checks if a variable contains number data type
Checks if a variable contains boolean data type
Question 29
$x = true and false; What is the value of the $x variable in a PHP script?
true
false
null x
0
Question 30
In PHP, superglobals are special variables that are always ...
Initialized at the start of a script
Declared in the global scope
Available in all scopes throughout a script x
Definable by the user
Question 31
What is the purpose of the 'isset()' function in PHP?
It determines whether a variable is set and is not NULL x
It sets a specific value for a variable
It checks if a variable is of a specific datatype
It deletes a variable from the memory
Question 32
What is the primary use of an abstract class in Object-Oriented Programming (OOP)?
It is used to create objects. x
It provides a way to group related code happily together.
It forces developers to follow a certain pattern or structure.
They can only contain static methods.
Question 33
Which of the following PHP superglobals are not request-specific?
$_GET
$_POST
$GLOBALS x
$_SESSION
Question 34
What happens when you declare a variable in PHP with specified type?
PHP automatically assigns the variable the default value of the declared type.
PHP throws an error because explicit type declaration isn't supported.
Nothing particular happens, it operates same as loose type language. x
PHP changes all previous declarations of said variable to match the new type.
Question 35
What will be the output of following code snippet in PHP ?
$x = 5;
$y = '5';
var_dump($x === $y);
int(1)
bool(true)
bool(false) x
int(0)
Question 36
In PHP, how would you create a loop that iterates over an array and displays each element of the array?
Using a buffer and concatenating each element to the buffer.
Using the `for` loop and an iteration variable incremented after each cycle.
`foreach($array as $value) { echo $value; }` x
`while($array) { echo array_shift($array); }`
Question 37
What will the output of this PHP script be?:
11
12 x
10
13
Question 38
What is the main purpose of the isset() function in PHP?
It checks if a variable is set and is not NULL x
It sets a specific value to a variable
It checks if a variable is set and is NULL
It destructs a particular variable
Question 39
n PHP language, which of the following global variables is used to get HTTP file upload variables?
$_POST x
$_GET
$_FILES
$_SERVER
Question 40
What does the $_SESSION['user'] superglobal accesses in a PHP script?
A globally-initialized variable named 'user'
Data stored in the session array for 'user' x
A constant named 'user'
A browser's user agent info
Question 41
Which of the following PHP superglobals are used to get information from a form that is transmitted using method="get"?
$_POST
$_GET x
$_REQUEST
$_SERVER
Question 42
Which of the following PHP math functions can be used to generate a random number?
rand() x
random_num()
mt_rand()
randomint()
Question 43
What is the use of 'self' keyword in PHP?
'Self' is used for referring to static members of the class
'Self' is used to create new instances of the class
'Self' is used to destroy instances of the class
'Self' is used to refer to the parent class x
Question 44
What is the primary role of the __construct() function in a PHP class?
It handles exception for a class
It initiates all attributes of a class x
It deletes a class object
It hides all data members of a class
Question 45
In PHP, how can you connect to a MySQL database using PDO (PHP Data Objects)?
`$db = new PDO('mysql:host=localhost;dbname=test', 'root', '');` x
`$db = new MySQL('localhost', 'root', '', 'test');`
`$db = mysqli_connect('localhost', 'user', 'password', 'database');`
`$db = new PDO(); $db->connect('localhost', 'root', '', 'test');`
Question 46
Which of the following PHP superglobal variables does not exist?
$_POST
$_SERVER
$_REQUEST
$_USER x
Question 47
In PHP, which of the following can be used to define a constant?
define() function x
constant keyword
$ sign
const keyword
Question 47
In PHP, what global variable stores information submitted by HTML forms?
$_GET
$_POST
$_GLOBALS x
$_FILES
Question 48
In PHP, what kind of variable is $_POST?
An array variable x
A string variable
A Boolean variable
A numerical variable
Question 49
In PHP, what does the $_POST superglobal variable do?
It can hold variables passed to the current script through the HTTP POST method. x
It stores session data across multiple pages.
It holds variables passed to the current script through the environment method.
It carries form data as part of the URL in an HTTP GET.
Question 50
In PHP, which of the following is the correct way to define a variable?
var $myVar = 'Hello World';
$myVar == 'Hello World';
$myVar := 'Hello World';
$myVar = 'Hello World'; x
Question 51
What does the ?> shorthand signify in PHP?
It signals the start of a PHP code block
It signals the end of a PHP code block x
It denotes a multi-line comment
It signifies a string value
Question 52
What is the correct way of declaring a global variable in a function in PHP?
global $var = 'value';
function functionName() { $GLOBALS['var'] = 'value'; } x
$GLOBALS var = 'value';
Both 'global $var = 'value';' and 'function functionName() { $GLOBALS['var'] = 'value'; }'
Question 53
In PHP, what is the purpose of the foreach loop?
It is used to continue execution of the current script at the next iteration of the outermost containing loop.
It provides an easy way to iterate over arrays. x
It is used to execute a block of code a specified number of times.
It is used to execute code if a condition is true.
Question 54
In PHP, which of the following are valid conditional operators?
== (Equal)
<= (Less than or Equal to) x
* (Multiply)
$= (Dollar equal)
Question 55
In PHP, performs a search for a match in a string, and replaces the matched text with the replacement text. Which function achieves this?
preg_replace()
str_replace() x
substr_replace()
preg_match()
Question 56
In PHP, what is the correct way to start a session?
session_star()
session_start
session.Start()
session_start() x
Question 57
In PHP, what is the SuperGlobal variable that holds information about headers, paths and script locations?
$_GET
$_POST
$_SERVER x
$_SESSION
Question 58
In PHP, which statement is used to include a specified file in the script?
include() x
import()
add()
incorporate()
Question 59
In PHP, what is the purpose of using a foreach loop structure?
To execute a block of code a single time.
To execute a block of code for every item in an array or object. x
To check if a certain condition is true or false.
To securely connect to a database.
Question 60
Which of the following superglobals in PHP do not contain user input data?
$_SERVER x
$_GET
$_POST
$_SESSION
Question 61
Which of the following PHP superglobals would you typically use to retrieve data from a form that was submitted using the GET method?
$_GET x
$_POST
$_SERVER
$_REQUEST
Question 62
Which PHP superglobal variable holds information about headers, paths, and script locations?
$GLOBAL
$_GET
$_SERVER x
$_POST
Question 63
What is the superglobal variable $_POST designed for in PHP?
To store session data
To collect form data after submitting an HTML form with method="post" x
To collect URL parameters
To manage cookies
Question 64
Which of the following is NOT a standard superglobal in PHP?
$_GET
$GLOBALS
$_POST
$_DATA x
Code Quiz Challenge
You answered 26 out of 39 questions correctly
260 XP
Question 65
Output "Hello World" if $a is greater than $b.
$a = 50;
$b = 10;
if ($a > $b) {
echo "Hello World";
}
Question 66
Output $i as long as $i is less than 6.
$i = 1;
while ($i < 6) {
echo $i;
$i++;
}
Question 67
Create a loop that runs from 0 to 9.
for ($i = 0; $i < 10; $i++) {
echo $i;
}
Question 68
Use the correct function to output the number of items in an array.
$fruits = array("Apple", "Banana", "Orange");
echo count($fruits);
Question 69
Create an associative array containing the age of Peter, Ben and Joe.
$age = array("Peter" => "35", "Ben" => "37", "Joe" => "43");
Question 70
Loop through an associative array and output the key and the value.
foreach($age as $x => $y) {
echo "Key=" . $x . ", Value=" . $y;
}
Question 71
Use the correct array method to sort the $colors array alphabetically.
$colors = array("red", "green", "blue", "yellow");
sort($colors);
Question 72
Use the correct array method to sort the $colors array descending alphabetically.
$colors = array("red", "green", "blue", "yellow");
rsort($colors);
Question 73
Use the correct array method to sort the $age array according to the values.
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
asort($age);
Question 74
If the form in the white section below gets submitted, how can you, in welcome.php, output the value from the "first name" field?
Welcome
Question 75
If the form in the white section below gets submitted, how can you, in welcome.php, output the value from the "first name" field?.
Hint: The http method is not the same as in Exercise 1.
Welcome
Question 76
Use the correct date function to output the weekday of today (monday, tuesday etc.).
echo date("l");
Question 77
Use the correct format parameter to output a date like this: 2024.03.01.
echo date("Y.m.d");
Question 78
Use the correct format parameter to output the time like this: 20:56:42 (with hour as a 24-hour format).
echo date("H:i:s");
Question 79
Assume we have a file named "webdict.txt", write the correct syntax to open and read the file content.
echo readfile("webdict.txt");
Question 80
Open a file, and write the correct syntax to output one character at the time, until end-of-file.
$myfile = fopen("webdict.txt", "r");
while(!feof($myfile)) {
echo fgetc($myfile);
Question 80
Create a session variable named "favcolor".
session_start();
$_SESSION["favcolor"] = "green";
Question 81
Output the value of the session variable "favcolor".
echo $_SESSION["favcolor"];
Question 82
Reverse the string "Hello World!".
echo strrev("Hello World!");
Question 83
In PHP, what will the output of the following be?
$foo = true;
if ($foo) {
echo 'YES';
} else {
echo 'NO'
}
'YES'
'NO'
'TRUE'
Error x
Question 84
What is the primary purpose of 'echo' in PHP?
It is used to output one or more strings. x
It is used to input one or more strings.
It is used to assign a value to a variable.
It is used as a looping statement.
Question 85
What does the private keyword in OOP PHP mean?
A private method or property can be accessed everywhere within the PHP script.
A private method or property can be only accessed within the class that defines it.
A private method or property can be accessed within the children of class that defines it. x
A private method or property can't be accessed anywhere in the PHP script.
Question 86
Which of the following is a valid way to declare an array in PHP?
$arr = array(1, 2, 3);
$arr[] = 1, 2, 3;
$arr = [1, 2, 3]; x
$arr -> (1, 2, 3);
Question 87
What function is used in PHP to get a value from an associative array using its key?
$array[key] x
get($array, key)
$array->get(key)
$array->key
Question 88
Which of the following statements are true about PHP global variables?
$_GET collects form-data sent with the HTTP GET method x
$_POST is an associative array of variables passed to the current script via the HTTP POST method
Global variables cannot be accessed from inside a function
$_COOKIE is used to store user data within a session
Question 89
Which of the following PHP conditions would be useful to verify if a variable is an integer or a float?
is_float() or is_integer() x
is_int() or is_double()
is_var() or is_number()
is_digital() or is_flt()
Question 90
Which of the following variable type in PHP can hold a text string?
boolean
string x
integer
object
Question 91
In PHP, which variable types are considered 'compound'?
Integer and Float
Array and Object x
Boolean and String
Resource and NULL
Question 92
What is the function in PHP to check if a variable is a number?
is_numeric x
is_number
check_numeric
is_integer
Question 93
In PHP, which one of the following functions would be appropriate to add an element to the beginning of an array?
array_unshift()
array_shift()
array_push() x
array_pop()
In PHP, which of these options is a superglobal array?
$_GET x
$_POST x
$server x
$session x
Question 94
In PHP, which of the following are considered 'superglobal' variables?
$_GET x
$_POST
$_result x
$myVar
Question 95
In PHP, which of the following operators are used for comparison?
== (Equal) x
/= (Not Equal) x
=== (Identical) x
<= (Less than or equal to) x
Question 96
Which of the following operations in PHP is used to declare variables?
$variable = value; x
var variable = value;
int variable = value;
$variable = new Value;
Question 97
Which of the following are valid PHP variable names?
$var1 x
1$var
$_var x
$VAR x
Question 98
In PHP, which of the following are considered as loosely typed data types?
Integer
Float
String
None of the above. x
Question 99
In PHP scripting, which of these are superglobals?
$_GET x
$_POST x
$myGlobalVar
$_FILE
Question 100
In PHP, which of the following are numeric data types?
Integer x
Float x
String
Char
Question 101
In PHP, which syntax correctly defines an associative array?
`$my_array = array('key1'=>'value1', 'key2'=>'value2');`
`$my_array = ['key1'->'value1', 'key2'->'value2'];`
`$my_array = ('key1'=>'value1', 'key2'=>'value2');` x
`$my_array = array:'key1'=>'value1', 'key2'=>'value2';`
Question 102
What are the basic rules for declaring a variable in PHP?
Variable names can begin with a number
Variable names are case sensitive x
Variable names must start with a letter or underscore x
Variable names can only contain numbers and letters
Question 103
What is the output of the following PHP code?
'5'
'10'
NULL
Error x
Question 104
In PHP, what is the correct way to initialize a string variable?
$variable = 'Hello, World'; x
string $variable = 'Hello, World';
$variable := 'Hello, World';
$variable == 'Hello, World';
Question 105
var salary = 25000f; The above line of code, throws a syntax error. Which of the following is a legitimate reason for PHP's refusal?
PHP doesn't support prefix `var` with variable declaration.
`salary` is a reserved word in PHP.
'f' at the end is not permitted in PHP for float type value assignment. x
Semicolon is missing at the end.
Question 106
Which of the following PHP functions can be used to merge arrays?
isset()
array_combine()
array_merge() x
implode()
Question 107
In PHP, which of the following are considered scalar data types?
Integer
Boolean
Array x
Object
Question 108
Which of the following are valid native PHP data types?
String x
Char
Float x
Hexadecimal
Question 109
What is the correct way to concatenate two strings in PHP?
$a + $b
$a . $b x
concat($a, $b)
$a & $b
Question 110
What is the main advantage of using prepare statements in MySQL with PHP?
Improves performance
Automatically connects to the database
Prevents SQL injection attacks x
Automatically closes the database connection
Question 111
What does the $_POST superglobal do in PHP?
It contains information from forms submitted with the POST method. x
It contains all global variables of the server.
It carries text data embedded in the body of HTTP requests. x
It withdraws data from MySQL database related to POST transactions.
Question 112
In PHP, which of the following is a supported database type in the DBMS?
SQLite3
MongoDB
Oracle Database
Microsoft SQL Server
Question 113
In PHP, which operator is used for object instantiation?
->
:: x
new
this
Question 114
In PHP, which operator is used for object property access?
->
.
:: x
\
Question 115
In PHP, what does the preg_match() function do?
Performs a Regular Expression match test. x
Deletes all matches of a pattern within a string.
Replaces all occurrences of a string in another string.
Parses a CSV string into an array.
Question 116
Output $i as long as $i is less than 6.
$i = 1;
do {
echo $i;
$i++;
} while ($i < 6);