Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5

PHP - Part 4

#1
How to Program PHP
Tuturial, part 4

So far, we've explored several fundamental concepts in programming. We've explored the echo statement, and how a PHP file needs to be structured in order for the server to run it. In the second tutorial, an overview was given on how to use variables and arrays. In the third tutorial, functions were introduced. Functions are an extremely fundamental concept in PHP, but it can take quite a while to get used to them. If you're still a little confused, keep reading. All of this will become more clear as you continue working through these tutorials.

In this tutorial, I will introduce the concept of conditional statements. Conditional statements allow us to run different parts of our code, depending on whether a certain condition is met. For example, what if you had a script that denies registration and leaves a message for the user if the user is under 13? Many forums for example are required to deny registration to users under 13 due to COPPA restrictions in US law, so this is a popular real-world example.

Now, you have some code that echoes "sorry, but users must be at least 13 years of age to register," but you need to actually check to see if the user is at least 13 years of age before you execute that code. How exactly would you do that?

PHP has a set of "conditionals" that allow you to check for certain conditions before executing a block of code.

Code:
if ($condition == 1) {
// execute this code
// this IF statement only executes the code in between the curly brackets, and it will only execute this code if the $condition variable is equal to 1.
echo "executing the IF statement code block now. ";
}

The code above won't actually do anything, but it does demonstrate that the code between the curly braces will only execute if the $condition variable is equal to one.

TIP: Notice carefully how there were two equal signs used in the IF statement for ($condition == 1). Why did we need two instead of one? Turns out = and == are different operators in PHP. if you just use =, it will try to set the $condition variable to 1 which obviously is not what we want. If you use ==, instead of trying to assign a value to the variable, it will check its existing value, which is obviously what we want. Generally, you will always use == instead of = in conditionals for PHP.

Now, the code inside of the IF statement will only execute if the $condition variable is equal to 1. What if we want to execute a separate block of code if the $condition variable IS NOT equal to 1? In this case, we'll use an IF statement, and then and ELSE statement.

Code:
if ($condition == 1) {
// execute this code
// this IF statement only executes the code in between the curly brackets, and it will only execute this code if the $condition variable is equal to 1.
echo "executing the IF statement code block now. ";
}
else {
// this ELSE block will ONLY execute if the IF block does not execute. Basically, if $condition is equal to 1, PHP will execute the code inside of the IF block. If $condition is equal to anything OTHER than 1, it will execute the code in the ELSE block instead.
echo "executing ELSE block code now!";

}

As you can see, we've made use of the "else" block above. Simply put, the code in the "else" block will only run if the "if" block fails to run. If $condition is equal to anything other than 1, the else block code will run. If the $condition variable is equal to 1, the "if" block code will run. In no circumstance will both blocks of code ever run together.

So, what if it turns out that you need to execute a block of code if $condition = 1, a separate block of code if $condition = 2, and yet another separate block of code if $condition is equal to anything else? Turns out PHP has the "IF ELSE" block for that.


Code:
if ($condition == 1) {
// execute this code
// this IF statement only executes the code in between the curly brackets, and it will only execute this code if the $condition variable is equal to 1.
echo "choice 1 has been selected! ";
}
if else ($condition == 2) {
// this block of code ONLY executes if the $condition variable is equal to 2.
echo "choice 2 has been selected! ";

}
else {
// this ELSE block will ONLY execute if the IF block does not execute. Basically, if $condition is equal to 1, PHP will execute the code inside of the IF block. If $condition is equal to anything OTHER than 1, it will execute the code in the ELSE block instead.
echo "Oops! We don't recognize your choice selection. An error has occurred. ";

}

As you can see, the first IF block only executes if $condition is equal to 1. The second "IF ELSE" block only executes if $condition is equal to 2. The last "ELSE" block only executes if neither of the previous two blocks of code executed. In other words, "ELSE" executes if your network of IF and IF else statements never "found a match".

There are actually other conditional statements in PHP as well. The "switch" statement is fairly popular, and there are try and catch statements as well. These aren't very important to the purpose of our tutorials and aren't essential to learning the basics of PHP, so we won't attempt to go over these in detail.

Anyway, back to the original script concept for denying user registration to users under thirteen...

Code:
if ($age < 13) {
echo "Sorry, but users must be at least 13 years of age to register. ";
// the < operator is the "less than" operator for PHP.
}
else {
processRegistration (); // runs a function that processes the user registration.
// This only runs if the user is 13 or over. If the user was under 13, the IF block above would have run instead of this block of code. Remember, in no circumstance will the "ELSE" block ever run if the IF block has run.
}

If you would like to play around with these scripts, copy and paste the code below and give it a try. Big Grin

Code:
<?php
// play around with the following variable
$age = 12;
if ($age < 13) {
echo "Sorry, but users must be at least 13 years of age to register. ";
// the < operator is the "less than" operator for PHP.
}
else {
processRegistration (); // runs a function that processes the user registration.
// This only runs if the user is 13 or over. If the user was under 13, the IF block above would have run instead of this block of code. Remember, in no circumstance will the "ELSE" block ever run if the IF block has run.
}
?>

In the next tutorial, we will explore how HTML forms are set up and how we can design the first interactive applications. The fun stuff will be beginning soon. Big Grin

Stay tuned!

Next Tutorial: Let the fun stuff begin; forms and PHP introduction
Directory of PHP Tutorials

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP - Part 6 Darth-Apple 2 7,126 November 12th, 2013 at 5:31 PM
Last Post: Damian B.
  PHP - Part 5 Darth-Apple 5 12,172 August 28th, 2013 at 10:10 PM
Last Post: Darth-Apple
  PHP - Part 2 Darth-Apple 2 7,614 August 19th, 2013 at 9:26 PM
Last Post: Darth-Apple
  PHP - Part 1 Darth-Apple 4 10,952 August 19th, 2013 at 7:40 PM
Last Post: Damian B.



Users browsing this thread: 2 Guest(s)

Dark/Light Theme Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2024
Forum design by Makestation Team © 2013-2024