PHP - If Statements and Variables
In PHP and other programming languages you don’t want all the code to execute unless a specific value has been reached or is set. To get this functionality we use “If” statements. If statements are a way to check and compare variables to see if they are set to values you want to process code. For instance, if a user is logged into a site, that is when you grab their account information. If the user is not logged into the site, there is no need to grab account information. This is applied to all aspects of the code.
First thing we need to do is create our PHP file again and set some variables. We will add onto this code throughout this tutorial.
<?php
$age = 20;
echo $age;
?>
This simple PHP file just sets a variable “age” to 20 then shows it on the screen. But what if we only care about if the person is 18 or older, or under 18?
<?php
$age = 20;
if($age >= 18) {
echo '18 or Older';
}
if($age < 18) {
echo 'Younger than 18';
}
?>
We run two if
statements to see if 18 or older, and then to see if
they are under 18 and if they are we echo different results. If you
don’t understand the format of the “if” statement it goes something
like this.
First you specify it to run if()
. Inside the parentheses we add our
request. You have 2 variables a left and a right, and compare them
using the possible operations below.
Left Variable is EQUAL to Right Variable - ==
Left Variable is GREATER than Right Variable - >
Left Variable is GREATER than or EQUAL to Right Variable - >=
Left Variable is LESS than Right Variable - <
Left Variable Is LESS than or EQUAL to Right Variable - <=
There is another operation but we will get into that later when working with booleans (true or false). All of these operations also allow for “!” in front of them to mean “NOT”. This lets you check if the operation is NOT true. After the closing parentheses we add a curly bracket opening and closing. Inside the curly brackets we add the code that only executes when the “if” statement is true.
In the code about the age check, the “Over 18” will be printed out since the age is set to 20, which is over 18. If we set it to 17 we will get a different result though. But the above code is bad because we we checking the same variable twice and it only has two possible options in our current case, either 18 or older, or under 18. We should use the “else” command instead.
<?php
$age = 20;
if($age >= 18) {
echo '18 or Older';
} else {
echo 'Younger than 18';
}
?>
In this code, if they are not >=
18, “else” will run without having
to check any variables. The “else” statement is setup similar to “if”
in the sense it has brackets in which only that code will execute if it
is true. You don’t have to set up variables to compare since we already
did that in the first if
statement. Else statements have to be linked
to an “if” statement like the one above, right after the “if” closing
curly bracket you need to place the “else” statement.
Let us look at another example just to show more “if” statements being used.
<?php
$age = 18;
$approved = 1;
if($age >= 18) {
if($approved == 1) {
echo 'Allowed To Access Site';
}else{
echo 'Not Approved';
}
}else{
echo 'Not 18 or Older';
}
?>
We have multiple statement here, and they are nested instead of each other. We first check if they are 18 or older, if they are we then check if they are approved (when approved equals 1, not 0). Each statement has an “else” statement connected to it showing the user a “status” as to why they can’t access the program or site, and then of course if they do have access we tell them. But this code is a little messy and could easily be changed to perform better. They only have access if approved and 18 or older, so let’s make that a single “if” statement, and then have the else handle the “error” messages.
<?php
$age = 18;
$approved = 1;
if($age >= 18 && $approved == 1) {
echo 'Access to Site Approved';
}else{
echo 'Not Allowed because...';
if($age < 18) {
echo 'Under 18 years of age.';
}
if($approved == 0) {
echo 'Not Approved';
}
}
?>
The above code has 2 sets of variables getting compared in the first “if” statement, and if they are both true will make the “if” statement true. We use the symbol “&&” to mean “and”, so both the left and right side of that “if” statement have to be true to work. We could use “||” to mean “or”, so either side has to be true or both for the “if” statement to work. Then on the “else” we check why they are not allowed, and use two separate statements to check if it was because of age and to check if it was because they were approved or not.
You do not always have to show the user exactly why something happened, but if it includes user interaction it is a great idea so they know what they need to fix or wait for. If you just “echo” out “Can’t Access Site” instead of telling them about the age or approved, they won’t know what is wrong, and may just assume the site is not working. But don’t share too much information to the user because it became a privacy concern; but we will get into that in the future.
If I told you to create a site to only allow America and Canada to have access to it. And if a visitor doesn’t have access to it show them the message “Only American and Canadian visitors are allowed”, how will you do it? The visitors country is in a text format of “America”, “Canada”, “Mexico”, etc.
This code will look something similar to this, but do note that if your code doesn’t look exactly the same that is fine, no two codes look alike.
<?php
$country = "America";
if($country == "America" || $country == "Canada") {
echo 'You are Allowed into the site';
} else {
echo 'Only American and Canadian Visitors Allowed';
}
?>
I use a single “if” and then “else” to check. Inside the “if” statement I check if country equals america OR country equals canada. But using string variables for this type of checking isn’t that good of an idea because it uses more space to store information in a database or file. So let us make each country have a number (PHP code will only get the number, not the string)
1: USA
2: Canada
3: Mexico
4: Peru
5: Russia
6.. 7.. 8..
Now we only need to check if the country is 1 or 2, not a string. This is how most sites will handle this. They will have a database table containing 2 columns, ID and Country. Then users will store the ID of the country they are located in. The string is used only when we need to show the country name dynamically, for instance on a users profile where we display location. Now create a PHP script where instead of strings we use the integers above to check if they are in the USA or Canada.
<?php
$country = 1;
if($country == 1 || $country == 2) {
echo 'You are Allowed into the site';
} else {
echo 'Only American and Canadian Visitors Allowed';
}
?>
This is much cleaner and more reliable. There is one more type of “if” statement I want to show before this tutorial ends and we move into part 3, the “else if” statement. It works similar to “else” making it run only when the “if” statement before it isn’t true, but it also allows parameter like in “if” to refine our statements. If I wanted USA and Canada to both see different sites I would consider using if, else if, and then else.
<?php
$country = 1;
if($country == 1) {
echo 'Show USA Site';
} else if($country == 2) {
echo 'Show Canadian Site';
} else {
echo 'Only American and Canadian Visitors Allowed';
}
?>
First we check “if” country is 1, if it is none of the other “if”, or “else” statements execute. If it isn’t true, we check “else if” country is 2. And then it keeps going down the “if” statements until it reaches one that is true. If none of them are true it goes to the final “else” or doesn’t execute any code if there is no “else” at the end. You can have unlimited “else if” statements attached together but you can only have 1 “else” and 1 “if” in a series.
For example, the below code has multiple “else if” statements attached to the first “if” statement.
<?php
$country = 1;
if($country == 1) {
echo 'Show USA Site';
} else if($country == 2) {
echo 'Show Canadian Site';
} else if($country == 3) {
echo 'Country #3';
} else if($country == 4) {
echo 'Country #4';
} else {
echo 'Only American and Canadian Visitors Allowed';
}
?>