php operators

PHP arithmetic operator are used to integer (numeric) value to perform basic operation like sum, multiplication, subtraction and division.

PHP Supports all basic arithmetic operators :

  • Add               +
  • Subtract        –
  • Multiply         *
  • Division         /
  • Modulus       %

ADD (+) arithmetic operator

First we take an example of addition of two numeric value in php.
here, we have declare two variable with integer value.

 <?php
 $first = 5;
 $second = 2;
 $sum = $first + $second;
 echo “sum = ” . $sum;
?>

The output of example:
sum = 7

The continue statement

Let's use the previous example, but this time let's add a check to see if the number is even. If it is, we will skip it, so that only odd numbers will be printed out.

$counter = 0;

while ($counter < 10) {
    $counter += 1;

    if ($counter % 2 == 0) {
        echo "Skipping number $counter because it is even.\n";
        continue;
    }

    echo "Executing - counter is $counter.\n";
}

Variables and Types

<?php
// Part 1: add the name and age variables.
echo "Hello $name. You are $age years old.\n";
// Part 2: sum up the variables x and y and
// put the result in the sum variable.
$x = 195793;
$y = 256836;
$sum = NULL;
echo "The sum of $x and $y is $sum."
?>

PHP while Loop

The while statement will loops through a block of code as long as the condition specified in the while statement evaluate to true.

while(condition){
    // Code to be executed
}
<?php
$i = 1;
while($i <= 4){
    $i++;
    echo "The number is " . $i . "<br>";
}
?>

PHP do…while Loop

The do-while loop is a variant of while loop, which evaluates the condition at the end of each loop iteration. With a do-while loop the block of code executed once, and then the condition is evaluated, if the condition is true, the statement is repeated as long as the specified condition evaluated to is true.

do{
    // Code to be executed
}
while(condition);
<?php
$i = 1;
do{
    $i++;
    echo "The number is " . $i . "<br>";
}
while($i <= 3);
?>

 

 NEXT