PHP Operators

What can you do without operators? Not much, if you’re trying to do without them in a programming language, and PHP is no exception. On Monday, we barely had time to show you a long list of the operators in PHP. Today, we’re going to show you what they do.

In our last tutorial I left you with a giant table full of operators to study. But I know you, and you are a slacker, so instead of making you go back to that previous article like I should, I am going to spot you one and post it here for you again. That and well…it helps increase my word count. All hail laziness!

 

Symbol

What it Does

Type

+

Used for Addition

Arithmetic

-

Used for Subtraction

Arithmetic

*

Used for Multiplication

Arithmetic

/

Used for Dividing

Arithmetic

%

Used for Modulation

Arithmetic

++

Used to increase value by one

Arithmetic

Used to decrease value by one

Arithmetic

“=”

Used to Assign a Value

Assignment

+=

Used to Add and Assign a Value

Assignment

-=

Used to Subtract and Assign a Value

Assignment

*=

Used to Multiply and Assign a Value

Assignment

/=

Used to Divide and Assign a Value

Assignment

.=

String Concatenation

Assignment

%=

Used to Modulate and Add a Value

Assignment

“==”

Equal To

Comparison

!=

Not Equal To

Comparison

>

Greater Than

Comparison

<

Less Than

Comparison

>=

Greater Than or Equal To

Comparison

<=

Less Than or Equal To

Comparison

&&

This is the Logical (AND) Operator that Returns a True Value if All Expressions Being Checked Are True

Logical

||

This is the Logical (OR) Operator that Returns a True Value if Any of the Expressions Being Checked Are True

Logical

!

This is the Logical (NOT) Operator that Returns a True Value if the Expression Being Checked Is Not True

Logical

So there you go. No, you aren’t having deja vu. That’s just a glitch in the Matrix. Like the third part in that movie series. I mean, way to ruin a trilogy. Let’s create a third movie and then have the main characters, who have been the focus of the previous two movies, only be in the final installment for like 20 minutes. Don’t get me started.

Okay where was I?

{mospagebreak title=Assignment Operators}

We’ve already worked briefly with one of the assignment operators. Their basic function is to assign a value to a variable.


<html>

<body>


<?php

$example_variable = “An example”;

$number_variable = 1;


?>


</body>

</html>

Spark your memory? In addition to using the = operator, you can also add a value to your variable and reassign it the new value. Like this:


<html>

<body>


<?php

$initial_value = 10;

$initial_value +=;


?>


</body>

</html>

In the above example the variable $initial_value is set to 10. Then we take the value of $initial_value and add it to itself. The line $initial_value += is the same as writing $initial_value + $initial_value. Since the value is originally set at 10, the value is now 20.

It works the same with the rest of the assignment operators. If you had used the *= operator, it would have multiplied $initial_value by itself and resulted in the new value of 100.

{mospagebreak title=Arithmetic Operators}

You have been using most of the arithmetic operators since childhood. That and your fingers and toes. But don’t worry; PHP is smarter than you. It’s prettier than you. And according to some of the other programming languages, it really gets around. ASP is really jealous, but don’t tell it I said so.

Let’s look at some examples.


<html>

<body>


<?php


$add = 1 + 0;

$subtract = 2 – 1;

$multiply = 1 * 1;

$division = 2 / 2;

$modulus = 5 % 2;


echo $add;

echo $subtract;

echo $multiply;

echo $division;

echo $modulus;


?>


</body>

</html>

The above code would print out all the values of our variables:


  1

  1

  1

  1

  1

A Brief Note About My Dear Friend…The Modulus

The majority of the tutorials I’ve read on PHP never really explained Modulus in simple terms and so I never knew what it did. Maybe I’m just an idiot. But if there are other idiots out there, then you are in luck, because I am going to put this in very simple terms. Modulus gives you the remainder in division. Period. If I divide 2 / 2, there is no remainder. If I divide 3 /7 there is a remainder of 1. Modulus would return that 1.

{mospagebreak title=Incremental Operators}

Sometimes you want to increase or decrease the value of a variable by one. This is especially useful when using Loops, which we will cover in another tutorial. Here is an example of how you would work with them:


<html>

<body>


<?php


$value=9;


echo $value;

echo ++$value;


?>


</body>

</html>

The above code results in this print-out:

  9

  10

When the ++ appears before the variable it adds a one to it. If a –- appears before a variable it subtracts one from it.

Let’s make it a little trickier. Consider the following code:


<html>

<body>


<?php


$value=9;


echo $value;

echo $value++;


?>


</body>

</html>

This code would result in:

  9

  9

That is because when we place the ++ after the variable name, it increments the value after the line of code is executed.


<html>

<body>


<?php


$value=9;


echo $value;

echo $value++; // increments the value by one after the line

echo $value; // prints the new value, which is now ten


?>


</body>

</html>

This code prints:

  9

  9

  10

{mospagebreak title=Better by Comparison}

People love to compare things. Is this better than that? Are you fatter than me? Am I just as ugly as you are? Is Picard Greater than or Equal to Kirk? I’ll tell you right now, Cisco was greater than Kirk and Picard combined. Deep Space 9 FTW.

Well, in PHP you can compare all day long with the help of Comparison Operators. They allow you to compare two or more values and then have the program respond appropriately. We will cover these more during the upcoming tutorial on Statements, but here is how they look in code:


<html>

<body>


<?php


$my_name = “Mr. T”;


if ( $my_name == “Mr. T”) {

echo “Your name is really Mr. T? I pity you fool!”;

}


?>


</body>

</html>

In the above example, we assigned the value “Mr. T” to the variable $my_name. We then made a program that stated if the value of $my_name was indeed “Mr. T” then print “Your name is really Mr. T? I pity you fool!”. If the value of $my_name had been anything else, the line would not have printed.

Note that we used the == to compare the value of $my_name to our criteria (Mr. T).

In the following code we will use the not equal to operator (!=).


<html>

<body>


<?php


$my_name = “Charlie Brown”;


if ( $my_name != “Mr. T”) {

echo “Your name is not Mr. T? Then quit that jibba-jabba!”;

}


?>


</body>

</html>

The code above checks to see if the value of $my_name is “Mr. T”. Since it isn’t, it prints out the line:

 

  You name is not Mr. T? Then quit that jibba-jabba!

If the value had been “Mr. T”, then nothing would have occurred.

If it is still a little confusing, have no fear. It’ll be clear as mud after the next few tutorials.

{mospagebreak title=Logical Operators}

Logical operators are used to help test one or more criteria. The && operator says: if this and this are true, then do this. The || operator says: if this criteria OR that criteria is true, then do this. And finally the ! operator checks to see if a value is NOT true.

Oops I Did It Again

One thing I neglected to cover in the previous article was the use of quotes when using the echo function. This caused me to be beaten with many whips and locked in the basement with only episodes of Ugly Betty to keep me company. I know. I still haven’t recovered.

When mixing HTML and PHP together be sure to follow these rules:


  • Do not use quotes inside of a string

  • Use the backslash to escape your quotes within a string

  • Use the single quote

  • Never feed them after midnight

Here is an example:


<html>

<body>


<?php


echo “<h1 class=”yourH1”>Hello Fatty Boom Balatty</h1>;

echo “<h1 class=’yourH1′>Hello Fatty Boom Balatty</h1>


?>


</body>

</html>

Both of the above are good examples of how to use quotes when mixing PHP and HTML.

Well, we covered a lot of ground this episode. Hopefully you have a decent grasp of how operators work. Don’t worry if you aren’t a master of them yet; that comes with practice.

In our next tutorial we will discuss the various Statements available in PHP and go more in-depth on the comparison and logical operators. So check back in. You might learn something.

Till then…

Google+ Comments

Google+ Comments