PHP Interview Questions
This page contains PHP interview questions and answers.
PHP resembles Perl and C programming language.
print
echo
Both echo and print are language construct and not a function and both are used to print output.
But they have the following differences.
PI
3.14
We can create constants in the following two ways.
define()
const
In the following code we are creating the PI constant using the define function.
define('PI', 3.14);
In the following code we are creating the PI constant using the const keyword.
const PI = 3.14;
In the following example we have an associative array $assocArr and we are printing its content using the foreach loop.
$assocArr
foreach
$assocArr = array( 'firstname' => 'Yusuf', 'lastname' => 'Shakeel', 'youtube' => 'https://www.youtube.com/yusufshakeel' ); foreach ($assocArr as $key => $value) { echo "<p>{$key} : {$value}</p>"; }
isset
We use the isset function to check if a variable is defined and has a value other than NULL.
NULL
PHP Code:
$x = null; isset($x);
Answer: false as $x is set to NULL and isset returns false if value is NULL.
$x = "Hello World"; unset($x); isset($x);
Answer: false as $x was unset.
$x
$$x
$x is a variable whereas, $$x is a variable of variable.
Example:
$x = 'a'; $$x = 20; echo $x . ' ' . ${$x} . ' ' . $a;
Output: a 20 20.
a 20 20
In the above code we have a variable $x which is assigned value a.
a
$$x means $a as $x holds value a. So, we are assigning value 20 to $a.
$a
In the last line we are printing all the value.
Magic constants are predefined constants which starts and ends with double underscore (__).
__
Following are some of the magic constants.
__LINE__
__FUNCTION__
__CLASS__
__FILE__
__METHOD__
We can create single line comments as follows.
//
#
We can create multi line comments using /* and */.
/*
*/
To create variable length argument function in PHP we have to use three dots like the following.
function foo(...$args) { // some code goes here }