Php Interview Questions And Answers

What is php ?

PHP stands for Hypertext Preprocessor. Its old full form was Personal Home Page. Php is an open source, server side scripting language, primarily used for web development. Php code could be executed with command line (CLI).

How variable is defined in Php ?

In php variable is defined using $ followed by variable name. Variables are case senstives. Variable name must be begin with either using a letter or underscore (_). As php is a loosely typed language so declaring data type of variable is not required. Php converts correct data type internally itself.
$name = "Joe"; /*string*/
$_age = 29; /*integer*/
$Price = 20.31; /* float*/



What is a contant in PHP ?

A constant is a name or identifier for a simple value. It cannot be changed during execution of script.

How is constant declared in PHP ?

A constant is declared by using function define().
example : define("MAX_VALUE",100);
In class define() function does not work. In order to declare constant in class const keyword is used.
example :

class Foo {
cosnt MAX_VALUE = 100;
}

Which function is used to check whether constant is declared or not ?

defined() function is used to check.
example : if(defined("MAX_VALUE")){
echo "Maximum value is ".MAX_VALUE ;
}
else
{
echo "There is no maximum value" ;
}

What are magic constants in php ?

Magic constants are predefined constants available to any script. magic constants start and end with double underscore.
Examples :
__LINE__ , it gives current line number
__FILE__ , it gives full file path
__DIR__ , it gives full directory path
__FUNCTION__ , it gives function name
__CLASS__ , it gives class name including namesapce
__TRAIT__ , it gives trait name including namesapce
__METHOD__ , it gives class method name.
__NAMESPACE__ , it gives current namesapce.

What is echo ?

echo is used to print string, number, escaping characters. "echo" is a language construct not a function so parenthesis may not be used. If more than one parameters are passed then we use parenthesis.

What is print ?

print is used to print string, number, escaping characters. "print" is a language construct not a function so parenthesis may not be used. It returns 1 unlike echo.

What are different types of loops in PHP ?

There are four types of loops i.e for, foreach, while, do..while.

In how many ways, we can do comments in php ?

There are two types of comments i.e single line and mutiline.
Single line comment is done in two ways.
1. using //,for example // this line is commented.
2. using #, for example #this line is commented.
Multiline comment is done within /* */.
For example:
/*
comment line 1
comment line 2
comment line 3.
*/

How many types tags are there in PHP ?

PHP tags are
01. <?php ?>
02. <? ?>
03. <% %>

For what isset() is used ?

isset() is used to check whether variable exists or not. If variable exists then it returns true otherwise false.

What is the use of unset() ?

unset() function is used to destroy variable, array and array element etc.

What is the use of empty() ?

empty() function is used to determined whether variable is empty or not.

In php, how files are included in other files ?

To include files in php require(), include(), require_once() and include_once() are used. File path as parameter is passed in these functions.

What is difference between require() and require_once() ?

require() includes file whether file is already included or not. But require_once() includes if file is not already included.

What is difference between include() and include_once() ?

include() includes file whether file is already included or not. But include_once() includes if file is not already included.

What is difference between require() and include() ?

require() throws fatal error if file is not found and halts the execution of the script. Whereas include() gives warning if file is not found and does not terminate the script.

Which php functions are used for script termination ?

exit() and die() functions are used to stop the php script.

What is PHP Array ?

Array stores multiple values under single variable. It can store different types of data types.

How many types of arrays are in php ?

There are three types of arrays
01. Indexed array i.e. $fruits = array("Apple", "Orange", "Grapes");
02. Associative array i.e. $salaries = array("Joe"=>22000, "Mac"=>30000, "Peter"=>28000);
03. Multidimensional array
 i.e. $employees = array(
   array(
   "name" => "Joe",
   "salary" => 22000,
   ),
   array(
   "Mac" => "Clark Kent",
   ),
   array(
   "name" => "Peter",
   "salary" => 28000,
   )
   );

Which function is used to count the number of elements in Php ?

count() function is used i.e. $fruits = array("Apple", "Orange", "Grapes");
echo count($fruits);

Which function is used to check if value exists in Php array ?

in_array(), function checks value exists in array.

Which function is used to check if key exists in Php array ?

key_exists(), function checks key exists in array.

Which function is used to check if key exists in Php array ?

key_exists(), function checks key exists in array.

Which function is used to get / set the current session id in php ?

session_id() is used to get / set the current session id.

How can get / set the current session name in php ?

session_name() is used to get / set the current session name.

How session is started in php ?

function session_start() starts php session.