PHP For Beginners: Part III
If you haven’t seen the first two posts for this series, be sure to check them out below:
This exercise assumes that you either have PHP installed on your computer already or that you have access to a server with PHP support.
Some might call it an absence, I prefer to call it a long nap. Yes, I’m back, bringing with me part three of my PHP tutorial line! Follow the program throughout the summer as I provide you at least weekly updates on this versatile language of Web 2.0.
Lets get down and dirty. This lesson will cover the following:
- More operators
- Concatenation operator
- Combined assignment operators
- Comparison operators
- Incrementing and Decrementing
- Operator precedence
Arithmetic Operators were discussed in part two of this series, but I’m sorry to say that was just the surface. Operators are necessary to manipulate information and data values to create a result desirable to the author. Thus, the more operators, the more possibilities of the script, and thus the more objectives achieved with the code! These operators are fairly common among any programming language that you find, but their unique use in PHP is what helps make it such a strong language.
We’ll start out simple. The concatenation operator is a single period (.) that essentially binds together two operands (remember, these are expressions or strings, basically values of data or information). This may not seem important now, but this operator is important in combining the results of an expression with some kind of string. Here’s an example:
$centimeters=245; print “The width is “.($centimeters/100).” meters”;
Here I used a variable then printed it within a string operand. This script will return the output “The width is 2.45 meters”.
Combined assignment operators
Compacting code is important in a website because it means that you have fewer things to sift through when troubleshooting. Shorter code also means a smaller file size, and if you intend to enter into a serious programming profession then that is a huge plus.
The name “combined assignment operators” is pretty much the exact description of this entire section. Basically, we combine the functions of two operators into one in an effort to simplify the code. Below is an example:
$x=4; $x=$x+4; //$x now equals 8 $x+=4; //$x now equals 12 by the combined operator
Here is a table detailing just a few of the basic combined operators:
Combined Assignment Operators
Pretty simple so far right? Well here’s where one of the biggest parts of PHP comes into play: comparison operators. Comparison operators perform tests on their operands, returning the boolean result true or false. This is again important for relating various pieces of information to one another from the user input.
We’ll just be getting familiar with them for right now; we’ve already covered a lot this lesson and this is a very important topic to fully comprehend. For right now, it is important to understand that these values are equivalent to true or false; they do not actually return the boolean value. For example:
$x=4; $x<9; // equivalent to the value true $x>23; // equivalent to the value false
Below is a table of comparison operators. We will learn more about these in complex coding sequences next lesson.
Incrementing and Decrementing
Incrementing and decrementing, which is increasing or decreasing by one, respectively, is a handy little ability when it comes to integrating loops into your code. For example, say that you want the script to execute a certain operation eight times; you can’t simply say “do this eight times.” Instead, you must say “start at one, then at the conclusion of each execution, add one to this number. When the number reaches eight, stop executing the function.” This method is seen in almost every programming language so mastery of this is crucial in order to learn other languages with ease.
This tutorial guide has already showed you two ways to increment and decrement a variable: the arithmetic operator and the combined operator. Below are examples of this:
$x=$x+1; // incremented by 1 $x-=1; // decremented by 1
Because changing a variable by the value of one is so important, however, the whole operation has received its own operator! Below are the respective operators for incrementation:
$x++; // $x is incremented $x--; // $x is decremented
Putting them together into a little test code, we receive the following:
$x=3; $x++<4; // true
But hold on, if we refer to our comparison operators, we’ll see that the < sign means only less than. How can this function return true then? It’s true because the variable x is evaluated in the function BEFORE being incremented. THIS IS VERY IMPORTANT! In order to increment the variable before evaluation, you must do the following:
++$x; // incremented --$x; // decremented
Now to replace it in the above code:
$x=3; ++$x<4; // false
The above argument brings us into operator precedence, or what most people will be familiar with, what I like to call “order of operations.” Certain operators have precedence over one another in this wonderful language PHP. Below is an example:
4+5*2 // returns 14, multiple first (4+5)*2 // returns 18, parentheses first
This is a pretty straightforward concept that you just need to familiarize yourself with. Beyond that, this table is just for basic reference:
Order of Precedence for Selected Operators
I know that we have not yet touched numbers 9, 10, or 11, but hold on to your knickers because they’ll be there next lesson!
School is fun, especially in the summer! That’s why I’ve included this quiz. That, and to help you retain your knowledge of what we’ve just learned. Don’t peak back!
#1: Which of the following will increase $x by 2?
- $x + (3/2)
- 4*2/6 + $x
#2: 4*9+9+(4/1) will return the following:
#3: 4.0===4 will return the value TRUE.
- Not enough information
#4: The expression $x*=2 is equal to:
#5: Without looking, which operator is executed first from this list?
- + -
Answer key: A,A,B,C,A
That’s all folks! We’ve taken some great steps today, and hopefully we will take many more in my next tutorial, PHP for Beginners: Part IV.