FUNDAMENTAL OPERATIONS


php128 apg

In simplifying mathematical expressions consisting of the same type of operation, we perform one operation at a time generally starting from the left towards the right.

If an expression has more than one fundamental operation, you cannot perform operations in the order they appear in the given question.

We need follow the rules to perform the operations.

Some operations have to be performed before the others.

That is, each operation has its own precedence.

This entities has common usage throughout PHP.



OPERATORS


Operators are used to perform operations on variables and constants.

PHP divides the operators in the following groups:



ASSIGNMENT Operators
The PHP assignment operators
are used with NUMERIC values to write a value to a variable.
OPERATION SAME AS NAME WHAT DOES MAKES
$a = $b $a = $b ASSIGNMENT Assigns $a to $b
$a += $b $a = $a + $b ADDITION $a = $a + $b
$a -= $b $a = $a - $b SUBTRACTION $a = $a - $b
$a *= $b $a = $a * $b MULTIPLICATION $a = $a * $b
$a /= $b $a = $a / $b DIVISION $a = $a / $b
$a %= $b $a = $a % $b MODULUS $a = $a % $b
ed48

ARITHMETIC Operators
The PHP arithmetic operators
are used with numeric values to perform
common arithmetical operations,
such as ADDITION, SUBTRACTION, MULTIPLICATION etc.
OPERATOR USE NAME WHAT DOES MAKES
+ $a + $b ADDITION Sum of $a and $b
- $a - $b SUBTRACTION Difference of $a and $b
* $a * $b MULTIPLICATION Product of $a and $b
/ $a / $b DIVISION Quotient of $a and $b
% $a % $b MODULUS Remainder of $a divided by $b
** $a ** $b EXPONENTIATION Result of raising $a to the $b'th power
Since PHP 5.6.0
ed48

COMPARISON Operators
The PHP comparison operators
are used to compare two values, (NUMBER or STRING).
OPERATOR USE NAME RETURNS
== $a == $b EQUAL TRUE
if $a is EQUAL to $b
!= $a != $b NOT EQUAL TRUE
if $a is NOT EQUAL to $b
<> $a <> $b
=== $a === $b IDENTICAL TRUE
if $a is INDENTICAL to $b
!== $a !== $b NOT IDENTICAL TRUE
if $a is NOT INDENTICAL to $b
> $a > $b GREATER THAN TRUE
if $a is GREATER than $b
< $a < $b LESS THAN TRUE
if $a is LESS than $b
>= $a >= $b GREATER THAN or EQUAL TRUE
if $a is GREATER than or EQUAL to $b
<= $a <= $b LESS THAN or EQUAL TRUE
if $a is LESS than or EQUAL to $b
<=> $a <=> $b Spaceship An integer less than, equal to, or greater than zero when $a is respectively less than, equal to, or greater than $b.
Available as of PHP 7.
ed48

Increment/Decrement Operators
The PHP increment/decrement operators
are used to increment or decrement a variable's value.
OPERATOR NAME DESCRIPTION
++$a PRE-INCREMENT Increments $a by one, then returns $a
$a++ POST_INCREMENT Returns $a, then increments $a by one
--$a PRE-DECREMENT
Decrements $a by one, then returns $a
$a-- POST-DECREMENT Returns $a, then decrements $a by one
ed48

LOGICAL Operators
The PHP logical operators
are used to combine conditional statements.
OPERATOR USE NAME WHAT DOES MAKES
and $a and $b AND Returns TRUE if both $a and $a are TRUE
&& $a && $b
or $a or $b OR Returns TRUE if either $a or $b is TRUE
|| $a || $b
xor $a xor $b XOR Returns TRUE if either $a or $b is TRUE, but NOT BOTH
! !$a NOT Returns TRUE if $a is not TRUE
ed48

BITWISE Operators
Bitwise operators
allow evaluation and manipulation of specific bits within an INTEGER.
OPERATOR USE NAME WHAT DOES MAKES
& $ar1 & $ar2 AND Bits that are set in both $ar1 and $ar2 are set
| $ar1 | $ar2 Or (inclusive) Bits that are set in either $ar1 or $ar2 are set
^ $ar1 ^ $ar2 Or (exclusive) Bits that are set in $ar1 or $ar2
but not both are set
~ ~$ar1 Not Bits that are set in $a are not set,
and vice versa
<< $ar1 << $ar2 Shift Left Shift the bits of $r1 $ar2 steps to the left
(each step means "multiply by two")
>> $ar1 >> $ar2 Shift Right Shift the bits of $r1 $ar2 steps to the right
(each step means "divide by two")
ed48

  1 EXERCISE   

<?php

// Run this code severarl times

// ASSIGNMENT Operators I

$a01a mt_rand(-55);

$b01a mt_rand(-55);

echo 
"( $a01a += $b01a ) => " .  $a01a += $b01a;
echo 
'<br>';
echo 
"( $a01a -= $b01a ) => " .  $a01a -= $b01a;
echo 
'<br>';
echo 
"( $a01a *= $b01a ) => " .  $a01a *= $b01a;
echo 
'<br>';
if(
$b01a == 0)
{
echo 
"<br>Dision by $b01a is not possible!<br>";
}
else
{
echo 
"( $a01a /= $b01a ) => " .  $a01a /= $b01a;
echo 
'<br>';
echo 
"( $a01a %= $b01a ) => " .  $a01a %= $b01a;
}

?>

  1 EXERCISE   

<?php
 
// ASSIGNMENT Operators II

   
$t9a 92233720368547758;
$cr10a 2;

$t9b 92233720368547758;
$cr10b 2;

$t9aa 92233720368547758;
$cr10aa 2;

$t9bb 92233720368547758;
$cr10bb 2;

$t9aaa 92233720368547758;
$cr10aaa 2;

$t9bbb 92233720368547758;
$cr10bbb 2;

$t9aaaa 92233720368547758;
$cr10aaaa 2;

$t9bbbb 92233720368547758;
$cr10bbbb 2;

$t9aaaaa 92233720368547758;
$cr10aaaaa 2;

$t9bbbbb 92233720368547758;
$cr10bbbbb 2;

echo 
'( + ) ADDITION<br>';
echo 
'$t9a += $cr10a = ' $t9a += $cr10a;
echo 
'<br>THE SAME AS:<br>';
echo 
'$t9b + $cr10b = ';
echo 
$t9b $cr10b;

echo 
'<br><br>( - ) SUBTRACTION<br>';
echo 
'$t9aa -= $cr10aa =' $t9aa -= $cr10aa;
echo 
'<br>THE SAME AS:<br>';
echo 
'$t9bb - $cr10bb = ';
echo 
$t9bb $cr10bb;

echo 
'<br><br>( * ) MULTIPLICATION<br>';
echo 
'$t9aaa *= $cr10aaa = ' $t9aaa *= $cr10aaa;
echo 
'<br>THE SAME AS:<br>';
echo 
'$t9bbb * $cr10bbb = ';
echo 
$t9bbb $cr10bbb;

echo 
'<br><br>( / ) DIVISION<br>';
echo 
'$t9aaaa /= $cr10aaaa = ' $t9aaaa /= $cr10aaaa;
echo 
'<br>THE SAME AS:br>';
echo 
'$t9bbbb / $cr10bbbb = ';
echo 
$t9bbbb $cr10bbbb;

echo 
'<br><br>( % ) MODULUS<br>';
echo 
'$t9aaaaa %= $cr10aaaaa = ' $t9aaaaa %= $cr10aaaaa;
echo 
'<br>THE SAME AS:<br>';
echo 
'$t9bbbbb % $cr10bbbbb = ';
echo 
$t9bbbbb $cr10bbbbb;
echo 
'<br><br>';

?> 

  2 EXERCISE   

<?php

// setlocale(LC_ALL, 'es-ES', 'es_ES');

// ARITHMETIC Operators I

$a01a mt_rand(-55);

$b01a mt_rand(-55);

echo 
"( $a01a + $b01a ) => " .  ($a01a $b01a);
echo 
'<br>';
echo 
"( $a01a - $b01a ) => " .  ($a01a $b01a);
echo 
'<br>';
echo 
"( $a01a * $b01a ) => " .  ($a01a $b01a);
echo 
'<br>';
if(
$b01a == 0)
{
echo 
"<br>Dision by $b01a is not possible!<br>";
}
else
{
echo 
"( $a01a / $b01a ) => " .  ($a01a $b01a);
echo 
'<br>';
echo 
"( $a01a % $b01a ) => " .  ($a01a $b01a);
}
echo 
'<br>';
echo 
"( $a01a ** $b01a ) => " .  ($a01a ** $b01a);
?>

  2 EXERCISE   

<?php

// setlocale(LC_ALL, 'es-ES', 'es_ES');
 
// ARITHMETIC Operators II
   
$t9a 92233720368547758;
$cr10a 2;
   
echo 
'( + ) ADDITION<br>';
echo 
'$t9a + $cr10a = ' . ($t9a $cr10a);

echo 
'<br><br>( - ) SUBTRACTION<br>';
echo 
'$t9a - $cr10a = ' . ($t9a $cr10a);

echo 
'<br><br>( * ) MULTIPLICATION<br>';
echo 
'$t9a * $cr10a = ' . ($t9a $cr10a);

echo 
'<br><br>( / ) DIVISION<br>';
echo 
'$t9a / $cr10a = ' . ($t9a $cr10a);

echo 
'<br><br>( % ) MODULUS<br>';
echo 
'$t9a % $cr10a = ' . ($t9a $cr10a);

echo 
'<br><br>( ** ) EXPONENTIATION<br>';
echo 
'$t9a ** $cr10a = ' . ($t9a ** $cr10a);
echo 
'<br><br>';

?> 

  3 EXERCISE   

<?php

// COMPARISON Operators

$a01a mt_rand(-55);

$b01a mt_rand(-55);

function 
evalue01($var01a$var01b)
{
    if(
$var01a == $var01b)
    {
        echo 
"( $var01a == $var01b )";
        echo 
'<br>';
    }
}
    
function 
evalue02($var01a$var01b)
{    
    if(
$var01a != $var01b)
    {
        echo 
"( $var01a != $var01b )";
        echo 
'<br>';        
    }
}


function 
evalue03($var01a$var01b)
{
    if(
$var01a <> $var01b)
    {
        echo 
"( $var01a <> $var01b )";
        echo 
'<br>';        
    }
}

function 
evalue04($var01a$var01b)
{
    if(
$var01a === $var01b)
    {
        echo 
"( $var01a === $var01b )";
        echo 
'<br>';        
    }
}

function 
evalue05($var01a$var01b)
{
    if(
$var01a !== $var01b)
    {
        echo 
"( $var01a !== $var01b )";
        echo 
'<br>';        
    }
}

function 
evalue06($var01a$var01b)
{
    if(
$var01a $var01b)
    {
        echo 
"( $var01a > $var01b )";
        echo 
'<br>';        
    }
}

function 
evalue07($var01a$var01b)
{
    if(
$var01a $var01b)
    {
        echo 
"( $var01a < $var01b )";
        echo 
'<br>';        
    }
}

function 
evalue08($var01a$var01b)
{
    if(
$var01a >= $var01b)
    {
        echo 
"( $var01a >= $var01b )";
        echo 
'<br>';        
    }
}

function 
evalue09($var01a$var01b)
{
    if(
$var01a <= $var01b)
    {
        echo 
"( $var01a <= $var01b )";
        echo 
'<br>';        
    }
}

function 
evalue10($var01a$var01b)
{
    if(
$var01a <=> $var01b)
    {
        echo 
"( $var01a <=> $var01b )";
        echo 
'<br>';        
    }
}

evalue01($a01a$b01a);

evalue02($a01a$b01a);

evalue03($a01a$b01a);

evalue04($a01a$b01a);

evalue05($a01a$b01a);

evalue06($a01a$b01a);

evalue07($a01a$b01a);

evalue08($a01a$b01a);

evalue09($a01a$b01a);

evalue10($a01a$b01a);

?>

  4 EXERCISE   

<?php

// setlocale(LC_ALL, 'es-ES', 'es_ES');

// Increment/Decrement Operators

   
$v1r 999;
  
   
$v2r 999;   
   
   
$v3r 999;
   
   
$v4r 999.123
   
echo 
'Initial values:<br><br>';

echo 
"\$v1r = $v1r <br>\$v2r = $v2r 
                     <br>\$v3r = 
$v3r <br>\$v4r = $v4r<br><br>";

echo 
'After ++$v1r = ' . ++$v1r ' <br>Now  $v1r = ' $v1r '<br><br>';

echo 
'After $v2r++ = ' $v2r++ . ' <br>Now  $v2r = ' $v2r '<br><br>';

echo 
'After --$v3r = ' . --$v3r ' <br>Now  $v1r = ' $v3r '<br><br>';

echo 
'After $v4r-- = ' $v4r-- . ' <br>Now  $v2r = ' $v4r '<br><br>';

?> 

  5 EXERCISE   

<?php

//  LOGICAL Operators
 
function if_and($pr1$pr2$v1$v2)
   {
      if(
$pr1 == $v1  and $pr2 == $v2)
      
// if($pr1 == $v1  && $pr2 == $v2)
      
{
          echo 
'( Y ) Logical Operator and IS applicable<br><br>';
       
      }
      else
      {
          echo 
'( N ) Logical Operator and IS NOT applicable<br><br>';
      }  
   }

// - - - - - - - - - - - - - - - - - - - - - - -    
   
function if_or($pr1$pr2$v1$v2)
   {
      if(
$pr1 == $v1  or $pr2 == $v2)
      
// if($pr1 == $v1  || $pr2 == $v2)
      
{
          echo 
'( Y ) Logical Operator or IS applicable<br><br>';
       
      }
      else
      {
          echo 
'( N ) Logical Operator or IS NOT applicable<br><br>';
      }  
   }

// - - - - - - - - - - - - - - - - - - - - - - -    
   
function if_xor($pr1$pr2$v1$v2)
   {
      if(
$pr1 == $v1  xor $pr2 == $v2)
      {
          echo 
'( Y ) Logical Operator xor IS applicable<br><br>';
       
      }
      else
      {
          echo 
'( N ) Logical Operator xor IS NOT applicable<br><br>';
      }  
   }
   
// - - - - - - - - - - - - - - - - - - - - - - - 
   
function if_not($pr1$v1)
   {
      if(
$pr1 !== $v1)
      {
          echo 
'( Y ) Logical Operator EQUAL IS applicable<br><br>';
       
      }
      else
      {
          echo 
'( N ) Logical Operator NOT EQUAL IS NOT applicable<br><br>';
      }  
   }
   
// - - - - - - - - - - - - - - - - - - - - - - -    

 
$lr1 8;
 
$lr2 9;
 
 
 
if_and($lr1$lr289);

 
if_and($lr1$lr2890);
 

 
if_or($lr1$lr2890);
 
 
if_or($lr1$lr2809);
 
 
if_or($lr1$lr28090);
 

 
if_xor($lr1$lr2890);
 
 
if_xor($lr1$lr2809);
 
 
if_xor($lr1$lr28090);
 

 
if_not($lr18);
 
 
if_not($lr19);
 
 
if_not($lr29);
 
 
if_not($lr28);

?> 

 RESULT   

( Y ) Logical Operator and IS applicable

( N ) Logical Operator and IS NOT applicable

( Y ) Logical Operator or IS applicable

( Y ) Logical Operator or IS applicable

( N ) Logical Operator or IS NOT applicable

( Y ) Logical Operator xor IS applicable

( Y ) Logical Operator xor IS applicable

( N ) Logical Operator xor IS NOT applicable

( N ) Logical Operator NOT EQUAL IS NOT applicable

( Y ) Logical Operator EQUAL IS applicable

( N ) Logical Operator NOT EQUAL IS NOT applicable

( Y ) Logical Operator EQUAL IS applicable