In this tutorial, I presented some information about PHP and I wrote some lines of code to show how PHP works, the structure of a page. Today I will present the basic blocks of any program: decisional statements. You must use them no matter in what language you are coding. We need them to tell the computer what decisions should be made based on our input. I will present :
- IF Statement.
- IF, ELSE Statement.
- ElseIf Statement.
- Switch Statement.
All of them are very easy as I will show you below :
We can see for the example above that the general structure of “if” is : if (variable
Ok, we are keeping in $day, the nr. of current day 1 to 7. We use echo $day to see what’s the nr. of today. Just for testing purposes. We use also || operator which means “or”. So if $day is equal to 7 or $day is equal to 6 that we have a weekend, else we don’t have a weekend.
The difference with elseif is that we can say exactly for which case we should handle the situation. We can add as many elseif’s we need. And keep on else just something as an “not known” message.
Switch is an elegant alternative to elseif, but we must see the differences between them. After each commands in a case branch we need to use break. Also in elseif we can have a message on else branch if nothing was found that met our conditions. Personally I didn’t use Switch too much, but is good to know how to use it.
This is the end of my tutorial. Next I will present “include” and “require” to important functions of PHP. I hope you enjoy this. Also always any questions or suggestions are welcomed. Thanks for reading!
Leave a Reply