PHP Expressions


Share

Expression is most fundamental part of any programming language.
An expression is a combination of values, variables, operators, and functions that results in a value.
We can use Boolean values in expression. A basic Boolean value can be either TRUE or FALSE.
For example, the expression in 2==2(2 is equal to 2) is TRUE and 3==4(3 is equal to 4) is FALSE.



Literals and Variables

The simplest form of an expression is a literal,
which simply means something that evaluates to itself, such as the number 38 or the string “Good Morning”.
An expression could also simply be a variable, which evaluates to the value that has been assigned to it.
These are both types of expressions because they return a value.


In below example we can see five literals.

<?php
$name= "jiya";
$age= 34;
echo "a: " . 73.; // Numeric literal
echo "b: " . "Hello"; // String literal
echo "c: " . FALSE // Constant literal
echo "d: " . $name // Variable string literal
echo "e: " . $age // Variable numeric literal
?> 
OUTPUT:
a: 73 
b: Hello
c:
d: jiya
e: 34

When you combine assignment or control-flow constructs with expressions, the result is a statement
shows one of each. The first assigns the result of the expession
366 - $day_number to the variable $days_to_new_year, and the second outputs
a friendly message only if the expression $days_to_new_year < 30 evaluates to TRUE.

Example in below

<?php
$days_to_new_year = 366 - $day_number;  
if ($days_to_new_year < 30)
echo "Not long now till new year"; 
?>