
If statements are awesome, without them your PHP wouldn't be the same. So today we are going to look at IF statements and the other questions that go along with it.
If statements are questions that PHP asks, the most common use is to check whether a variable is set to something, and if it is you will do something, otherwise you will do something else. It's always best to think long and hard about IF statements because if you have one statement that contradicts another you wont get very far at all. I would say there are 2 levels of IF statements, the simple ones that just ask one question, or the slightly more complex that ask two questions in one statement.
To get started lets open up our PHP, using the <? to open and ?> to close, now at the top we want to set some variable, these are values that are assigned to a name, variables can change as the name suggests. So lets set up some names below...
Really very simple, now lets ask a question, i will keep it simple and output 'Hello Mr John' if my first name is set to John.
Now the problem with this IF statement is that if my first name isn't John it will not do anything, that's a bad move, so if I'm not called John we will give an error message by using the ELSE part of IF statements.
As you can see now, if I'm not John i will be told that life sucks.
If you run the script you have just compiled on a PHP server you will see that it outputs 'Hello Mr John' which is great, if you now go and change the code slightly so that the $first_name is now Tom instead of John, when you run this script you will see it gives the other message.
Now what if you want to ask if they are one of two people instead of just the one, well that's easy too, to do this we use another IF function called ELSEIF which is like doing another IF statement.
Now when you change the $first_name from John to Chris you will see a second welcome message.
You should always bare in mind that its good practice to always end an IF statement by adding an ELSE to the end, you don't need to do, and hell i don't even do it sometimes, but if you do it makes your code all pretty. Also always remember that ELSEIF should only be used if the question is similar to the first IF question, otherwise its pointless.
So your final code will look like below and you code will work perfectly. Congratulations on completing you IF statement tutorial.



