
PHP Interview Questions
This page contains PHP interview questions and answers.
PHP resembles Perl and C programming language.
print and echo in PHP?Both echo and print are language construct and not a function and both are used to print output.
But they have the following differences.
echo returns no value while print returns 1echo can take multiple arguments while print can take one argument.PI and set the value to 3.14 in PHPWe can create constants in the following two ways.
define() functionconst keywordIn 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 = array(
'firstname' => 'Yusuf',
'lastname' => 'Shakeel',
'youtube' => 'https://www.youtube.com/yusufshakeel'
);
foreach ($assocArr as $key => $value) {
echo "<p>{$key} : {$value}</p>";
}
isset function?We use the isset function to check if a variable is defined and has a value other than 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 and $$x in PHP?$x is a variable whereas, $$x is a variable of variable.
Example:
$x = 'a';
$$x = 20;
echo $x . ' ' . ${$x} . ' ' . $a;
Output: a 20 20.
In the above code we have a variable $x which is assigned value a.
$$x means $a as $x holds value a. So, we are assigning value 20 to $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
}
ADVERTISEMENT