Beginner’s Guide to PHP – Part Three

I’ve had to restart this about 4 times now, because each time I decide to finish off, I get half way and somehow lose the file. I don’t know about l33t PHP Ninja; recently I’ve been l33t file loser.

Anyway, quick recap: in Part Two of the PHP Beginners Guide I briefly covered the basic mathematical operators, these allow us to do sums, and the echo statement. Although there’s more to the echo statement (including concatenation, echo vs. print, etc) I will touch on these later, possibly guide 4. This time we’ll be looking at comparison & logical operators, and the if construct.

Comparison Operators

It’s quite common in a PHP script to want to compare two things. In fact, I can’t think of any script I’ve created that doesn’t rely on some sort of comparison. The comparison operators are as follows:

==
Means: is equal to
For example: 5 is equal to 5 and will return true, but ‘hello world’ is not equal to ‘foo bar’
===
Means: is identical to (both value and type)
For example 5 is identical to 5 and will return true, but 5 is not identical to “5” (because PHP interprets “5” with quotation marks as a string, rather than an integer)
!=
Means: is not equal to
For example: 11 is not equal to 2.56 and will return true, but ‘string’ is equal to ‘string’
!==
Means: is not identical to (both value and type)
For example 5 is not identical to “5” or “five” and will return true, but 5 is identical to 5
>
Means: greater than
For example: 5 is greater than 1 and will return true
<
Means: less than
For example: 3 is less than 6 and will return true
>=
Means: greater than or equal to
For example: 5 is greater than 1 and will return true, 3 is equal to 3 and will return true, but 3 is not greater than 6
<=
Means: less than or equal to
For example: 3 is less than 6 and will return true, 2.4 is equal to 2.4 and will return true, but 21 is not less than 20

Comparison operators are most typically used within an if; that is, you would compare 2 items to come to a conclusion and display data or execute the next part of the script depending on the result.

if Construct (Statements)

The if construct — generally (and incorrectly, if you want to be anal) referred to as an if statement — executes depending on the result of the expression. A typical if statement looks like this:

if (expression)
	statement executed if result is true;

The basic premise is that the expression contains our ‘check’, or conditional, and if the result is a Boolean true, the statement or following block of code is executed. (We covered Boolean values in Beginners Guide Part One.) This is where the comparison operators come in – we use these to compare two things to get our true or false.

In Part Two, we had the following section of code:

<?php
	$name = "Jem";
	$age = 20 + 1;
?>

We can adapt this to include our if. As I’m now 22 (a PHP guide per year?!) we can conveniently use a comparison operator to see if our age is correct:

<?php
	$name = "Jem";
	$age = 20 + 1;
	
	if ($age == 22)
		echo "The age variable contains the correct value";
?>

Except we know that $age is 21. To prevent an empty page from being returned, and to give our script user (you) a clue as to what’s going on, we use an else statement. else extends the basic functionality of an if, and will execute if the result of the if condition is false. For example:

<?php
	$name = "Jem";
	$age = 20 + 1;
	
	if ($age == 22)
		echo "The age variable contains the correct value";
	else
		echo "The age variable contains the wrong value!";
?>

As we already know $age is 21, we should be able to easily guess that the page will now show “The age variable contains the wrong value!“.

In my examples, I’ve only used a single echo statement after the if/else check, and therefore haven’t used curly braces to separate the blocks/groups of code. If you wish to execute more than one statement, or feel you need the curly braces to visually distinguish between groups, the previous code example would look like this:

<?php
	$name = "Jem";
	$age = 20 + 1;
	
	if ($age == 22) {
		echo "The age variable contains the correct value";
	} else {
		echo "The age variable contains the wrong value!";
	}
?>

Logical Operators

The three most basic logical operators, AND (&&), OR (||) and NOT (!) are also used in if statements to alter the expression and change the condition on which the statement is executed.

The NOT operator is often used before a function to reverse the result. For example, you can use is_numeric() to check whether or not a variable is numeric, thus we could place ! before it to reverse the purpose of the function (to check if it is not numeric):

<?php
	$age = "heehee, we've changed this variable to a string";
	
	if (!is_numeric($age))
		echo "Uh-oh, who changed my variable? :( ";
?>

As $age is now a string, we’d see “Uh-oh, who changed my variable? :( ” on the page.

AND and OR are used to check for multiple conditions. Example:

<?php
	$name = "Jem";
	$age = 20 + 1;
	
	if ($name == "Jem" && $age == 22) {
		echo "Our variables are both correct :) ";
	} else {
		echo "Our variables contain incorrect data :( ";
	}
?>

Can you figure out which statement will be echo’ed on screen?

Next guide topic – err, haven’t decided yet. Keep your eyes peeled and I’ll try and get it up before the end of the year this time. (I know, I said that before.)

php beginner's guideThis article is part of the Beginner’s Guide to PHP series. You can see all the guides or jump to Part Four.
affiliate window advert

4 comments so far

  1. Beginner’s Guide to PHP - Part Four — jemjabella.co.uk said:
    On June 16, 2008 at 5:03 pm

    […] Part Three of the Beginner’s Guide to PHP we learnt about the if construct (”statement”), and using it to check the value of some […]