Get ready for your next interview with our comprehensive question library
PHP stands for "PHP: Hypertext Preprocessor" (a recursive acronym). It's a server-side scripting language designed for web development that executes on the server and returns HTML to the browser. PHP is open-source, platform-independent, and can be embedded directly into HTML.
Both echo
and print
are used to output data, but they have key differences:
echo
has no return value, while print
always returns 1echo
can take multiple parameters, print
can only take oneecho
is marginally faster since it doesn't return a valueecho
can be used with or without parentheses, print
can also be used both waysecho "Hello", " World"; // Works
print "Hello World"; // Works
// print "Hello", " World"; // Error - multiple parameters not allowed
The "_once" variants prevent redeclaration errors when the same file might be included multiple times.
PHP tags are used to embed PHP code in HTML. The types are:
<?php ... ?>
(recommended and always available)<?= ... ?>
(for outputting, always enabled in PHP 5.4+)<? ... ?>
(not recommended, requires short_open_tag enabled)<% ... %>
(removed in PHP 7.0)Standard tags are recommended for maximum compatibility and clarity.
PHP supports several data types grouped into three categories:
PHP is dynamically typed, meaning variables don't need explicit type declaration and can change types during execution.
$a = "5"; // string
$b = 5; // integer
var_dump($a == $b); // true (values equal after conversion)
var_dump($a === $b); // false (different types)
Type juggling is PHP's automatic conversion of data types based on context. While convenient, it can cause unexpected results.
$str = "10";
$num = 5;
echo $str + $num; // 15 (string converted to integer)
Use strict comparison (===) and type declarations to avoid issues.
Superglobals are built-in variables that are always accessible from any scope without using the global
keyword. The main superglobals are:
Constants can be declared using define()
function or const
keyword:
define('MAX_SIZE', 100);
const MIN_SIZE = 10;
// define() can be conditional
if ($condition) {
define('DYNAMIC', 'value');
}
// const cannot be used conditionally
The null coalescing operator ??
(introduced in PHP 7.0) returns the first operand if it exists and is not null, otherwise returns the second operand. It's useful for providing default values.
$username = $_GET['user'] ?? 'guest';
// Equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'guest';
PHP 7.4 introduced the null coalescing assignment operator ??=
for even more concise code.
The spaceship operator <=>
(introduced in PHP 7.0) is a three-way comparison operator that returns:
echo 1 <=> 2; // -1
echo 2 <=> 2; // 0
echo 3 <=> 2; // 1
It's particularly useful for sorting callbacks and comparing multiple values.
Upgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumUpgrade to Premium to see the answer
Upgrade to PremiumAccess all premium content - interview questions, and other learning resources
We regularly update our features and content, to ensure you get the most relevant and updated premium content.
1000 monthly credits
Cancel anytime