MC Press Online

Saturday, Jun 24th

Last updateFri, 23 Jun 2017 1pm

You are here: Home ARTICLES Programming Web Languages Weird, Simple Stuff in PHP: Operators

Programming / Web Languages

Weird, Simple Stuff in PHP: Operators

Support MC Press - Visit Our Sponsors


Flexible Input, Dazzling Output with IBM i



Click for this Month's

Bookstore Special Deals

Yeah, they're sort of the same as in RPG…but not really.


Over the last few articles, I have been on a PHP kick, talking about the language in general and how it integrates into the IBM i in particular. The next step, in the twisted overgrown forest that is my mind, is to take a look at some specific things in PHP that are different from what we're used to in RPG. Not a tutorial, but maybe an appendix to the tutorial or book you might be using if you're trying to make that jump from RPG to PHP. And the place I want to start is to talk about PHP operators.

The First Weird Thing: Extra Operators

If you're coming to PHP from RPG, one of the first things you notice is that there are a lot more operators or symbols associated with PHP than there are with RPG. And this can be scary. So we'll start by looking at what some of these are.


Most of us are familiar with just a few basic operators: = (meaning that two things are identical to each other or one is being set identical to another), and the basic math functions of +, -, x, /. But PHP has a fair number of different symbols that it uses as operators, and the first step in being able to read PHP code is to be able to read the operators and symbols. They're the conjunctions and prepositions of the PHP language. Not as important as the nouns and verbs perhaps, but it is almost impossible to read code without them.


Most PHP texts identify them piecemeal throughout the text. I guess that makes sense, but I always wished there was one master list, and unless somebody acts pretty quickly to stop me, I'm going to give you one right here.


Quotation Marks

I've never thought of quotation marks as "troublemakers," but here they are right off the bat. While in RPG we generally don't differentiate between single and double quote marks (other than being careful how we mix them when they're both present in a statement), the two different marks mean two different things in PHP.


Single quotes are used to enclose a literal, just like in RPG. Something like this:


$var = '1234567890';                

$var = 'My name is $name';


In this case, if you wanted to display the value of the variable $var, the first time it would be '1234567890', and the second time it would be 'My name is $name'.


Note: All PHP variables begin with $; we'll talk more about that later.


Double quotes, however, are used if you want to embed a variable in a string and have that variable value show up when the string is displayed. The following would yield the string "My name is Dave".



$name = 'Dave';

$var = "My name is $name";


Equal Sign

And then, there are the equal signs. RPG uses only the single equal sign (=) to show identity. PHP is more specific, and there are actually three symbols that use the equal sign: =, ==, ===.


A single equal sign is the "assignment operator." That is, it means you are assigning a value to a variable. For example:


      $val = 6;

      $string = 'mate';


Now this may seem ridiculously obvious to most of you, and that's good. By the time we get toward the end of this, we'll be looking for things that are ridiculously easy.


Double Equal Sign  

Yeah, that would be an operator of ==. Don't have this in RPG.


The double equal sign (==) indicates that we're comparing two values, two variables, generally to determine if they have the same value. The output of this is a Boolean value of either True or False, which is ultimately how it works in RPG.


In RPG, this is typically done by the single equal sign (=), which covers both setting and identity. But not in PHP! So watch out for that until you get used to it. For example:


If ($var1 == $var2)     {

           echo 'the two variables are equal';




           echo 'the two variables are not equal';



Here, echo is a PHP operator that means display something on the console (the Web page you are currently working on).


Triple Equal Sign

The triple equal sign (===) is similar to the double equal sign except it means that not only are the two variables are equal in value, but they are also the same data type. We'll talk about data types when we talk about variables, but for the moment just remember that you could have two variables where the values were the same but one was one data type (say, integer) and the other was something else (say, floating point).


What's the bottom line here? When you see the single equal sign, you're looking at setting a value. It will appear when you're defining or redefining a variable. When you see the double and triple equal signs, it will be in an if statement.


Not Equal Sign  

Two symbols can be used for not equal: != and <>.


$var != 6;

$string <> 'mate';


And that's all there is to say.



What about AND and OR, you say? Shoot, we couldn't say nothing about two little fellas who do more than any other operators to maximize program complexity and exercise our little logic brains.


Of course, the standard AND and OR operators can be found in PHP, but in here they have another format that gives a little twist to things. The AND condition can be signaled by AND or by &&. What's the difference? && has a higher precedence; it will be evaluated before an AND.


Similarly, the OR operator comes in two formats: OR and ||, where the || has a higher precedence than OR.


While it's nice to have four options to help us sort out those "if" statements, it's even nicer if you use brackets or parentheses to sort things out and keep all the precedence straight. Or maybe just write simpler functions.


There's also one more operator here that we should mention: the XOR, which basically means that a or b can be true, but not both. In other words, the following statement will be true if $var1 = 1 and $var2 = 1 or $var1 = 4 and $var2 = 2 but not if $var1 = 1 and $var2 = 2 or $var1 = 3 and $var2 = 3.


if ($var1 == 1 xor $var2 == 2)


Get it? Yeah, I would prefer not to run into a situation like that either.


Ternary Operator

The ternary operator is the question mark/colon (?:) combo where often there's a variable or expression separating the question mark from the colon.


In its full format, the ternary operator looks like this:


Expression1   ?   expression2   :   expression3  


Yeah, I know. Pretty weird. What this means is if expression 1 is true, then expression 2 is executed. If expression 1 is false, then expression3 is executed. In this way, it's sort of a shorthand form of an if-else statement.


There's also a second format:


Expression1 ?: expression3


In this case, if expression1 is true, then expression1 is executed. If expression1 is false, then execute expression3.


As you can see, the ternary operator is all about true and false, not about any other values. And this is something that is so PHP; the question of whether something is true or false. And so, you might use this a lot more than you actually think. What kinds of things would you be checking for? Well, maybe something like whether any records were returned from the server. You'll see this again.


Other Operators

Is that it? No, not completely. There are a few more, like the increment shorthands (+=, -+, ++=, etc.; => +1 = x is the same as x + 1 = x and stuff like that), but I don't like them. I like to see it spelled out for clarity.


The ones I showed you are the most important ones. Don't gloss over them. Look at them because while they're all pretty simple, what you want to do is really understand them so when you are looking over some PHP code they don't trip you up and get you all flustered. You never know what might trip you up. Might be something in your character.


And there are some other operators that are part of the OO world in PHP, and we'll get to those eventually. At least I suppose so. If I run out of simple topics to write about.


Next time? What, do I look like the kind of person who plans ahead? Really? Well, actually I am. But I'm also the kind of person who likes to keep things a secret. Stay tuned.


David Shirey

Dave Shirey is president of Shirey Consulting Services (, providing technical and business consulting services for the IBM i world. Among the services provided are IBM i technical support, including application design and programming services, ERP installation and support, and EDI setup and maintenance. With experience in a wide range of industries (food and beverage to electronics to hard manufacturing to drugs (the legal kind) to medical devices to fulfillment houses) and a wide range of business sizes served (from very large, like Fresh Express, to much smaller, like Labconco), SCS has the knowledge and experience to assist with your technical or business issues. You may contact Dave by email at or phone at 616 304 2466.