PHP Expressions, Statements, and Blocks
Essential PHP expressions, statements, and blocks
Expressions
In PHP, an expression is a combination of values, variables, operators, and function calls that can be evaluated to produce a value. Expressions can be as simple as a single value or variable, or they can be more complex involving multiple components.
// Simple expression
$result = 5 + 3; // Evaluates to 8
// Complex expression
$total = (10 * 2) + (5 / 2); // Evaluates to 25
Statements
A statement is a complete instruction that performs an action. In PHP, statements are typically terminated with a semicolon (;). Statements can include expressions, control structures, function calls, and more.
// Variable assignment statement
$name = "John";
// If statement
if ($name === "John") {
echo "Hello, John!";
}
// For loop statement
for ($i = 0; $i < 5; $i++) {
echo $i;
}
Blocks
A block is a group of statements that are enclosed within curly braces ({}). Blocks are used to define the scope of control structures such as loops and conditionals.
// If statement with a block
if ($name === "John") {
echo "Hello, John!";
echo "Welcome to PHP programming.";
}
// For loop with a block
for ($i = 0; $i < 5; $i++) {
echo "The value of i is: " . $i;
}
Related Posts
PHP Array Functions
Essential PHP array functions and patterns
Singleton Pattern
Implementing the singleton pattern in PHP
PHP Array and Map
Essential PHP array and map functions and their usage
PHP Casting and Conversions
Learning about PHP casting and conversions