Php Operators and Variables

PHP Operators

  • Arithmetic Operators
  • Comparison Operators
  • Logical (or Relational) Operators
  • Assignment Operators
  • Conditional (or ternary) Operators
  1. Arithmetic Operator: It is a symbol used for basic mathematical calculations.
    Examples: +, -, /, %, ++, –
  2. Comparison Operator: It is used to compare the values of two different operands.
    Examples: ==, !=, <. <=, >, >=
  3. Logical Operator: It is used to connect two or more operands.
    Examples: AND or &&, OR or ||, !
  4. Assignment Operator: It is used to assign value or a result of an expression to an identifier.
    Examples: =, +=, =+, /=, %=
  5. Conditional Operator: It first evaluates an expression for a true or false value and then executes one of the two given statements depending upon the result of the evaluation.
    The conditional operator has this syntax- ?:

Variable Manipulation

Sometimes it is convenient to be able to have variable names. That is a variable name that can be set and used dynamically.

<?php  
$a="hello";  
?>

A variable takes the value of a variable and treats that as the name of the variable. In the above example, hello can be used as the name of a variable by using two dollar signs i.e,

<?php  
$$a="world";  
?>

At this point two variables have been defined and stored in the PHP symbol tree: $a with content “hello” and $hello with contents “world”, Therefore this statement:

<?php  
echo "$a${$a}";  
?>

Produces the exact same output as:

<?php  
echo "$a $hello";  
?>