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.
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?
PHP stands for: Hypertext Preprocessor. PHP
is the most popular open-source programming language for web development. It is a server-side language that collects form data, sends or retrieves cookies, interact with the database, and much more. With the help of PHP
, we can create static as well as dynamic websites.
Who is the father of PHP?
Rasmus Lerdorf is the father of the PHP who created PHP language in 1994.
Initially, PHP was written using a simple set of CGI (Common Gateway Interface) binaries written in the C language. Rasmus Lerdorf has worked on initial development with Andi Gutmans and Zeev Suraski.
What is the correct & common way to start and finish a PHP block of code?
The two most common ways to start and finish a PHP script are:
- Using standard tags:
<?php PHP Code here ?>
- Using short tags:
<? PHP Code here ?>
Example 1: Using standard tags:
This is the recommended way.
<?php
echo "I love PHP";
?>
Example 2: Using short tags:
This method is not supported by all server (depending on the PHP configuration), so always use first method.
<?
echo "I love PHP";
?>
What is Open Source Software?
Open-source software is one type of software on which the original source code is made freely available to the public. This software can also be redistributed and modified by the public.
Examples:
PHP, MySQL, and WordPress are a few examples of popular Open Source Software.
Is PHP a case sensitive language?
In PHP, variable names are case-sensitive but function names are not case sensitive.
Explanation:
- If you defined variable in lowercase, then you need to use it in lowercase everywhere in the program. If we define a variable
$name = "James";
then we must need to use $name. $NAME will not work
- If you defined function name in lowercase, but calling them in uppercase it will work. For example, If we define
function sum() {}
then calling SUM() will also work.
What is the default extension of the PHP file?
.php
is the default extension of PHP file.
What are the different data types in PHP?
PHP data type is an attribute that tells about the type of data needs to be stored. There are 8 data types in PHP:
- Integers: are whole numbers, without a decimal point, like 2567
- Doubles: are floating-point numbers, like 1.5 or 134.153
- Booleans: have only two possible values either true or false
- NULL: is a special type that only has one value: NULL
- Strings: is a sequence of characters, like “Hello PHP!”
- Arrays: stores multiple values in one single variable
- Objects: instances of user-defined classes
- Resources: are special variables that hold references to resources such as database calls
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
?>
What are the form method Attribute available for submitting?
The method attribute specifies how to send form-data. "GET"
and "POST"
are two methods to send form data to another page.
<form action="welcome.php" method="post">
// form elements will be placed here
</form>
<form action="welcome.php" method="get">
// form elements will be placed here
</form>
What are the different PHP functions to sort an array?
- asort()
- arsort()
- ksort()
- krsort()
- uksort()
- sort()
- nasort()
- rsort()
For more details: http://tutorialsclass.com/learn/php/php-array-functions
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:
- Notices
- Warnings
- Fatal Error
- 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.
Constants are used instead of variables where its value is rarely changed. For an example, Tax rate is almost constant for many years. So we can define it in a constant rather than variable.
<?php
define("TAX", "14");
echo TAX;
?>
How will you concatenate two strings in PHP?
To concatenate two string variables together, use the dot (.)
operator.
PHP’s numerically indexed array begins 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 slightly faster than double quotes but a single quote does not parse variables. Here is the difference between a single quote and double quote string in PHP:
Single quotes
- The simplest method to declare a string is using single quotes. They are faster because everything written inside single quotes is treated as a plain string.
- 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 the same variable name but not its value.
- Using single-quoted string, PHP will not evaluate most escaped characters except a single quote with a backslash
(\')
. For example, If we want to output single quoted word inside single-quoted string:echo 'This is \' sample\' string';
Double quote
- It will parse various escaped characters, regular expressions, and variables in the strings.
- 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"
. - Because of parsing escaped characters & variables, this is slower than single quoted string.
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:
$sample_string = ' Welcome to TutorialsClass.com ';
$clean_string = trim($sample_string);
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.