wayground logo

Free Printable Worksheets

NEW

Font size

S
M
L
XL
Worksheets

PHP Functions Quiz

Total questions: 40

Worksheet time: 20mins

Name
Class
Date
1.

What keyword is used to define a user-defined function in PHP?

a)

def

b)

function

c)

define

d)

func

2.

Which of the following is a valid reason to use functions in PHP?

a)

To increase the number of lines of code

b)

To minimize the number of source-code

c)

To make the code more difficult to read

d)

To prevent the use of variables

3.

What is the main purpose of the return statement in a function?

a)

To stop the PHP script entirely

b)

To output a value directly to the browser

c)

To send a value back to the calling code

d)

To declare a new variable

4.

Which pre-defined function is used to include a file and produce a fatal error if the file is not found?

a)

include()

b)

insert()

c)

require()

d)

attach()

5.

The date() function is used to:

a)

Calculate the difference between two dates

b)

Create and format a date and/or time

c)

Convert a date into a timestamp only

d)

Set the server's default timezone

6.

Which function is used to start a PHP session?

a)

session_begin()

b)

start_session()

c)

session_start()

d)

$_SESSION()

7.

Which function is used to calculate the square root of a number?

a)

pow()

b)

sqrt()

c)

exp()

d)

hypot()

8.

What is the purpose of the strtoupper() function?

a)

Converts a string to lowercase letters

b)

Converts the first character of a string to uppercase

c)

Converts a string to uppercase letters

d)

Converts the first character of each word to uppercase

9.

Which function returns the absolute (positive) value of a number?

a)

ceil()

b)

floor()

c)

round()

d)

abs()

10.

The explode() function is used to:

a)

Combine strings with a delimiter

b)

Break a string into an array

c)

Calculate the exponent of e

d)

Find the position of a character in a string

11.

Which function would you use to generate a random integer?

a)

mt_rand()

b)

getrandmax()

c)

rand()

d)

Both a and c are correct

12.

The number_format() function is primarily used to:

a)

Convert a number to a currency string

b)

Format a number with grouped thousands

c)

Calculate the remainder of a division

d)

Round a number up to the nearest integer

13.

Which function is used to calculate the hyperbolic cosine of a number?

a)

cos()

b)

acos()

c)

cosh()

d)

acosh()

14.

The str_pad() function is used to:

a)

Remove padding from a string

b)

Split a string into an array of chunks

c)

Pad a string to a new length

d)

Compare two strings with padding

15.

What does the md5() function calculate?

a)

The SHA-1 hash of a string

b)

The square root of a number

c)

The MD5 hash of a string

d)

The natural logarithm of a number

16.

Which function converts characters to HTML entities?

a)

htmlspecialchars()

b)

html_entity_decode()

c)

htmlentities()

d)

strip_tags()

17.

The ceil() function always:

a)

Rounds a number down to the nearest integer

b)

Rounds a floating-point number

c)

Rounds a number up to the nearest integer

d)

Returns the absolute value of a number

18.

What is the purpose of the strlen() function?

a)

Count the number of words in a string

b)

Return the length of a string

c)

Find the position of a character in a string

d)

Reverse a string

19.

Which function converts a string to lowercase letters?

a)

lcfirst()

b)

strtolower()

c)

strtoLower()

d)

strtolowercase()

20.

The deg2rad() function is used to:

a)

Convert a radian value to a degree value

b)

Calculate the degree of an angle in a triangle

c)

Convert a degree value to a radian value

d)

Convert a number from degrees to Fahrenheit

21.

What is a PHP cookie?

a)

A variable that is stored on the server indefinitely

b)

A small file that the server embeds on the user's computer

c)

A function used to encrypt user data

d)

A type of PHP session

22.

Which superglobal variable is used to access a cookie value?

a)

$_GET

b)

$_POST

c)

$_SESSION

d)

$_COOKIE

23.

What is the primary purpose of PHP state management?

a)

To make the website run faster

b)

To store site or user information to remember users

c)

To connect to a MySQL database

d)

To validate HTML forms

24.

Which function must be called to start a session before using $_SESSION variables?

a)

start_session()

b)

session_begin()

c)

session_start()

d)

init_session()

25.

Where is session data stored?

a)

In a database on the server

b)

In a file on the user's computer

c)

In a variable on the server

d)

In the URL of the web page

26.

Which function is used to completely destroy all session data?

a)

session_delete()

b)

session_remove()

c)

session_unset()

d)

session_destroy()

27.

You want to set a cookie named "theme" with a value of "dark" that expires in 30 minutes. Which line of code is correct?

a)

setcookie("theme", "dark", time() + 60*30);

b)

setcookie("theme", "dark", 30);

c)

$_COOKIE["theme"] = "dark", time() + 1800;

d)

create_cookie("theme", "dark", 1800);

28.

How do you correctly assign a value to a session variable named user_id?

a)

$user_id = $_SESSION(123);

b)

$_SESSION['user_id'] = 123;

c)

session('user_id', 123);

d)

$SESSION->user_id = 123;

29.

A cookie was set with setcookie("username", "john_doe");. How would you access its value on another page?

a)

$username = getcookie("username");

b)

$username = $_REQUEST["username"];

c)

$username = $_COOKIE["username"];

d)

$username = $cookie_username;

30.

What is the main difference between include() and require() when including files?

a)

require() includes HTML files, include() includes PHP files.

b)

require() produces a fatal error and stops the script if the file is missing, while include() produces a warning.

c)

include() produces a fatal error and stops the script if the file is missing, while require() produces a warning.

d)

There is no difference; they are aliases of the same function.

31.

You need to ensure a cookie is valid for all directories on your domain. What value should the 'path' parameter have in setcookie()?

a)

""

b)

"./"

c)

"/"

d)

"*"

32.

What will happen if you try to set a cookie after sending HTML output to the browser?

a)

It will work perfectly fine.

b)

It will cause a warning, and the cookie may not be set.

c)

The browser will ignore the HTML and only set the cookie.

d)

The PHP script will automatically restart.

33.

You want to delete a cookie named "visit_count". How can this be achieved?

a)

delete_cookie("visit_count");

b)

setcookie("visit_count", "", time() - 3600);

c)

unset($_COOKIE['visit_count']);

d)

$_COOKIE['visit_count'] = null;

34.

Which function is used to free all session variables without destroying the session itself?

a)

session_destroy()

b)

session_unset()

c)

session_free()

d)

unset($_SESSION);

35.

To make a session variable available on a different page, what must you do at the top of that new page?

a)

Redeclare the variable with the global keyword.

b)

Call session_start() again.

c)

Include the file where the variable was first set.

d)

Nothing; session variables are global and automatic.

36.

What is the correct syntax for the setcookie() function?

a)

setcookie(name, value, expire, path, domain, security);

b)

set_cookie(name, value, expire, path, domain, secure);

c)

create_cookie(value, name, expire, path, domain);

d)

setcookie(value, name, expire, path, domain, security);

37.

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?

a)

Use setcookie() to store the user ID.

b)

Store the user ID in a global variable.

c)

Store the user ID in a $_SESSION variable after calling session_start().

d)

Append the user ID to the URL of every link.

38.

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?

a)

The variable is still accessible on the current page because session_unset() was not called.

b)

The variable is unset and the session is destroyed, so it is no longer accessible.

c)

The variable is stored permanently until the browser is closed.

d)

The variable is only destroyed if the script ends.

39.

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?

a)

Store the user's ID in a session variable.

b)

Store the user's ID in a cookie with an expiration time set to time() + (14 * 24 * 3600).

c)

Store the user's ID in a database and query it on every page load.

d)

Use JavaScript local storage to store the user's ID.

40.

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?

a)

0

b)

1

c)

true

d)

"https"