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.



OPERATORS


Operators are used to perform operations on variables and values.

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
$bX =& $a $bX =& $a REFERENCE $bX = $a
By reference
$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


PHP_INT_MIN
          -9223372036854775808

PHP_INT_MAX
          9223372036854775807

PHP_FLOAT_EPSILON
          2.2204460492503E-16

PHP_FLOAT_MIN
          2.2250738585072E-308

PHP_FLOAT_MAX
          1.7976931348623E+308

  1 EXERCISE   

<?php
 
/* - - - - - - - - - - - - - - - - - - - - - - -

    Assignment operators 
    for numeric values

   - - - - - - - - - - - - - - - - - - - - - - - */
   
$rn01 10;

$as01 922;

$vl01 $as01 $rn01;

$vr01 5;


// $vl01 += $vr01;
echo 'ADDITION<br>' .
'( ' $vl01 ' += ' $vr01 ' ) = ' . ($vl01 += $vr01) . '<br><br>';

// $vl01 -= $vr01;
echo 'SUBTRACTION<br>' .
'( ' $vl01 ' -= ' $vr01 ' ) = ' .  ($vl01 -= $vr01) . '<br><br>';

// $vl01 *= $vr01;
echo 'MULTIPLICATION<br>' .
'( ' $vl01 ' *= ' $vr01 ' ) = ' .($vl01 *= $vr01) . '<br><br>';

// $vl01 / $vr01;
echo 'DIVISION<br>' .
'( ' $vl01 ' /= ' $vr01 ' ) = ' . ($vl01 /= $vr01) . '<br><br>';

// $vl01 %= $vr01;
echo 'MODULUS<br>' .
'( ' $vl01 ' %= ' $vr01 ' ) = ' . ($vl01 %= $vr01) . '<br><br>';

$rf01 =& $vr01;
echo 
'REFERENCE TO THE VALUE OF $vr01 = ' $vr01 '<br>' .
'( ' $vr01 ' =& ' $rf01 ' ) = ' $rf01 '<br><br>';

?> 

 RESULT   

ADDITION
( 912 += 5 ) = 917

SUBTRACTION
( 917 -= 5 ) = 912

MULTIPLICATION
( 912 *= 5 ) = 4560

DIVISION
( 4560 /= 5 ) = 912

MODULUS
( 912 %= 5 ) = 2

REFERENCE TO THE VALUE OF $vr01 = 5
( 5 =& 5 ) = 5


  2 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

Assignment operators 
for numeric values

- - - - - - - - - - - - - - - - - - - - - - - */

$a02 PHP_FLOAT_MIN;

$b02 =& $a02;

$c02 =& $b02;

echo 
'$a02 = ' $a02 
'<br><br>$b02 = ' $b02 
'<br><br>$c02 = ' $c02;

?> 

 RESULT   

$a02 = 2.2250738585072E-308

$b02 = 2.2250738585072E-308

$c02 = 2.2250738585072E-308


The same value is REFERENCED to the three variables.


 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


  3 EXERCISE   

<?php
 
/* - - - - - - - - - - - - - - - - - - - - - - -

   Arithmetic Operators

   - - - - - - - - - - - - - - - - - - - - - - - */
   
$a03 92233720368547758;
$c03 2;
   
echo 
'ADDITION<br>' .
'( ' $a03 ' + ' $c03 ' ) = ' . ($a03 $c03);

echo 
'<br><br>SUBTRACTION<br>' .
'( ' $a03 ' - ' $c03 ' ) = ' . ($a03 $c03);

echo 
'<br><br>MULTIPLICATION<br>' .
'( ' $a03 ' * ' $c03 ' ) = ' . ($a03 $c03);

echo 
'<br><br>DIVISION<br>' .
'( ' $a03 ' / ' $c03 ' ) = ' . ($a03 $c03);

echo 
'<br><br>MODULUS<br>' .
'( ' $a03 ' % ' $c03 ' ) = ' $a03 $c03;

echo 
'<br><br>EXPONENTIATION<br>' .
'( ' $a03 ' ** ' $c03 ' ) = ' . ($a03 ** $c03);
echo 
'<br><br>';

?> 

 RESULT   

ADDITION
( 92233720368547758 + 2 ) = 92233720368547760

SUBTRACTION
( 92233720368547758 - 2 ) = 92233720368547756

MULTIPLICATION
( 92233720368547758 * 2 ) = 184467440737095516

DIVISION
( 92233720368547758 / 2 ) = 46116860184273879

MODULUS
( 92233720368547758 % 2 ) = 0

EXPONENTIATION
( 92233720368547758 ** 2 ) = 8.5070591730235E+33


 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 Returns 0
if $a = $b
Returns -1
if $a < $b
Returns 1
if $a > $b
ed48


  4 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

Comparison Operators

Individualized
USER-DEFINED 
comparison functions

- - - - - - - - - - - - - - - - - - - - - - - */

function isequal($par1$par2)
{
if(
$par1 == $par2)
{
echo 
$par1 
' is EQUAL to ' $par2 '<br>';
}
else
{
echo 
$par1 
' is NOT EQUAL to ' $par2 '<br>';
}
}


function 
isnotequal($par1$par2)
{
if(
$par1 != $par2)
{
echo 
$par1 
' is NOT EQUAL to ' $par2 '<br>';
}
else
{
echo 
$par1 
' is EQUAL to ' $par2 '<br>';
}
}


function 
isidentical($par1$par2)
{
if(
$par1 === $par2)
{
echo 
$par1 
' is IDENTICAL to ' $par2 '<br>';
}
else
{
echo 
$par1 
' is NOT IDENTICAL to ' $par2 '<br>';
}
}


function 
isgreaterthan($par1$par2)
{
if(
$par1 $par2)
{
echo 
$par1 
' is GREATER than ' $par2 '<br>';
}
else
{
echo 
$par1 
' is NOT GREATER than ' $par2 '<br>';
}
}


function 
islessthan($par1$par2)
{
if(
$par1 $par2)
{
echo 
$par1 
' is LESS than ' $par2 '<br>';
}
else
{
echo 
$par1 
' is NOT LESS than ' $par2 '<br>';
}
}


function 
isgreaterthanorequal($par1$par2)
{
if(
$par1 >= $par2)
{
echo 
$par1 
' is GREATER than or EQUAL ' $par2 '<br>';
}
else
{
echo 
$par1 
' is NOT GREATER than or EQUAL ' $par2 '<br>';
}



function 
islessthanorequal($par1$par2)
{
if(
$par1 <= $par2)
{
echo 
$par1 
' is LESS than or EQUAL ' $par2 '<br>';
}
else
{
echo 
$par1 
' is NOT LESS than or EQUAL ' $par2 '<br>';
}



$a04 98.321;
$n0492.987;

$t04s '"Non ducor duco."';
$t04t '"Non ducor duco!"'

echo 
'- - - - - - - - - - - - - - - -<br>';

echo 
'NUMBERS COMPARE<br>';

echo 
'- - - - - - - - - - - - - - - -<br><br>';

isequal($a04$a04);   
isequal($a04$n04);
echo 
'<br><br>';
isnotequal($a04$n04);
isnotequal($n04$a04);
echo 
'<br><br>';
isidentical($a04$n04);
isidentical($a04$a04);
isidentical($n04$n04);
echo 
'<br><br>';
isgreaterthan($a04$n04);
isgreaterthan($n04$a04);
echo 
'<br><br>';
islessthan($a04$n04);
islessthan($n04$a04);
echo 
'<br><br>';
isgreaterthanorequal($a04$n04);
isgreaterthanorequal($n04$a04);
isgreaterthanorequal($a04$a04);
isgreaterthanorequal($n04$n04);
echo 
'<br><br>';
islessthanorequal($a04$n04);
islessthanorequal($n04$a04);
islessthanorequal($a04$a04);
islessthanorequal($n04$n04);

echo 
'<br><br><br>- - - - - - - - - - - - - - - -<br>';

echo 
'STRINGS COMPARE<br>';

echo 
'- - - - - - - - - - - - - - - -<br><br>';

isequal($t04t$t04t);
isequal($t04s$t04t);
echo 
'<br><br>';
isnotequal($t04s$t04t);
isnotequal($t04t$t04s);
echo 
'<br><br>';
isnotequal($t04s$t04t);
isnotequal($t04t$t04s);
echo 
'<br><br>';
isidentical($t04s$t04t);
isidentical($t04s$t04s);
isidentical($t04t$t04t);
echo 
'<br><br>';
isgreaterthan($t04s$t04t);
isgreaterthan($t04t$t04s);
echo 
'<br><br>';
islessthan($t04s$t04t);
islessthan($t04t$t04s);
echo 
'<br><br>';
isgreaterthanorequal($t04s$t04t);
isgreaterthanorequal($t04t$t04s);
isgreaterthanorequal($t04t$t04t);
isgreaterthanorequal($t04s$t04s);
echo 
'<br><br>';
islessthanorequal($t04s$t04t);
islessthanorequal($t04t$t04s);
islessthanorequal($t04t$t04t);
islessthanorequal($t04s$t04s);

?> 

 RESULT   


- - - - - - - - - - - - - - - -
NUMBERS COMPARE
- - - - - - - - - - - - - - - -

98.321 is EQUAL to 98.321
98.321 is NOT EQUAL to 92.987


98.321 is NOT EQUAL to 92.987
92.987 is NOT EQUAL to 98.321


98.321 is NOT IDENTICAL to 92.987
98.321 is IDENTICAL to 98.321
92.987 is IDENTICAL to 92.987


98.321 is GREATER than 92.987
92.987 is NOT GREATER than 98.321


98.321 is NOT LESS than 92.987
92.987 is LESS than 98.321


98.321 is GREATER than or EQUAL 92.987
92.987 is NOT GREATER than or EQUAL 98.321
98.321 is GREATER than or EQUAL 98.321
92.987 is GREATER than or EQUAL 92.987


98.321 is NOT LESS than or EQUAL 92.987
92.987 is LESS than or EQUAL 98.321
98.321 is LESS than or EQUAL 98.321
92.987 is LESS than or EQUAL 92.987


- - - - - - - - - - - - - - - -
STRINGS COMPARE
- - - - - - - - - - - - - - - -

"Non ducor duco!" is EQUAL to "Non ducor duco!"
"Non ducor duco." is NOT EQUAL to "Non ducor duco!"


"Non ducor duco." is NOT EQUAL to "Non ducor duco!"
"Non ducor duco!" is NOT EQUAL to "Non ducor duco."


"Non ducor duco." is NOT EQUAL to "Non ducor duco!"
"Non ducor duco!" is NOT EQUAL to "Non ducor duco."


"Non ducor duco." is NOT IDENTICAL to "Non ducor duco!"
"Non ducor duco." is IDENTICAL to "Non ducor duco."
"Non ducor duco!" is IDENTICAL to "Non ducor duco!"


"Non ducor duco." is GREATER than "Non ducor duco!"
"Non ducor duco!" is NOT GREATER than "Non ducor duco."


"Non ducor duco." is NOT LESS than "Non ducor duco!"
"Non ducor duco!" is LESS than "Non ducor duco."


"Non ducor duco." is GREATER than or EQUAL "Non ducor duco!"
"Non ducor duco!" is NOT GREATER than or EQUAL "Non ducor duco."
"Non ducor duco!" is GREATER than or EQUAL "Non ducor duco!"
"Non ducor duco." is GREATER than or EQUAL "Non ducor duco."


"Non ducor duco." is NOT LESS than or EQUAL "Non ducor duco!"
"Non ducor duco!" is LESS than or EQUAL "Non ducor duco."
"Non ducor duco!" is LESS than or EQUAL "Non ducor duco!"
"Non ducor duco." is LESS than or EQUAL "Non ducor duco."
- - - - - - - - - - - - - - - -


  5 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

Comparison Operators

A single
USER-DEFINED 
comparison funtion

- - - - - - - - - - - - - - - - - - - - - - - */

function compare($par1$par2)
{
if(
$par1 == $par2)
{
echo 
$par1 ' is EQUAL to ' $par2 '<br><br>';
}
else
{
echo 
$par1 ' is NOT EQUAL to ' $par2 '<br><br>';
}

if(
$par1 != $par2)
{
echo 
$par1 ' is NOT EQUAL to ' $par2 '<br><br>';
}
else
{
echo 
$par1 ' is EQUAL to ' $par2 '<br><br>';
}

if(
$par1 === $par2)
{
echo 
$par1 
' is IDENTICAL to ' $par2 '<br><br>';
}
else
{
echo 
$par1 
' is NOT IDENTICAL to ' $par2 '<br><br>';
}

if(
$par1 $par2)
{
echo 
$par1 
' is GREATER than ' $par2 '<br><br>';
}
else
{
echo 
$par1 
' is NOT GREATER than ' $par2 '<br><br>';
}

if(
$par1 $par2)
{
echo 
$par1 
' is LESS than ' $par2 '<br><br>';
}
else
{
echo 
$par1 
' is NOT LESS than ' $par2 '<br><br>';
}

if(
$par1 >= $par2)
{
echo 
$par1 
' is GREATER than or EQUAL ' $par2 '<br><br>';
}
else
{
echo 
$par1 
' is NOT GREATER than or EQUAL ' $par2 '<br><br>';
}

if(
$par1 <= $par2)
{
echo 
$par1 
' is LESS than or EQUAL ' $par2 '<br><br>';
}
else
{
echo 
$par1 
' is NOT LESS than or EQUAL ' $par2 '<br><br>';
}

}

$n02a 92233720368547758;
$n02b 92233720368547758;

$t02a '"Non ducor duco."';
$t02b '"Non ducor duco!"';

compare($n02a$n02b);

compare($t02a$t02b);

?> 

 RESULT   

- - - - - - - - - - - - - - - - -
92233720368547758 is EQUAL to 92233720368547758

92233720368547758 is EQUAL to 92233720368547758

92233720368547758 is IDENTICAL to 92233720368547758

92233720368547758 is NOT GREATER than 92233720368547758

92233720368547758 is NOT LESS than 92233720368547758

92233720368547758 is GREATER than or EQUAL 92233720368547758

92233720368547758 is LESS than or EQUAL 92233720368547758

- - - - - - - - - - - - - - - - -
"Non ducor duco." is NOT EQUAL to "Non ducor duco!"

"Non ducor duco." is NOT EQUAL to "Non ducor duco!"

"Non ducor duco." is NOT IDENTICAL to "Non ducor duco!"

"Non ducor duco." is GREATER than "Non ducor duco!"

"Non ducor duco." is NOT LESS than "Non ducor duco!"

"Non ducor duco." is GREATER than or EQUAL "Non ducor duco!"

"Non ducor duco." is NOT LESS than or EQUAL "Non ducor duco!"

- - - - - - - - - - - - - - - - -


  6 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

Comparison Operators

A single
USER-DEFINED 
spaceship comparison funtion

- - - - - - - - - - - - - - - - - - - - - - - */

function spacesh($par1$par2)
{

if((
$par1 <=> $par2) == 0)
{
    echo 
$par1 ' = ' $par2 '<br><br>';
}

elseif((
$par1 <=> $par2) == -1)
{
    echo 
$par1 ' &lt; ' $par2 '<br><br>';
}

elseif((
$par1 <=> $par2) == 1)
{
    echo 
$par1 ' &gt; ' $par2 '<br><br>';
}

}

$n02a 922;
$n02b 922;
$n02c 9223;

$n02e 9223.372;
$n02f 9223.372;
$n02g 9223.373;

spacesh($n02a$n02b);

spacesh($n02a$n02c);

spacesh($n02c$n02a);


spacesh($n02e$n02f);

spacesh($n02e$n02g);

spacesh($n02g$n02e);

?> 

 RESULT   

- - - - - - - - - - - - - - - - -

922 = 922

922 < 9223

9223 > 922

9223.372 = 9223.372

9223.372 < 9223.373

9223.373 > 9223.372

- - - - - - - - - - - - - - - - -


 Increment and 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 1, then returns $a
$a++ POST_INCREMENT Returns $a,
then increments $a by 1
--$a PRE-DECREMENT
Decrements
$a by 1, then returns $a
$a-- POST-DECREMENT Returns $a,
then decrements $a by 1
ed48


  7 EXERCISE   

<?php
 
/* - - - - - - - - - - - - - - - - - - - - - - -

    Increment 
    and 
    Decrement Operators

   - - - - - - - - - - - - - - - - - - - - - - - */
   
   
$vl06a 999;
  
   
$vl06b 999;   
   
   
$vl06c 999;
   
   
$vl06d 999

echo 
'PRE-INCREMENT<br>Given value = ' $vl06a 
'<br>Increments ' $vl06a ' by 1, then returns ' . ++$vl06a 
'<br>CURRENT VALUE = ' $vl06a '<br><br>';

echo 
'POST-INCREMENT<br>Given value = ' $vl06b 
'<br>Returns ' $vl06b ' then  increments ' $vl06b 
' by 1, that is '$vl06b++ . 
'<br>CURRENT VALUE = ' $vl06b '<br><br>';

echo 
'PRE-DECREMENT<br>Given value = ' $vl06c 
'<br>Decrements ' $vl06c ' by 1, then returns ' . --$vl06c 
'<br>CURRENT VALUE = ' $vl06c '<br><br>';

echo 
'POST-DECREMENT<br>Given value = ' $vl06d 
'<br>Returns ' $vl06d ' then  decrements ' $vl06d 
' by 1, that is '$vl06d-- . 
'<br>CURRENT VALUE = ' $vl06d '<br><br>';

?> 

 RESULT   

PRE-INCREMENT
Given value = 999
Increments 999 by 1, then returns 1000
CURRENT VALUE = 1000

POST-INCREMENT
Given value = 999
Returns 999 then increments 999 by 1, that is 999
CURRENT VALUE = 1000

PRE-DECREMENT
Given value = 999
Decrements 999 by 1, then returns 998
CURRENT VALUE = 998

POST-DECREMENT
Given value = 999
Returns 999 then decrements 999 by 1, that is 999
CURRENT VALUE = 998


 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


  8 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - 

Logical Operators

- - - - - - - - - - - - - - - - - - - - - - - */ 

function if_and($pr1$pr2$v1$v2)
{
if(
$pr1 == $v1  and $pr2 == $v2)
// if($pr1 == $v1  && $pr2 == $v2)
{
echo 
'(' $pr1 ' == ' $v1 ' and ' $pr2 .' == ' $v2 .')<br>';    
echo 
'( Y ) AND is applicable<br><br>';

}
else
{
echo 
'(' $pr1 ' == ' $v1 ' and ' $pr2 .' == ' $v2 .')<br>';
echo 
'( N ) 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 
'(' $pr1 ' == ' $v1 ' or ' $pr2 .' == ' $v2 .')<br>';    
echo 
'( Y ) OR is applicable<br><br>';

}
else
{
echo 
'(' $pr1 ' == ' $v1 ' or ' $pr2 .' == ' $v2 .')<br>';
echo 
'( N ) OR is NOT applicable<br><br>';
}  
}

 

function 
if_xor($pr1$pr2$v1$v2)
{
if(
$pr1 == $v1  xor $pr2 == $v2)
{
echo 
'(' $pr1 ' == ' $v1 ' xor ' $pr2 .' == ' $v2 .')<br>';
echo 
'( Y ) XOR is applicable<br><br>';

}
else
{
echo 
'(' $pr1 ' == ' $v1 ' xor ' $pr2 .' == ' $v2 .')<br>';
echo 
'( N ) XOR is NOT applicable<br><br>';
}  
}



function 
if_not($pr1$v1)
{
if(
$pr1 !== $v1)
{
echo 
'(' $pr1 ' !== ' $v1 ')<br>';
echo 
'( Y ) NOT EQUAL is applicable<br><br>';

}
else
{
echo 
'(' $pr1 ' !== ' $v1 ')<br>';
echo 
'( N ) NOT EQUAL is NOT applicable<br><br>';
}  
}


$vl07a 8;
$vl07b 9;


if_and($vl07a$vl07b89);

if_and($vl07a$vl07b890);


if_or($vl07a$vl07b890);

if_or($vl07a$vl07b809);

if_or($vl07a$vl07b8090);


if_xor($vl07a$vl07b890);

if_xor($vl07a$vl07b809);

if_xor($vl07a$vl07b8090);


if_not($vl07a8);

if_not($vl07a9);

if_not($vl07b9);

if_not($vl07b8);

?> 

 RESULT   

(8 == 8 and 9 == 9)
( Y ) AND is applicable

(8 == 8 and 9 == 90)
( N ) AND is NOT applicable

(8 == 8 or 9 == 90)
( Y ) OR is applicable

(8 == 80 or 9 == 9)
( Y ) OR is applicable

(8 == 80 or 9 == 90)
( N ) OR is NOT applicable

(8 == 8 xor 9 == 90)
( Y ) XOR is applicable

(8 == 80 xor 9 == 9)
( Y ) XOR is applicable

(8 == 80 xor 9 == 90)
( N ) XOR is NOT applicable

(8 !== 8)
( N ) NOT EQUAL is NOT applicable

(8 !== 9)
( Y ) NOT EQUAL is applicable

(9 !== 9)
( N ) NOT EQUAL is NOT applicable

(9 !== 8)
( Y ) NOT EQUAL is applicable


 String Operators 


PHP has two operators that are specially designed for STRINGS.

OPERATOR USE NAME WHAT DOES MAKES
. $str1 . $str2 CONCATENATION Performs the concatenation of $str1 and $str2
.= $str1 .= $str2 CONCATENATION ASSIGNMENT Appends
$str2 to $str1
=& $strX =& $str1 REFERENCE By reference
$strX = $str1
<=> $str1 <=> $str2 SPACESHIP Returns 0
if $str1 = $str2
Returns -1
if $str1 < $str2
Returns 1
if $str1 > $str2
ed48


You may have seen, here, the use of the CONCATENATION operator, (.), to concatenate several STRINGS.

You can also use the REFERENCE operator, (=&), to obtain the values of other variables like STRING.


  9 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - 

    String Operators

   - - - - - - - - - - - - - - - - - - - - - - - */ 
   
$str1 'It\'s six of one and ';
$str2 'half a dozen of the other.';

echo 
'CONCATENATION<br>' $str1 $str2 '<br><br>';

$str1 .= $str2
echo 
'CONCATENATION ASSIGNMENT<br>' $str1 '<br><br>';

$str3 =& $str1;
echo 
'BY REFERENCE<br>' $str3 '<br><br>';

?> 

 RESULT   

CONCATENATION
It's six of one and half a dozen of the other.

CONCATENATION ASSIGNMENT
It's six of one and half a dozen of the other.

BY REFERENCE
It's six of one and half a dozen of the other.


  10 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

STRING Operators

A single
USER-DEFINED 
spaceshstrip comparison funtion

- - - - - - - - - - - - - - - - - - - - - - - */

function spaceshstr($par1$par2)
{

if((
$par1 <=> $par2) == 0)
{
echo 
$par1 ' = ' $par2 '<br><br>';
}

elseif((
$par1 <=> $par2) == -1)
{
echo 
$par1 ' &lt; ' $par2 '<br><br>';
}

elseif((
$par1 <=> $par2) == 1)
{
echo 
$par1 ' &gt; ' $par2 '<br><br>';
}

}

$str08a '"spaceship operator"';
$str08b '"spaceship operator"';
$str08c '"Spaceship Operator"';
$str08d '"SPACESHIP OPERATOR"';


spaceshstr($str08a$str08b);

spaceshstr($str08a$str08c);

spaceshstr($str08c$str08a);

spaceshstr($str08d$str08a);


?> 

 RESULT   

"spaceship operator" = "spaceship operator"

"spaceship operator" > "Spaceship Operator"

"Spaceship Operator" < "spaceship operator"

"SPACESHIP OPERATOR" < "spaceship operator"


 Array Operators 


The PHP array operators are used to compare arrays.

OPERATOR USE NAME WHAT DOES MAKES
+ $ar1 + $ar2 UNION UNION of
$ar1 and $ar2
== $ar1 == $ar2 EQUALITY Returns TRUE
if $ar1 and $ar2 have the same key/value pairs
!= $ar1 != $ar2 INEQUALITY Returns TRUE
if $ar1 is not equal to $ar2
<> $ar1 <> $ar2
=== $ar1 === $ar2 IDENTITY Returns TRUE
if $ar1 and $ar2 have the same key/value pairs in the same order and of the same types
!== $ar1 !== $ar2 NON IDENTITY Returns TRUE
if $ar1 is not identical to $ar2
=& $arX !=& !$ar1 REFERENCE By reference
$arX = $ar1
??= $ar1['key']
??=
computeDefault()
NULL COALESCING Null coalescing assignment operator
<=> $ar1 <=> $ar2 SPACESHIP Returns 0
if $ar1 = $ar2
Returns -1
if $ar1 < $ar2
Returns 1
if $ar1 > $ar2
ed48


  11 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - 

Array Operators

UNION

$ar1 + $ar2 will be 
displayed with
ALL THE ELEMENTS of 
$ar1 and $ar2, 
since the indexes of the two
do not repeat themselves.

- - - - - - - - - - - - - - - - - - - - - - - */ 

$ar1 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$ar2 = [ 'O' => 32'P' => 18'Q' => ];

$ar12 $ar1 $ar2;

echo 
'<pre>';
print_r($ar12);
echo 
'</pre>';

/* - - - - - - - - - - - - - - - - - - - - - - - - - -

Array Operators

UNION      

The elements from the 
$ar1 will be used, 
and the matching elements
from the $ar2 will be ignored. 

- - - - - - - - - - - - - - - - - - - - - - - - - - - */

$ar3 = [ 281832 ];

$ar4 = [ 3218];

$ar34 $ar3 $ar4

echo 
'<pre>';
print_r($ar34);
echo 
'</pre>';

?> 

 RESULT   

- - - - - - - - - - - - - - - - - - - - - -

The union of ARRAYS is implemented based
on the keys, (indexes) -
- not on the values, (elements).

- - - - - - - - - - - - - - - - - - - - - -

Array Operators

UNION

$ar1 + $ar2 will be displayed with
ALL THE ELEMENTS of $ar1 and $ar2,
since the indexes of the two
do not repeat themselves.

Array ( [K] => 2 [L] => 8 [M] => 18 [N] => 32 [O] => 32 [P] => 18 [Q] => 8 )

- - - - - - - - - - - - - - - - - - - - - -

Array Operators

UNION

The elements from the $ar1 will be used,
and the matching elements from the
$ar2 will be ignored.

Array ( [0] => 2 [1] => 8 [2] => 18 [3] => 32 )

- - - - - - - - - - - - - - - - - - - - - -


  12 EXERCISE   

<?php

function is_equality($par1$par2)
{
if((
$par1 == $par2) == TRUE)
{
echo 
'( ';
var_dump($par1);
echo 
' == ';
var_dump($par2);
echo 
' )<br><br>(TRUE) The ARRAYS have the 
              SAME key/value pairs.<br>No matter the order!<br><br>'
;    
}
else
{
echo 
'( ';
var_dump($par1);
echo 
' == ';
var_dump($par2);
echo 
' )<br><br>(FALSE) The ARRAYS DOES NOT have the 
              SAME key/value pairs.<br><br>'
;    
}
}

/* - - - - - - - - - - - - - - - - - - - - - - - 

Array Operators

EQUALITY

The key/value pairs of
$ar5 and $ar6 have 
different values.

- - - - - - - - - - - - - - - - - - - - - - - */ 

$ar5 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$ar6 = [ 'O' => 32'P' => 18'Q' => ];

is_equality($ar5$ar6);

/* - - - - - - - - - - - - - - - - - - - - - -

Array Operators

EQUALITY      

The key/value pairs of
$ar7 and $ar8 have 
the same values,
no matter the order.      

- - - - - - - - - - - - - - - - - - - - - - */

$ar7 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$ar8 = [ 'K' => 2'N' => 32'L' => 8'M' => 18 ];

is_equality($ar7$ar8);

?> 

 RESULT   

( array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } == array(3) { ["O"]=> int(32) ["P"]=> int(18) ["Q"]=> int(8) } )

(FALSE) The ARRAYS DOES NOT have the SAME key/value pairs.


( array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } == array(4) { ["K"]=> int(2) ["N"]=> int(32) ["L"]=> int(8) ["M"]=> int(18) } )

(TRUE) The ARRAYS have the SAME key/value pairs.
No matter the order!


  13 EXERCISE   

<?php

function is_identity($par1$par2)
{
if((
$par1 === $par2) == TRUE)
{
echo 
'( ';
var_dump($par1);
echo 
' === ';
var_dump($par2);
echo 
' )<br><br>(TRUE) The ARRAYS have the 
SAME key/value pairs.<br>In the SAME order!<br><br>'
;    
}
else
{
echo 
'( ';
var_dump($par1);
echo 
' === ';
var_dump($par2);
echo 
' )<br><br>(FALSE) The ARRAYS DOES NOT have the 
SAME key/value pairs.<br><br>'
;    
}
}

/* - - - - - - - - - - - - - - - - - - - - - - - 

Array Operators

IDENTITY

The key/value pairs of
$ar9 and $ar10 have 
different values.

- - - - - - - - - - - - - - - - - - - - - - - */ 

$ar9 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$ar10 = [ 'O' => 32'P' => 18'Q' => ];

is_identity($ar9$ar10);

/* - - - - - - - - - - - - - - - - - - - - - - - 

Array Operators

IDENTITY

The key/value pairs of
$ar11 and $ar12 have 
the SAME values,
however, in different order

- - - - - - - - - - - - - - - - - - - - - - - */ 

$ar11 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$ar12 = [ 'K' => 2'N' => 32'L' => 8'M' => 18 ];

is_identity($ar11$ar12);

/* - - - - - - - - - - - - - - - - - - - - - -

Array Operators

IDENTITY      

The key/value pairs of
$ar13 and $ar14 have 
the same values,
in the SAME order.      

- - - - - - - - - - - - - - - - - - - - - - */

$ar13 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$ar14 = [ 'K' => 2'L' => 8'M' => 18'N' => 32];

is_identity($ar13$ar14);

?> 

 RESULT   

( array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } === array(3) { ["O"]=> int(32) ["P"]=> int(18) ["Q"]=> int(8) } )

(FALSE) The ARRAYS DOES NOT have the SAME key/value pairs.

( array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } === array(4) { ["K"]=> int(2) ["N"]=> int(32) ["L"]=> int(8) ["M"]=> int(18) } )

(FALSE) The ARRAYS DOES NOT have the SAME key/value pairs.


( array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } === array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } )

(TRUE) The ARRAYS have the SAME key/value pairs.
In the SAME order!


  14 EXERCISE   

<?php

function is_nonidentity($par1$par2)
{
if((
$par1 !== $par2) == TRUE)
{
echo 
'( ';
var_dump($par1);
echo 
' !== ';
var_dump($par2);
echo 
' )<br><br>(TRUE) The ARRAYS DOES NOT have the 
SAME key/value pairs.<br><br>'
;    
}
else
{
echo 
'( ';
var_dump($par1);
echo 
' !== ';
var_dump($par2);
echo 
' )<br><br>(FALSE) The ARRAYS have the 
SAME key/value pairs.<br>In the SAME order!<br><br>'
;    
}
}

/* - - - - - - - - - - - - - - - - - - - - - - - 

Array Operators

IDENTITY

The key/value pairs of
$ar15 and $ar16 have 
different values.

- - - - - - - - - - - - - - - - - - - - - - - */ 

$ar15 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$ar16 = [ 'O' => 32'P' => 18'Q' => ];

is_nonidentity($ar15$ar16);

/* - - - - - - - - - - - - - - - - - - - - - - - 

Array Operators

NON-IDENTITY

The key/value pairs of
$ar17 and $ar18 have 
the SAME values,
however, in different order

- - - - - - - - - - - - - - - - - - - - - - - */ 

$ar17 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$ar18 = [ 'K' => 2'N' => 32'L' => 8'M' => 18 ];

is_nonidentity($ar17$ar18);

/* - - - - - - - - - - - - - - - - - - - - - -

Array Operators

NON-IDENTITY      

The key/value pairs of
$ar19 and $ar20 have 
the same values,
in the SAME order.      

- - - - - - - - - - - - - - - - - - - - - - */

$ar19 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$ar20 = [ 'K' => 2'L' => 8'M' => 18'N' => 32];

is_nonidentity($ar19$ar20);

?> 

 RESULT   


( array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } !== array(3) { ["O"]=> int(32) ["P"]=> int(18) ["Q"]=> int(8) } )

(TRUE) The ARRAYS DOES NOT have the SAME key/value pairs.

( array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } !== array(4) { ["K"]=> int(2) ["N"]=> int(32) ["L"]=> int(8) ["M"]=> int(18) } )

(TRUE) The ARRAYS DOES NOT have the SAME key/value pairs.

( array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } !== array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) } )

(FALSE) The ARRAYS have the SAME key/value pairs.
In the SAME order!


  15 EXERCISE   

<?php

/* - - - - - - - - - - - - - -

ARRAY Operators

 - - - - - - - - - - - - - - -*/

$arr15 = [ 'K' => 2'L' => 8'M' => 18'N' => 32 ];

$arr16 = [ 'O' => 32'P' => 18'Q' => ];

$narr15 =& $arr15;

$narr16 =& $arr16;

echo 
'REFERENCE TO THE VALUE OF<br>$arr15 = ';
var_dump($arr15);
echo 
'<br>is<br>$narr15 = ';
var_dump($narr15);

echo 
'<br><br>REFERENCE TO THE VALUE OF<br>$arr16 = ';
var_dump($arr16);
echo 
'<br>is<br>$narr16 = ';
var_dump($narr16);

?> 

 RESULT   

REFERENCE TO THE VALUE OF
$arr15 = array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) }
is
$narr15 = array(4) { ["K"]=> int(2) ["L"]=> int(8) ["M"]=> int(18) ["N"]=> int(32) }

REFERENCE TO THE VALUE OF
$arr16 = array(3) { ["O"]=> int(32) ["P"]=> int(18) ["Q"]=> int(8) }
is
$narr16 = array(3) { ["O"]=> int(32) ["P"]=> int(18) ["Q"]=> int(8) }


  16 EXERCISE   

<?php

/* - - - - - - - - - - - - - -

ARRAY Operators

 - - - - - - - - - - - - - - -*/

function listarr($arr)
{
    
var_dump($arr);
    echo 
'<br><br>';
}

$here = [ 'red''green'];

$arr15b = ['blue''yellow''black'];

$arr15 = [...$here'blue''yellow''black'];


listarr($here);

echo 
'<br>';

listarr($arr15b);

echo 
'<br>';

listarr($arr15);

?>

 RESULT   

$here = array(2) { [0]=> string(3) "red" [1]=> string(5) "green" }


$arr15b = array(3) { [0]=> string(4) "blue" [1]=> string(6) "yellow" [2]=> string(5) "black" }



The fisrt ARRAY was introduced in the indicated position, in the second ARRAY, resulting in this third ARRAY:

array(5) { [0]=> string(3) "red" [1]=> string(5) "green" [2]=> string(4) "blue" [3]=> string(6) "yellow" [4]=> string(5) "black" }


  17 EXERCISE   

<?php

/* - - - - - - - - - - - - - -

ARRAY Operators

 - - - - - - - - - - - - - - -*/

$data = [ 'title' => 'Lusiadas'
            
'subject' => 'An epic poem about the 
                         Portuguese language, published in 1572.' 
];

$data'author' ] ??= 'Luiz Vaz de Camões';

var_dump($data);

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

  Null Coalescing Assignment Operator checks 
  if $author is NULL, if so it assign 
  string(19) "Luiz Vaz de Camões" to it.
  
  Best benefit of 
  Null Coalescing Assignment Operator
  is when used for array element.

  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

?>

 RESULT   

array(3) { ["title"]=> string(8) "Lusiadas" ["subject"]=> string(80) "An epic poem about the Portuguese language, published in 1572." ["author"]=> string(19) "Luiz Vaz de Camões" }

  18 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

ARRAY Operators

The same result as the previous exercise, 
solved in another way.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

function DefaultValue()
{
return 
'Luiz Vaz de Camões';
}

$data = [ 'title' => 'Lusiadas'
'subject' => 'An epic poem about the 
Portuguese language, published in 1572.' 
];

if(!isset(
$data['author'] ))
{
$data['author'] = DefaultValue();
}

$data['author'] ??= DefaultValue();

var_dump($data);

?> 

 RESULT   

array(3) { ["title"]=> string(8) "Lusiadas" ["subject"]=> string(80) "An epic poem about the Portuguese language, published in 1572." ["author"]=> string(19) "Luiz Vaz de Camões" }

  19 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - - - - - - - -

ARRAY Operators

A single
USER-DEFINED 
spacesharr comparison funtion

- - - - - - - - - - - - - - - - - - - - - - - */

function spacesharr($par1$par2)
{

if((
$par1 <=> $par2) == 0)
{
echo 
'The two ARRAYS are the same:<br>';
var_dump($par1); echo ' = 'var_dump($par2); 
echo 
'<br><br>';
}

elseif((
$par1 <=> $par2) == -1)
{
echo 
'The first ARRAY is smaller than the second:<br>';
var_dump($par1); echo ' &lt; 'var_dump($par2); 
echo 
'<br><br>';
}
elseif((
$par1 <=> $par2) == 1)
{
echo 
'The first ARRAY is bigger than the second:<br>';
var_dump($par1); echo ' &gt; 'var_dump($par2); 
echo 
'<br><br>';
}

}

$arr18a = [ 3];
$arr18b = [ 2];
$arr18c = [ [ 45], [ 78] ];
$arr18d = [ [ 78], [ 45] ];
$arr18e = [ [ 78], [ 45] ];

spacesharr($arr18a$arr18b);

spacesharr($arr18a$arr18c);

spacesharr($arr18c$arr18a);

spacesharr($arr18d$arr18a);

spacesharr($arr18c$arr18d);

spacesharr($arr18d$arr18e);

?> 

 RESULT   

The first ARRAY is bigger than the second:
array(2) { [0]=> int(3) [1]=> int(2) } > array(2) { [0]=> int(2) [1]=> int(3) }

The first ARRAY is smaller than the second:
array(2) { [0]=> int(3) [1]=> int(2) } < array(2) { [0]=> array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) } [1]=> array(3) { [0]=> int(7) [1]=> int(8) [2]=> int(9) } }

The first ARRAY is bigger than the second:
array(2) { [0]=> array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) } [1]=> array(3) { [0]=> int(7) [1]=> int(8) [2]=> int(9) } } > array(2) { [0]=> int(3) [1]=> int(2) }

The first ARRAY is bigger than the second:
array(2) { [0]=> array(3) { [0]=> int(7) [1]=> int(8) [2]=> int(9) } [1]=> array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) } } > array(2) { [0]=> int(3) [1]=> int(2) }

The first ARRAY is smaller than the second:
array(2) { [0]=> array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) } [1]=> array(3) { [0]=> int(7) [1]=> int(8) [2]=> int(9) } } < array(2) { [0]=> array(3) { [0]=> int(7) [1]=> int(8) [2]=> int(9) } [1]=> array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) } }

The two ARRAYS are the same:
array(2) { [0]=> array(3) { [0]=> int(7) [1]=> int(8) [2]=> int(9) } [1]=> array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) } } = array(2) { [0]=> array(3) { [0]=> int(7) [1]=> int(8) [2]=> int(9) } [1]=> array(3) { [0]=> int(4) [1]=> int(5) [2]=> int(6) } }