02
Sat, Nov
2 New Articles

Weird, Simple Stuff in PHP: Operators

Web Languages
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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';

     }

else

                                           {

           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.

 

AND/OR

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

David 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 This email address is being protected from spambots. You need JavaScript enabled to view it. or by phone at (616) 304-2466.


MC Press books written by David Shirey available now on the MC Press Bookstore.

21st Century RPG: /Free, ILE, and MVC 21st Century RPG: /Free, ILE, and MVC
Boost your productivity, modernize your applications, and upgrade your skills with these powerful coding methods.
List Price $69.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: