• About
  • Write For Us
  • Contact
  • Top 50 Photography Blogs
  • Resources
  • Forum

IndustryDev

  • Design
    • Photoshop
    • Lightroom
    • Camera Raw
    • Bridge
  • Development
    • HTML
    • CSS
    • Javascript
    • PHP
    • Dev Tools
    • WordPress
  • Photography
  • Blogging
  • Technology
  • Inspiration
You are here: Home / PHP / Operators in PHP

Operators in PHP

April 18, 2015

Operators in PHP

In PHP, operators are used to manipulate or perform operations on variables and values. And just like other programming languages, there are quite a few operators. These operators can be grouped under specific headings to help keep things a bit more organized. I’ll go over each group below. Before I begin though, I thought I’d cover the three major types of operators.

The first type of operator is called, “unary.” A unary operation is an operation with only one operand. Examples of a unary operator are the + or the – operators.

The second type of operator is called, “binary.” A binary operator consists of two unary operators combined as one. Examples of this are >= or the <=. The third type of operator is called, “ternary.” Ternary operators use three operators to accomplish its task. Examples of these are the === to test the equality and type of two values, while just the == tests the equality.

Groups of PHP Operators

We can arrange PHP operators into a few groups. These groups are as follows. I’ll go over some of these in more detail below.

Arithmetic operators – Used with numeric values to perform operations. Addition, subtraction, multiplication, division. (+, -, *, /, %, **)

Assignment operators – Used to assign values to variables. (=, +=, -=, *=, /=, %=, .=)

Comparison operators – Used to compare two values. (==, ===, !=, <>, !==, >, <, >=, <=) Increment/Decrement operators – Used to increment or decrement a variable’s value. (++, –)

Logical operators – Used when combining conditional statements. (and, or, xor, &&, ||, !)

Array operators – Used when comparing arrays. (+, ==, ===, !=, <>, !==)


String operators – Used to either concatenate two arguments or to concatenate and assign the right argument to the left. (., .=)

Playing With Operators in Code

In this section, I’m going to go over some examples of how operators are used in real code. While these are fairly simple examples, they’ll give a good idea of what these operators look like and how they can function.

Arithmetic & String Operators

When learning PHP, the first operators people go to are the arithmetic operators. These are straightforward and make for some mighty nice understanding. Let’s write some code.

<?php
$a = 50;
$b = 25;

$add = $a + $b;
$subt = $a - $b;
$mult = $a * $b;
$div = $a / $b;

echo $add . "<br>";
echo $subt . "<br>";
echo $mult . "<br>";
echo $div . "<br>";
?>


As you can see in the example above, the first things I did was to assign the values of 50 and 25 to the variables $a and $b respectively. After that, I performed some operations on those variables. First, I added them together. Then, I subtracted them. After that, I multiplied them and finally, I divided one into the other. In the last part of the code, I echoed out the results using the concatenation operator and some HTML for line breaks. In this case, the output would be:

75
25
1250
2

If you’re interested in running the above code yourself, you can do that with a tool I use over at Runnable.

Increment & Decrement Operators

The next area we’re going to look at is incrementing and decrementing. Check out the code below.

<?php
$a = 50;
$b = 25;

$add = $a + $b;
$subt = $a - $b;
$mult = $a * $b;
$div = $a / $b;

$add++;
$subt++;
$mult++;
$div++;

echo $add . "<br>";
echo $subt . "<br>";
echo $mult . "<br>";
echo $div . "<br>";
?>

I used the same code from the example above and simply added an auto-increment to each variable after its assignment. In this case, the output would be this:

76
26
1251
3

If I did the same thing, but added an auto-decrement to each variable like this:

<?php
$a = 50;
$b = 25;

$add = $a + $b;
$subt = $a - $b;
$mult = $a * $b;
$div = $a / $b;

$add--;
$subt--;
$mult--;
$div--;

echo $add . "<br>";
echo $subt . "<br>";
echo $mult . "<br>";
echo $div . "<br>";
?>

Our output would look like this:

74
24
1249
1

Comparison Operators

In this section, we’re going to go over some comparison operators. Before we begin though, I’d like to cover a function that I’m going to use to help out with some of the variables we’re going to compare. The function is called var_dump() and is used to display structured information (type and value) about one or more variables. It’ll become more clear below as to how exactly this is useful.

In the following code, I’m going to show you some comparison operators and after that, I’ll go over them.


<?php
$a = 50;
$b = 50;
$c = 75;
$d = "50";

var_dump( $a == $b ); // equal (value)
var_dump( $a != $b ); // not equal (value)
var_dump( $a === $b ); // identical (value and type)
var_dump( $a !== $d ); // not identical (value and type)

var_dump( $a < $b ); // less than
var_dump( $a > $b ); // greater than
var_dump( $a <= $b ); // less than or equal to
var_dump( $a <= $c ); // greater than or equal to
?>

If we run the above code, we’ll see the output is this (I’ll give an explanation next to each one):

bool(true) – a is equal to b
bool(false) – a is equal to b
bool(true) – a is identical to b
bool(true) – a is not equal to d

bool(false) – a is equal to b
bool(false) – a is equal to b
bool(true) – a is equal to b
bool(true) – a is less than c

As you can see, the var_dump() function is outputting the type of statement we’re running, along with the value of its output. In the first line above, the output is bool(true) because a is, in fact, equal to b. The second line is false because a is equal to b. It’s not not equal. The third line is true because a is identical in type and value to b, while in the fourth, the value is true because a is not equal in type and value to d.

In the second set, the first line is false because the question asks, is a less than b? a is equal to b, so this is false. On the second line, the same thing is happening – a is equal to b, so it can’t be greater than it at the same time. On the third line, since a is equal to b, the output is true and on the fourth line, we get a true because a is less than c.

Logical Operators

If we use the var_dump() function some more for testing, we can play around with some logical operators. In the code below, I’m going to test to see if a and b are true.

<?php
$a = TRUE;
$b = TRUE;

var_dump( $a and $b );
?>

The only way the output of this statement can be true is if both the values of the variables a and b are true as well. Since they are, the output we get is:


bool(true)

Now, if we run the following code:

<?php
$a = TRUE;
$b = FALSE;

var_dump( $a and $b );
?>

We’ll get this output:

bool(false)

Because the value of a and b are different. b is not false.

I’ll write some additional code below to give some examples of more logical operators. To mix things up a bit, I’ll give a short explanation right next to each statement.

<?php
$a = TRUE;
$b = FALSE;

var_dump( $a and $b ); // returns true if a AND b are true
var_dump( $a or $b ); // returns true if a OR b are true
var_dump( ! $a ); // returns true if a is NOT true
var_dump( $a && $b ); // returns true if a AND b are true
var_dump( $a || $b ); // returns true if a OR b are true
?>

The output of this code would be:

bool(false) – a and b are not true
bool(true) – a is true
bool(false) – a is true
bool(false) – a and b are not true
bool(true) – a is true


So there we have some coverage of various operators in PHP. I’m going to go through all of these in greater detail in the future. To keep tabs on this, take a look at my PHP category.

Related posts:

  1. The Syntax of JavaScript
  2. Conditional Statements & Loops in PHP
  3. PHP Data Types

Filed Under: PHP

What’s Next? Email Updates!

If you enjoyed reading this post, why not consider signing up to receive others like it by email? It's so easy and you can unsubscribe at any time.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Connect With Me

  • Facebook
  • Instagram
  • Pinterest
  • RSS
  • Twitter

MOST POPULAR POSTS

  • How to Set the Photo Quality in your Canon Rebel DSLR Camera Before participating in any type of photo shoot, it's i...
  • How to Adjust the Mouse Click & Scroll Settings in Windows 10 I's say this is one of the very first settings I ed...
  • How to Export Video From Adobe Photoshop When it comes to exporting and rendering video clips, t...
  • How to Apply an Adjustment to Only One Layer in Adobe Photoshop The answer is clipping. I'll tell you that right up fro...
  • How to Speed Up & Slow Down Video in Adobe Photoshop This is one of those posts that's going to be super hel...
  • Cutting Out a Shape From a Shape in Adobe Photoshop I've been using shapes for various things in Adobe Phot...
  • How to Set the Self Timer On Your Canon Rebel DSLR Camera Camera self timers are great. I was recently part of a...
  • Animating Scale, Rotation & Opacity in Adobe Photoshop I sat down a few days ago and started messing around in...
  • How to Set Your Canon Rebel Camera For Continuous Shooting Continuous Shooting mode is very important for those wh...
  • 3 Ways to Close Applications in Windows 10 This is going to be a very quick post because the topic...

Recent Comments

  • angelica blanco on How to Organize Video & Audio Project Files for Adobe Photoshop
  • pete salomone on Downloading Photos From a Digital Camera Using Adobe Bridge
  • cdn on How To Create a Slideshow For Your Lock Screen in Windows 10
  • Jay Gaulard on How to Set Your Canon Rebel Camera For Continuous Shooting
  • Deb on How to Set Your Canon Rebel Camera For Continuous Shooting

Copyright © 2021 · IndustryDev.com Web Design & Photography Blog · About · Write For Us · Contact · Privacy / Terms · Sitemap