NEW
Font size
WorksheetsPHP Functions Quiz
Total questions: 40
Worksheet time: 20mins
What keyword is used to define a user-defined function in PHP?
def
function
define
func
Which of the following is a valid reason to use functions in PHP?
To increase the number of lines of code
To minimize the number of source-code
To make the code more difficult to read
To prevent the use of variables
What is the main purpose of the return statement in a function?
To stop the PHP script entirely
To output a value directly to the browser
To send a value back to the calling code
To declare a new variable
Which pre-defined function is used to include a file and produce a fatal error if the file is not found?
include()
insert()
require()
attach()
The date() function is used to:
Calculate the difference between two dates
Create and format a date and/or time
Convert a date into a timestamp only
Set the server's default timezone
Which function is used to start a PHP session?
session_begin()
start_session()
session_start()
$_SESSION()
Which function is used to calculate the square root of a number?
pow()
sqrt()
exp()
hypot()
What is the purpose of the strtoupper() function?
Converts a string to lowercase letters
Converts the first character of a string to uppercase
Converts a string to uppercase letters
Converts the first character of each word to uppercase
Which function returns the absolute (positive) value of a number?
ceil()
floor()
round()
abs()
The explode() function is used to:
Combine strings with a delimiter
Break a string into an array
Calculate the exponent of e
Find the position of a character in a string
Which function would you use to generate a random integer?
mt_rand()
getrandmax()
rand()
Both a and c are correct
The number_format() function is primarily used to:
Convert a number to a currency string
Format a number with grouped thousands
Calculate the remainder of a division
Round a number up to the nearest integer
Which function is used to calculate the hyperbolic cosine of a number?
cos()
acos()
cosh()
acosh()
The str_pad() function is used to:
Remove padding from a string
Split a string into an array of chunks
Pad a string to a new length
Compare two strings with padding
What does the md5() function calculate?
The SHA-1 hash of a string
The square root of a number
The MD5 hash of a string
The natural logarithm of a number
Which function converts characters to HTML entities?
htmlspecialchars()
html_entity_decode()
htmlentities()
strip_tags()
The ceil() function always:
Rounds a number down to the nearest integer
Rounds a floating-point number
Rounds a number up to the nearest integer
Returns the absolute value of a number
What is the purpose of the strlen() function?
Count the number of words in a string
Return the length of a string
Find the position of a character in a string
Reverse a string
Which function converts a string to lowercase letters?
lcfirst()
strtolower()
strtoLower()
strtolowercase()
The deg2rad() function is used to:
Convert a radian value to a degree value
Calculate the degree of an angle in a triangle
Convert a degree value to a radian value
Convert a number from degrees to Fahrenheit
What is a PHP cookie?
A variable that is stored on the server indefinitely
A small file that the server embeds on the user's computer
A function used to encrypt user data
A type of PHP session
Which superglobal variable is used to access a cookie value?
$_GET
$_POST
$_SESSION
$_COOKIE
What is the primary purpose of PHP state management?
To make the website run faster
To store site or user information to remember users
To connect to a MySQL database
To validate HTML forms
Which function must be called to start a session before using $_SESSION variables?
start_session()
session_begin()
session_start()
init_session()
Where is session data stored?
In a database on the server
In a file on the user's computer
In a variable on the server
In the URL of the web page
Which function is used to completely destroy all session data?
session_delete()
session_remove()
session_unset()
session_destroy()
You want to set a cookie named "theme" with a value of "dark" that expires in 30 minutes. Which line of code is correct?
setcookie("theme", "dark", time() + 60*30);
setcookie("theme", "dark", 30);
$_COOKIE["theme"] = "dark", time() + 1800;
create_cookie("theme", "dark", 1800);
How do you correctly assign a value to a session variable named user_id?
$user_id = $_SESSION(123);
$_SESSION['user_id'] = 123;
session('user_id', 123);
$SESSION->user_id = 123;
A cookie was set with setcookie("username", "john_doe");. How would you access its value on another page?
$username = getcookie("username");
$username = $_REQUEST["username"];
$username = $_COOKIE["username"];
$username = $cookie_username;
What is the main difference between include() and require() when including files?
require() includes HTML files, include() includes PHP files.
require() produces a fatal error and stops the script if the file is missing, while include() produces a warning.
include() produces a fatal error and stops the script if the file is missing, while require() produces a warning.
There is no difference; they are aliases of the same function.
You need to ensure a cookie is valid for all directories on your domain. What value should the 'path' parameter have in setcookie()?
""
"./"
"/"
"*"
What will happen if you try to set a cookie after sending HTML output to the browser?
It will work perfectly fine.
It will cause a warning, and the cookie may not be set.
The browser will ignore the HTML and only set the cookie.
The PHP script will automatically restart.
You want to delete a cookie named "visit_count". How can this be achieved?
delete_cookie("visit_count");
setcookie("visit_count", "", time() - 3600);
unset($_COOKIE['visit_count']);
$_COOKIE['visit_count'] = null;
Which function is used to free all session variables without destroying the session itself?
session_destroy()
session_unset()
session_free()
unset($_SESSION);
To make a session variable available on a different page, what must you do at the top of that new page?
Redeclare the variable with the global keyword.
Call session_start() again.
Include the file where the variable was first set.
Nothing; session variables are global and automatic.
What is the correct syntax for the setcookie() function?
setcookie(name, value, expire, path, domain, security);
set_cookie(name, value, expire, path, domain, secure);
create_cookie(value, name, expire, path, domain);
setcookie(value, name, expire, path, domain, security);
You are building a login system. After verifying a user's credentials, you need to store their user ID in a way that persists across multiple pages but is more secure than a cookie. Which combination of PHP features is most appropriate?
Use setcookie() to store the user ID.
Store the user ID in a global variable.
Store the user ID in a $_SESSION variable after calling session_start().
Append the user ID to the URL of every link.
Analyze the following scenario: A script uses session_start(), sets a session variable, and then calls session_destroy() but does not call session_unset(). What is the state of the session variable immediately after session_destroy() is called?
The variable is still accessible on the current page because session_unset() was not called.
The variable is unset and the session is destroyed, so it is no longer accessible.
The variable is stored permanently until the browser is closed.
The variable is only destroyed if the script ends.
You need to create a "Remember Me" feature for a login system. The user's login state should persist for two weeks even if they close the browser. What is the best implementation?
Store the user's ID in a session variable.
Store the user's ID in a cookie with an expiration time set to time() + (14 * 24 * 3600).
Store the user's ID in a database and query it on every page load.
Use JavaScript local storage to store the user's ID.
A developer wants to restrict a cookie so it is only sent over a secure HTTPS connection. What should the value of the 'security' parameter be in the setcookie() function?
0
1
true
"https"
