phpbeginner
PHP Basics
Variables, arrays and functions
6 questions
By EZ4Code Team
1. How are PHP variables declared?
Starting with $, e.g. $name
Starting with @
Just the variable name
Starting with #
Explanation: PHP variables start with $, e.g. $name = 'Tom'; variable names are case-sensitive.
2. What is the string concatenation operator in PHP?
.
+
&
::
Explanation: PHP uses . to concatenate strings, e.g. 'Hello' . $name; + is arithmetic addition (non-numeric strings are treated as 0).
3. What is the syntax for defining an associative array?
['key' => 'value']
{'key': 'value'}
('key', 'value')
['key' : 'value']
Explanation: PHP associative arrays use the ['key' => 'value'] form, with keys associated to values via =>; they are essentially ordered maps.
4. What is the difference between echo and print?
echo can output multiple parameters and has no return value; print takes only one parameter and returns 1
They are exactly the same
print can output multiple parameters
echo has a return value
Explanation: echo is a language construct that accepts multiple parameters and has no return value; print is a language construct that accepts one parameter and returns 1; echo is slightly faster.
5. What is the syntax for default parameters in PHP functions?
function foo($x = 10) { }
function foo($x : 10) { }
function foo(default $x = 10) { }
function foo(10 $x) { }
Explanation: Default parameters use = default value in the parameter list, e.g. $x = 10; parameters with default values should come after parameters without defaults.
6. What function checks whether a variable is set (and not null)?
isset()
defined()
exists()
is_null() only checks for null
Explanation: isset($var) returns true when the variable is declared and not null; empty() checks for empty values (0/''/null, etc.).