6 Things Your Daddy Never Told You About PHP Programming

When I first started messing around with WordPress programming, I was a complete PHP novice. It was a very frustrating ordeal, but if you’re in the middle of it you don’t need me to tell you that.

If you can relate with this, I’d like to offer you a ray of hope by sharing six simple PHP rules that are suspiciously absent from most PHP tutorials and instruction books:

  1. The Difference Between the PHP “=” Operator and the “==” Operator

Ever wondered why your booleons are being ignored or not functioning properly? It might be because “=” and “==” have a completely different function. The single character (=) is an assignment operator, while the double character is a comparison check.

For example, this…

$variable = 7;

…means I want $variable to be equal to the number seven. However, if I want to check and see if $variable is equal to 7, I’d use two equal signs:

if($variable==7) {

//code to be executed if variable is true//

}

This can be tricky if you’ve been writing if statements like the one below and wondering why your code is executing based on the assumption that “$variable” is true, even when it’s clearly false:

If($variable=TRUE) {

//code to be executed if variable is true//

}

  1. The Difference Between || and “OR” in PHP

Several published books on PHP programming say that “||” and “or” are interchangeable, but this is a half truth. What I learned the hard way was that “or” is a “weaker” operator while “||” takes precedent. In cases where you have one simple booleon such as the one below, using “or” might not cause any problems:

if($variable == TRUE or $variable_two == TRUE) {

//code to be executed//

}

You might also get away with enclosing your “or” comparisons in parentheses to be clear about what you want. But once you start trying to code long, complicated booleons which contain multiple cases of “or,” “and” or “xor,” you’re better off opting for the “||” operator.

  1. The Difference Between && and “and” in PHP

The difference between “&&” and “and” is comparable to the difference between “||” and “or,” meaning that the “&&” takes precedent. Combine this with the difference between “||” and “or” and you can end up with a mess when writing long conditional statements.

I’ve learned to simply use the “||” and the “&&” exclusively in order to avoid confusion.

  1. The Difference Between the Single and Double Quotes in PHP

This one confounded me for months before I stumbled across a gem of an insight which explained the difference between double and single quotes. The single quotes are used for literal assignments such as the one below:

$data = ‘A PHP variable needs a $ in front like this: $variable’;

echo $data;

So the example above would echo:

A PHP variable needs a $ in front like this: $variable.

However, if I use the double quotes, PHP would attempt to evaluate “$variable.” So the following code:

$variable = “example”;

echo “The double quotes cause PHP to evaluate the variable as in this $variable”;

…would echo out like this:

The double quotes cause PHP to evaluate the variable as in this example

Knowing this will make it much easier for you to work between html and PHP, and save you a lot of those “unexpected T_” errors.

  1. The Difference Between the print and echo commands

My first year in working with WordPress and PHP, I didn’t know there was a difference between print and echo. It’s a subtle difference, but print is actually more useful for printing out text or html in between pieces of executable PHP script. As long as you’re echoing out very simple data, this usually doesn’t matter.

But once you get into more complicated strings which would require multiple uses of the echo command, you’ll start getting parse errors. That’s where you need the print command, which can be used in between functions or comparison checks like this:

$variable? print “Success” : print “Failure”;

  1. The Difference Between “++$number” and “$number++”

The increment “++” operator is used a lot in PHP, but it has a different function depending on whether it’s before or after the variable. For example, the code below would first test if $number equaled ten (which it would) and then print out the number 11, not the number 10.

$number=10;

if($number++  == 10) print $number;

This is because putting the “++” before “$number, you’re telling PHP to add one after the variable has been testing using the “==” operator. However, the code below would whether if $number equaled 10 (which it would) and then print the number 9.

$number=9;

if(++$number  == 10) echo $number;

This is because putting the “++” before the variable causes it to increment before the comparison is made. The same rule is true for the placement of the decrement “- – “ operator.

That’s all I’ve got for now, but if anyone has questions please leave them below. Sometimes, you “don’t know what you don’t know” and it’s easier to ask someone than to suffer through epic ordeals of trial and error.

Hope I’ve saved you a few grey hairs ;-)

Tags

Comments (1)

  1. Not sure I understand or agree with your assessment of the differences between echo and print. As far as I know, the difference is “echo” returns void, and “print” returns 1. Therefore you can use “print” inside of a comparison expression and it will return true. Echo is used 99% of the time, because it has a slight performance advantage over print.

    The same line of code you gave as an example can be recreated with an echo statement as well (with one less word)…

    echo ($variable) ? “Success” : “Failure”;

    Also, for some bonus content, it might be worth mentioning that JavaScript does not use the “AND” and “OR” comparison operators, only the “&&” and “||” versions. I know this is a post about PHP, but I think it is worth mentioning, since the two languages are used side-by-side so often, and might sway a developer to inherit those flavors of the comparison operator over the latter.

    BTW, WordPress has omitted their preference for which operator to use. It’s always nice to look at their Coding Standards when deciding how to structure your PHP code. They have guidelines for when you should use single and double-quotes, and so on.

Participate