1

PHP Basics – The Operators (Part 1)

Posted April 17th, 2009 in PHP, Programming and tagged , , , by Aaron

Coming into PHP fresh, you may notice a few things that don’t exactly line up with some of the other programming languages out there. In this initial “Operators” post, I am going to talk a little bit about the different operators that are available for people programming in PHP. There are different ways to compare and assign variables, as well as the reasons you might want to use each of these. It’s a good springboard or starting point for starting to understand PHP.


The Equals (=) Operator

The primary role of the equals operator I would consider is setting variables. The single equals role is to set the variable on the left, to the value on the right.

$foo = "bar";

This will set the variable named $foo to contain a string value of “bar”. Simple eh?

Well, now what happens if you double up your equals?

$foo == "bar";

It no longer will set the variable $foo equal to “bar”, instead what it is doing is comparing the variable on the left to the value on the right. So really, the above line of code is totally impractical, because really it isn’t doing anything. Lets write something that makes a little bit more sense.

if($foo == "bar") {
  echo $foo;
}

So the “IF Statement” above now is a better idea of exactly what is going on here. The statement is saying:

“IF $foo is set to ‘bar’, lets display the value of $foo on the page.”

The Double Equals expression doesn’t care what type of variable $foo is, as its moving through, which leads to the next point. The Triple Equals operator. This ones a little bit trickier and is very, very specific. In the following example, we are no longer going to use a string for the value of $foo, but instead use a number as a string and as an integer.

Example 1:
$foo = 1;

if($foo === 1) {
  echo $foo;
}

Example 2:
$foo = "1";

if($foo === 1) {
  echo $foo;
}

Example #1 will display the value of $foo, while Example #2 will not.

The two above samples are almost identical, except for the fact that the value of $foo in Example #2 is encased in quotes, telling it that the variable is a string instead of an integer. The Triple Equals Operator not only compares the values of the variable, but it also compares the type of variable being passed in. This is extremely useful when you have code that will only function against a specific type of variable.

One last thing, and this is veering off topic a little bit, but you can cast the variables in the above statement if your really want to use quotes.

$foo = (int) "1";

if($foo === 1) {
  echo $foo;
}

By placing the (int) in front of the value you are setting, you are casting that variable as a specific type. It’s useful when you want to specifically set a variable as well, but you might have some problems between variable type conversions if you’re always casting every variable.

But that’s a topic for another day, another post. Perhaps the next operator post, we will do some “not” functionality.

One Response so far.

  1. [...] our first section, we talked about the multiple ways that you can use the equals sign to assign variables or compare [...]

Leave a Reply