Categories: Development

Maths operators in PHP

Just like most languages you can use PHP to do Arithmetic equations like so:

echo (10 / 2);//5
echo (10 + 2);//12
echo (10 * 2);//20
echo (10 - 2);//8
echo (10 % 2);//0

$value = 50;
$value2 = 10;
echo ($value * $value2);//500
echo ($value - ($value + $value2));//10

To check if a value or variable is even use:

if (($var % 2) == 1)
{ echo "$var is odd." ;}
if (($a % 2) == 0)
{ echo "$var is even." ;}

To alternate a script output:

for ($i = 1; $i <= 10; $i++) {
  if(($i % 2) == 1)
    {echo "

$i is Even

";} else {echo "

$i is Odd

";} }

 

 

Share

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago