PHP Functions Interview Questions & Answers

PHP Functions Interview Questions & Answers list

Here, you can read about PHP Function related Interview Questions & Answers. This list contains popular & frequently asked interview questions and answers related to PHP Functions. Find PHP Functions FAQs for Job Preparation for freshers and experienced level.

How to check if a variable is empty in PHP?

The empty() function is an in-built function in PHP which is used to check whether a variable is empty or not.

Example of empty function:

<?php
$var1 = "";
if(empty($var1))
{
echo"Yes, variable is empty";
}
else
{
echo"No, variable is not empty";
}
?>

Here is the list of values, that are returned as an empty

  • “” (an empty string)
  • 0(as an integer)
  • 0.0(o as a float)
  • “0”(o as a string)
  • Null
  • False
  • array() (an empty array)

What is PHP Function?

PHP Function is a set of statements that perform a specific task. A function usually takes input arguments, perform a specific action on them and returns the result.


Example of function in PHP:

<?php
function msg() { //function function name
    echo "Hello world!";
}

msg(); //call to function
?>

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

PHP is_array() the function is used to check if a variable is an array or not. This is_array() function will return true if a variable is an array or false if it is not.

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

PHP strpos() function is used to find the first occurrence of substring in the given string.

<?php
echo strpos("The color of apple is red.","apple");
?>
Tutorials Class - Output Window

13

The word 'apple' is found at the position '13'. Remember that the position of string/character start at 0, and not 1. This function is very useful to find certain patterns or words in a string.

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

PHP ucwords() function is used to capitalized the first letter of each word in String.

<?php
echo ucwords("welcome to the php tutorials");
?>

Run : http://ideone.com/83vvyF

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

The PHP str_replace() function is used to search & replace some words in a strings.

Example

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

In this example, "Hello" word will be replaced by "Hi" in the given string.

Run : http://ideone.com/AFs0DW

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

Yes, we can use include("sample.php")

function more than once in any PHP page. It will include code from 'sample.php' file multiple times.

Note: Note: If ‘sample.php’ file contains some functions declaration, there will be an error. Why? Because it will try to separate function with the same name again and it will throw an error.

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

PHP has lots of inbuilt Functions. One of them is the unlink() PHP function which is used to delete a file from server. Example: unlink("sample-file.php")

What is PHP floor() function?

With the help of the floor() function, we can convert a numeric value to it’s next lower integer value.


Example of floor() function in PHP:

<?php
$a=-145.13;
$result=floor($a);
echo "Floor value of $a is $result";    // Output: Floor value of -145.13 is -145
?>

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

With the help of func_num_args() function, we can count the number of parameters passed into a PHP function.

Example of function func_num_args()

<?php
function count_param()
{
    $count_args = func_num_args();
    echo "Number of parameters: $count_args\n";
}
 
count_param(10, 20);  // Output: Number of parameters: 2
 
?>

How can you pass a variable by reference in PHP?

By using an ampersand (&) sign in-front of the variable, we can pass the variable by reference.

Why or when to use variable by reference ?

When PHP function arguments are passed by value, and value is changed within function, it does not change variable value outside the function. To allow a function to modify its argument value outside the function, they must be passed by reference.

Example:

<?php
function add_line(&$line)
{
    $line.= 'and extra words added.';
}
 
$sample_line= 'This is a sample line, ';
add_line($sample_line);
echo $sample_line;    // outputs 'This is a sample line,  and extra words added.'
?>

If we do not use value by reference (&), then the output will be: ‘This is a sample line,’

How to Get the Current Year using PHP?

You can easily get current year and time using PHP built-in date() function.

<?php  
// Get current year in PHP
$current_year = date("Y");
echo $current_year;
?> 

This most common use of displaying the current year (dynamically) is inside the copyright message. For example, you must have seen in website footer: Copyright 2020 – Website brand-name.