PHP: Expressions

Introduction to PHP Expressions

In PHP, an expression is anything that has a value. Expressions are the most important building blocks of PHP. Almost everything you write in PHP is an expression. The simplest yet most accurate way to define an expression is "anything that has a value".

Types of Expressions

PHP supports several types of expressions:

  • Arithmetic Expressions
  • String Expressions
  • Logical Expressions
  • Assignment Expressions
  • Comparison Expressions
  • Conditional Expressions

Arithmetic Expressions

Arithmetic expressions use arithmetic operators to perform mathematical operations.


<?php
$x = 10;
$y = 5;
echo $x + $y;  // Outputs: 15
echo $x - $y;  // Outputs: 5
echo $x * $y;  // Outputs: 50
echo $x / $y;  // Outputs: 2
echo $x % $y;  // Outputs: 0 (modulus)
echo $x ** $y; // Outputs: 100000 (exponentiation)
?>
            

String Expressions

String expressions involve manipulation or combination of strings.


<?php
$greeting = "Hello";
$name = "World";
echo $greeting . " " . $name; // Outputs: Hello World
echo "$greeting $name";       // Outputs: Hello World
?>
            

Logical Expressions

Logical expressions evaluate to either true or false.


<?php
$x = true;
$y = false;
var_dump($x && $y); // Outputs: bool(false)
var_dump($x || $y); // Outputs: bool(true)
var_dump(!$x);      // Outputs: bool(false)
?>
            

Assignment Expressions

Assignment expressions assign values to variables.


<?php
$x = 10;
$y = 5;
$x += $y;  // Equivalent to $x = $x + $y
echo $x;   // Outputs: 15

$z = ($x = 3) + 5;
echo $z;   // Outputs: 8
?>
            

Comparison Expressions

Comparison expressions compare two values and return a boolean result.


<?php
$x = 10;
$y = 5;
var_dump($x == $y);  // Outputs: bool(false)
var_dump($x != $y);  // Outputs: bool(true)
var_dump($x < $y);   // Outputs: bool(false)
var_dump($x >= $y);  // Outputs: bool(true)
?>
            

Conditional Expressions

Conditional expressions use the ternary operator to make decisions.


<?php
$age = 20;
$status = ($age >= 18) ? "adult" : "minor";
echo $status;  // Outputs: adult

// Null coalescing operator (PHP 7+)
$name = $_GET['name'] ?? 'Anonymous';
echo $name;  // Outputs: 'Anonymous' if $_GET['name'] is not set
?>
            

Complex Expressions

Expressions can be combined to form more complex expressions.


<?php
$x = 10;
$y = 5;
$z = 2;

$result = ($x + $y) * $z;
echo $result;  // Outputs: 30

$isAdult = ($age >= 18) && ($country == "USA");
var_dump($isAdult);  // Outputs: bool(true) or bool(false)
?>
            

Expression Precedence

When multiple operators are used in an expression, the order of precedence determines how the expression is evaluated.


<?php
$result = 5 + 3 * 2;
echo $result;  // Outputs: 11 (not 16)

$result = (5 + 3) * 2;
echo $result;  // Outputs: 16
?>
            

It's often a good practice to use parentheses to make the order of operations clear, even when not strictly necessary.

Conclusion

Understanding expressions is fundamental to PHP programming. They form the building blocks of statements and control structures, allowing you to perform calculations, make decisions, and manipulate data in your scripts. As you become more familiar with PHP, you'll find yourself combining these different types of expressions to create more complex and powerful programs.