PHP Functions Interview Questions & Answers

PHP function interview questions: what a function is, pass by reference, count parameters, floor(), header(), constant(), str_replace(), ucwords() and explode() vs implode().

How to check if a variable is empty in PHP?

Use the empty() function. It returns true when the variable does not exist or has a false-like value, and false otherwise.

<?php
$a = "";
$b = "0";
$c = "Hello";

var_dump(empty($a));
var_dump(empty($b));
var_dump(empty($c));
?>

Output:

Tutorials Class - Output Window

bool(true)
bool(true)
bool(false)

These values are all empty: "", 0, 0.0, "0", null, false, [] and an undefined variable. The string “0” is a surprise for many people, so do not use empty() when 0 is a valid value.

Read the full chapter PHP empty() and the comparison isset() vs empty() vs is_null().

What is PHP Function?

A function is a block of statements that does one specific job. It can take values as input (parameters), do some work and give back a result. You write it once and call it whenever you need.

<?php
function add($a, $b) {
    return $a + $b;
}

echo add(5, 3);
?>

Output:

Tutorials Class - Output Window

8

PHP has many built-in functions (like strlen()) and you can also create your own with the function keyword. Learn more in PHP Functions.

Which PHP function can be used check if variable is an array?

Use the is_array() function. It returns true if the variable is an array and false if it is not.

<?php
var_dump(is_array([1, 2, 3]));
var_dump(is_array("Hello"));
var_dump(is_array(null));
?>

Output:

Tutorials Class - Output Window

bool(true)
bool(false)
bool(false)

There are similar functions for other types, like is_string(), is_int() and is_bool(). See how to check the data type of a variable.

What PHP Function is used to find position of a string?

Use the strpos() function. It returns the position of the first occurrence of a text inside a string, or false if the text is not found. The counting starts from 0.

<?php
echo strpos("I like apple and banana", "apple");
?>

Output:

Tutorials Class - Output Window

7

The word “apple” starts at position 7, because “I like ” has 7 characters (0 to 6). Because the result can be 0 for a match at the start, always check with !== false. Learn more in PHP strpos() Function and how to check if a string contains a substring.

What is the use of ucwords() PHP String function?

ucwords() changes the first letter of each word of a string to a capital letter. The rest of the letters are not changed.

<?php
echo ucwords("hello world from php") . "<br>";

// Different word separator
echo ucwords("hello-world foo", "-") . "<br>";

// Make the other letters small first
echo ucwords(strtolower("PHP TUTORIAL"));
?>

Output:

Tutorials Class - Output Window

Hello World From Php
Hello-World foo
Php Tutorial

By default words are separated by a space, tab or new line. Use the second parameter to give your own separators. To change only the first letter of the whole string use ucfirst().

What is the use of str_replace() PHP String function?

str_replace() searches a string for some text and replaces it with new text. It replaces all matches.

<?php
echo str_replace("Hello", "Hi", "Hello World, Hello PHP");
?>

Output:

Tutorials Class - Output Window

Hi World, Hi PHP

It is case-sensitive. For a case-insensitive replace use str_ireplace(). See more examples in how to replace a part of a string in PHP.

Can we include a file two times in any PHP page?

Yes, you can use include (or require) for the same file many times. PHP will add its code every time.

But there is a problem if that file declares a function or a class. The second include tries to create the same function again, and PHP stops with a fatal error like “Cannot redeclare function”.

<?php
include "helpers.php";
include "helpers.php";        // error if helpers.php has functions

include_once "helpers.php";   // safe: adds the file only one time
?>

Use include_once or require_once for files with functions, classes or settings. They check if the file was already added. See PHP File Inclusion.

Which is the PHP function to delete a file from server?

Use the unlink() function. It deletes a file from the server and returns true on success. It is better to check first if the file exists.

<?php
$file = "sample-file.txt";

if (file_exists($file) && unlink($file)) {
    echo "File deleted";
} else {
    echo "File not found or could not be deleted";
}
?>

Output:

Tutorials Class - Output Window

File deleted

  • The deleted file can not be taken back.
  • PHP needs permission to delete the file, otherwise you get a warning.
  • Never pass a file name from user input without checking it.
  • Do not mix it with unset(), see unlink() vs unset().

What is PHP floor() function?

The floor() function rounds a number down to the next lower whole number. It always returns a float.

<?php
echo floor(7.8) . "<br>";
echo floor(-3.2) . "<br>";
var_dump(floor(5.9));
?>

Output:

Tutorials Class - Output Window

7
-4
float(5)

Notice that floor(-3.2) is -4, because -4 is lower than -3.2. The related functions are ceil() (round up) and round() (round to the nearest). Use intdiv() for whole number division.

How to count the number of parameters passed in a PHP function?

Use func_num_args() inside the function. It returns how many arguments were passed. func_get_args() returns them in an array.

<?php
function total() {
    return func_num_args();
}

echo total(10, 20, 30);
?>

Output:

Tutorials Class - Output Window

3

In modern PHP the clearer way is a variadic parameter (...$args), and then count($args).

<?php
function total(...$args) {
    return count($args);
}

echo total(10, 20, 30);
?>

Output:

Tutorials Class - Output Window

3

Learn more in PHP Functions.

How can you pass a variable by reference in PHP?

Write an ampersand (&) before the parameter in the function. Then the function works on the original variable, not on a copy.

Normally PHP passes arguments by value. If the function changes the value, the variable outside does not change. With a reference it does.

<?php
function add_line(&$line) {
    $line .= " and extra words added.";
}

$sample = "This is a sample line,";
add_line($sample);

echo $sample;
?>

Output:

Tutorials Class - Output Window

This is a sample line, and extra words added.

Without the &, the output would be only “This is a sample line,”. Objects are different: PHP passes the handle of an object, so a function can change its properties without any ampersand.

Note: Use references only when you really need them, because they make the code harder to follow. Often it is cleaner to return the new value from the function. See PHP Functions.

How to Get the Current Year using PHP?

Use the built-in date() function with the format character Y. It gives the current year with 4 digits.

<?php
echo date("Y");
?>

Output:

Output in the year 2026

2026

The most common use is the copyright line in the footer, so you do not have to change it every year.

<footer>
    &copy; <?php echo date("Y"); ?> Tutorials Class
</footer>

The year comes from the server time and its time zone. Near midnight of 31 December it can be different for some visitors, see how to change the time zone. The same with the DateTime class: (new DateTime())->format("Y"). More in PHP Date and Time.

What is the constant() function in PHP?

The constant() function returns the value of a constant when you have the name of the constant as a string. It is useful when the constant name is stored in a variable or created at run time.

<?php
define("SITE_NAME", "Tutorials Class");

class App {
    const VERSION = "2.0";
}

echo constant("SITE_NAME") . "<br>";

$name = "SITE_NAME";
echo constant($name) . "<br>";

echo constant("App::VERSION");
?>

Output:

Tutorials Class - Output Window

Tutorials Class
Tutorials Class
2.0

If the constant does not exist, PHP 8 throws an Error. Check it first with defined(). Read PHP Constants for the basics.

What is the use of the header() function in PHP?

The header() function sends a raw HTTP header to the browser, before the page content. With headers you can give instructions to the browser.

Common uses of header():

  • Redirect the user to another page (Location header).
  • Tell the type of response, like JSON (Content-Type header).
  • Force a file download (Content-Disposition header).
  • Control browser cache (Cache-Control header).
  • Set the HTTP status code, for example 404 or 301.
<?php
header("Content-Type: application/json");

echo json_encode(["status" => "ok"]);
?>

Output:

Tutorials Class - Output Window

{“status”:”ok”}

Note: header() must be called before any output, even a blank space. Otherwise PHP shows the “headers already sent” warning. Full details and more examples are in PHP header() Function.

What is the difference between explode() and implode() in PHP?

explode() breaks a string into an array. implode() joins the elements of an array into a string. They do opposite work.

explode()implode()
String to arrayArray to string
explode(separator, string)implode(separator, array)
Splits at every separatorPuts the separator between elements
<?php
$text = "apple,banana,cherry";

$fruits = explode(",", $text);
print_r($fruits);

echo implode(" - ", $fruits);
?>

Output:

Tutorials Class - Output Window

Array
(
[0] => apple
[1] => banana
[2] => cherry
)
apple – banana – cherry

Learn more in PHP explode() Function and PHP implode() Function.