PHP Conditionals
PHP, like most programming languages, allows us to create separate code that executes given differing circumstances. When we talk about code that executes (or not) based upon certain conditions, we say that the code 'branches' or separates into two or more paths. This can be thought of like a 'choose your own ending' story, in which we as programmers must write the complete story, for all possible endings. The structures that allow us to 'branch' our code based on circumstances are called conditionals.
If Statements
One of the primary conditionals of PHP is the if statement. Code inside an if statement only executes based on a test of a condition:
if(TEST CONDITION)
{
//code that occurs if condition proves true
}
Here is an example:
$myPay = 50000;
echo "My pay is $" . $myPay . "<br />";
if($myPay > 40000)
{
echo "I'm Happy!";
}
In the above code we first create a variable named $myPay, and assigned it to the value 50000. Then we test the value inside the parens of the statement, and used a 'greater than' to see if the current amount of pay was greater than 40000. Since it is, the code inside the curly braces executes, and the words "I'm Happy!" appear.
If/else Statements
What happens if the pay is less than $40000? In the previous code, there is no 'echo' of the feeling associated with any value less than $40000. Let's add an "else" block to catch all other conditions:
$myPay = 30000;
echo "My pay is $" . $myPay . "<br />";
if($myPay > 40000)
{
echo "I'm Happy!";
}else{
echo "Not as Happy!";
}
else if Statements
What if we have yet a third condition? We can add as many additional conditions by inserting an else if for each specific condition we wish to check as well. At the end we still use the "else" block to catch all other conditions. We'll use a constant, which we'll declare inside a define() statement:
define("MY_PAY",35000);
if(MY_PAY > 40000)
{//that would be good!
echo "I'm CONSTANTLY happy!<br />";
}else if(MY_PAY > 30000){
echo "I'm RELATIVELY happy!<br />";
}else{//not good...
echo "Not so happy!<br />";
}
The define() command allows us to create a constant, which can be filled with a simple value, like a string or a number, but once filled, can't be changed. A constant is available across all code in a global manner, while a variable is only available in the scope in which it is declared. We'll see this when we examine user created functions.
User Created Functions
When we find we are using code repeatedly, its time to think about wrapping our code into a user created function. We can place our own code and include PHP's built in functions inside our own functions. However, all variables declared inside the function do not exist outside of the function. The variable thus created inside the function is said to have a local scope. I call this 'what happens in Vegas, stays in Vegas'.
Variables must be passed into the function, or use a special global keyword to get inside a function. Constants do not need to do this, as all constants are available to all code. Any code that is always available we'll call global. So, variables are local in scope and constants are global. Let's view our function:
function areYouHappy($myPay)
{
if($myPay > 40000)
{
return "I'm happy!<br />";
}elseif($myPay > 30000){
return "Relatively happy! Got benes?<br />";
}else{
return "Not so happy!<br />";
}
}
Once we create a function, we can simply call the function by name, and all the code inside it is automatically run:
$bobs_pay = 40000;
$myOutput = areYouHappy($bobs_pay);
echo $myOutput;
Here we create a variable named $bobs_pay and assign a value, then we pass the value into the function by placing it into the parens next to the function name. In this way a new variable is created inside the function, and the data is copied to the new variable, thus maintaining our 'local' variable system while passing in data.
Think of a function like a 'car wash'. Data goes into the right side of the function (the parens) and potentially returns out of the left side of the function, sometimes into the same variable name. This is done by using the keyword return in the function. When a branch of code sees return, the data to the immediate right of the word is 'kicked out' (returned) to the outside world, in our case, the data is copied into the variable named $myOutput that is to the immediate left of the variable name. Above we echo the data to see what is returned. We can shorten this by just echoing the data returned by the function directly, with no need to use a variable:
$bobs_pay = 40000;
echo areYouHappy($bobs_pay);
We can see the advantage of a function when we have several numbers to pass in in succession:
$bobs_pay = 40000;
echo areYouHappy($bobs_pay);
$junes_pay = 50000;
echo areYouHappy($junes_pay);
$steves_pay = 45000;
echo areYouHappy($steves_pay);
Not only is the code reduced to one line in our page, but we can fix our code if there is a problem in one place, inside our function!
Functions Are Out Of The Flow Of Code
Before we created our own functions, we could think of all code as being executed a line at a time in order, similar to how an HTML page is processed. However, there are several things that are 'out of flow' of the code, which means they are processed first, then potentially only used if called by name in a page.
A function is one of the PHP constructs that are out of flow. This means that before any code is executed, all available user defined functions are read and memorized by the PHP processor. Because of this we can place functions anywhere in the code, since they are out of flow. Later we'll take our favorite functions and move them to a separate file and then include that file at the top of our page, thus using a functional include file just as we had used a structural include file to include header and footer HTML. The biggest difference is the functional include file is out of flow, while the structural include file would be in flow, and need to be placed where the contained HTML is to appear.
The global Keyword
Most of what we have disussed thus far on this page is common to many programming languages. The next feature, the global keyword is more unique to PHP. If we wish, we can place the word global inside our function and then list all of the variables that will now be available inside the current function:
function areYouHappy($myPay)
{
global $CompanyName, $Position;
if($myPay > 40000)
{
return "I work for $CompanyName and my position is $Position and I'm happy!<br />";
}elseif($myPay > 30000){
return "I work for $CompanyName and my position is $Position and I'm happy! Got benes?<br />";
}else{
return "I work for $CompanyName and my position is $Position and I'm not so happy!<br />";
}
}
Note the variables above named $CompanyName and $Position are not passed into the function as parameters, but are identified as global to this function by the use of the global keyword. Note that the global keyword does not make the variable global to all functions, just to this one.