The PHP Interpreter. Contribute to php/php-src development by creating an account on GitHub. PHP if, else and else if Conditional Statements While writing programs/scripts, there will be scenarios where you would want to execute a particular statement only if some condition is satisfied. In such situations we use Conditional statements. A Dependency Manager for PHP. Download Composer Latest: v2.0.13. To quickly install Composer in the current directory, run the following script in your terminal.
The PHP if statement is very similar to other programminglanguages use of the if statement, but for those who are notfamiliar with it, picture the following:
Think about the decisions you make before you go to sleep. If you havesomething to do the next day, say go to work, school, or an appointment, thenyou will set your alarm clock to wake you up. Otherwise, you will sleep in aslong as you like!
This simple kind of if/then statement is very common in every day life and also appears in programming quite often. Whenever you want to make a decisiongiven that something is true (you have something to do tomorrow) and be sure that youtake the appropriate action, you are using an if/then relationship.
The if statement is necessary for most programming, thus it is important in PHP. Imagine that on January1st you want to print out 'Happy New Year!' at the top of your personal web page. With the use of PHP if statements you could have this process automated, months in advance, occuring every year on January 1st.
This idea of planning for future events is something you would never have had the opportunity of doing if you had just stuck with HTML.
The 'Happy New Year' example would be a little difficult for youto do right now, so let us instead start off with the basics of the if statement.The PHP if statement tests to see if a value is true, and if it is a segment ofcode will be executed. See the example below for the form of a PHP if statement.
Welcome to my homepage!
Did you get that we were comparing the variable $my_name with 'someguy' to see if theywere equal? In PHP you use the double equal sign () to compare values.Additionally, notice that because the if statement turned out to be true, the code segmentwas executed, printing out 'Your name is someguy!'. Let's go a bit more in-depth into this exampleto iron out the details.
- We first set the variable $my_name equal to 'someguy'.
- We next used a PHP if statement to check if the value contained in the variable $my_name was equal to 'someguy'
- The comparison between $my_name and 'someguy' was done with a double equal sign ', not a single equals'='! A single equals is for assigning a value to a variable, while a double equals is for checking if things are equal.
- Translated into english the PHP statement ( $my_name 'someguy' ) is ( $my_name is equal to 'someguy' ).
- $my_name is indeed equal to 'someguy' so the echo statement is executed.
Let us now see what happens when a PHP if statement is not true, inother words, false. Say that we changed the above example to:
Here the variable contained the value 'anotherguy', which is not equal to 'someguy'. The if statement evaluated to false, so the code segment of the if statement was notexecuted. When used properly, the if statement is a powerful tool to have in yourprogramming arsenal!
Php If Not
If you would rather download the PDF of this tutorial, check out our PHP eBook from the Tizag.com store. Print it out, write all over it, post your favorite lessons all over your wall!
Php If Not
Report a Bug or Comment on This Lesson - Your input is what keeps Tizag improving with time!