All PHP Interview Questions & Answers

All PHP Interview Questions & Answers list

Here, you can find PHP Interview Questions & Answers related to Arrays, Operators, Loops, Variables, Conditional Statements, Strings, Form, MySQL, Cookies, and Sessions. These popular & frequently asked interview questions are listed under different categories.

Tutorials Class covers Interview Questions for Freshers as well as Experienced professionals. We not only list core PHP but also provide Advanced PHP interview questions for students.

What is include() function in PHP?

The include() takes all the content in a specified file and includes it in the current file.
If an error occurs, the include() function generates a warning, but the script will continue execution.


What are the different types of error in PHP?

There are four basic types of run-time errors in PHP:

  1. Notices
  2. Warnings
  3. Fatal Error
  4. Parse Errors:

For more detail, please check PHP Errors


How to define a Constant in PHP?

Constant can be declared using define() function in PHP. To retrieve the value of a constant, we can specify its name.

Note: Unlike with variables, we do not need to use doller ($) to output constant value.


How will you concatenate two strings in PHP?

To concatenate two string variables together, use the dot (.) operator.


PHP’s numerically indexed array begin with which position?

PHP Indexed array begins with position 0.
For example if: $array=array(15,80,67,33); Then these elements will have following positions:
$array[0]=10 (This is beginning position 0 not 1)
$array[1]=89
$array[2]=34
$array[3]=67


What is the difference between single quote and double quote string in PHP?

PHP single quotes execute slight faster than double quotes but single quote does not parse variables. Here is the difference between single quote and double quote string in PHP:

Single quotes

  1. The simplest method to declare a string is using single quotes. They are faster because everything written inside single quotes is treated as plain string.
  2. This is useful when we need to output exactly as it is written in single quotes. If we place a variable inside single quotes, it will output as a same variable name but not its value.
  3. Using single quoted string, PHP will not evaluate most escaped characters except single quote with backslash (\’). For an example, If we want to output single quoted word inside single quoted string: echo ‘This is \’sample\’ string’;

Double quote

  1. It will parse various escaped characters, regular expressions, and variables in the strings.
  2. We can also use curly braces to wrap variables if additional word needs to attach with variable value output. For example if we have variable name $number (with its value 100) and you what to echo “The 100s kids are there”. You can wrap variable with curly braces such as: “The {$number}s kids are there”.
  3. Because of parsing escaped characters & variables, this is slower than single quoted string.

Note: If there is no variable or escaped characters in string, use single quote else double quote.


Which PHP function inserts an element to the end of an array?

PHP array_push() function is used to insert one or more elements to the end of an array.


Why we use PHP String functions trim(), ltrim() and rtrim()?

PHP trim related functions are used to remove white spaces or other given characters from the beginning and end of the strings.

  • trim() removes space & predefined characters from both sides of given string.
  • ltrim() removes space & predefined characters from the left side of the sting.
  • rtrim() removes space & predefined characters from the right side of the sting.

These functions are helpful when we want to clean strings from unwanted spaces or characters.

Syntax: trim(string,charlist)

Example:


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

PHP is_array() 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.

Output: 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.