PHP interview questions


Total available count: 33
Subject - PHP Frameworks
Subsubject - PHP

What is the difference between global vs $GLOBALS?

Global: all references to variable will refer to the global version. There is no limit to the number of global variables that can be modified by a function.

Example:

<?php
$x = 1;
$y = 2;

function Sum()
{
global $x, $y;

$y = $x + $y;
}

Sum();
echo $y;
?>

$GLOBALS: References to all variables available in global scope. An associative array contains references to all variables which are currently described in the global scope of the script. The variable names are the keys of the array.

Example:

<?php
function test() {
$foo = "local variables";

echo '$foo in global scope: ' . $GLOBALS["foo"] . "\n";
echo '$foo in current scope: ' . $foo . "\n";
}

$foo = "Example Info";
test();
?>

 




Next 5 interview question(s)

1
What is the difference between include and require?
2
What is the difference between named functions vs anonymous functions?
3
What is the difference between self and $this?
4
What is the difference between var_dump() vs print_r()?
5
What is the use of ::class?


Comments