PHP If Else usage

Source: Internet
Author: User

PHP, if the declaration is very similar to other programming languages using the IF statement, but for those who are unfamiliar with it, the pictures are as follows:


Think about your decision to make before you go to bed. If you have to do this the next day, say go to work, go to school, or appointment, then you will set the alarm clock to wake you up. Otherwise, you will sleep in, as long as you like!

This simple one, if/then the statement is very common in everyday life, also appears often in the program. Whenever you want to make a decision, because what is real (you have such a tomorrow), and be sure to take the appropriate action, you are using if/then the relationship.

PHP, if the statement
If the declaration is necessary for most programming, it is important to be in PHP. Just imagine, on January 1, to print out "Happy New Year!" "Top of your personal page. Using PHP's statement, if you can have this process automated, a few months in advance, occurs every January 1.

This idea of planning future events is something you will never have a chance to do if you have fallen into justice with HTML.

If the example of the statement
The example of "Happy New Year" will be a little difficult for you to do so now let's start with the basic if statement. PHP's statement, if tested to see if it is true value, if it is a part of the code will be executed. See the following example form if a PHP is spoken.

$my _name = "Someguy";

if ($my _name = = "Someguy") {
echo "Your name is someguy!<br/>";
}
echo "Welcome to my homepage!";

Without you, we compare variables $ my_name with "Someguy" to see if they are all equal? Use a double equal sign (= =) in PHP to compare values. Also, notice that if the original statement is true, the code snippet is executed and printed "Your name is Someguy!" ” 。 Let's go a little deeper into this example with the details of the iron.

We first set the variable $ my_name equal to "Someguy".
Below we use a PHP if statement to check the value contained in the variable $ my_name equals "Someguy"
Comparison $ my_name and "Someguy" do double equals "= =", not one equals "="! Just equal distribution of a value to a variable, while equality is double check if things are equal. The
translation into English PHP declaration ($ my_name = = "Someguy") is ($ my_name equals "Someguy"). The
$ my_name is indeed equal to the "Someguy", making the Echo statement executed.
If there is a false statement
Now let's see what happens when a PHP statement is incorrect, in other words, fake. Say, we changed the example above:

$my _name = "Anotherguy";

if ($my _name = = "Someguy") {
echo "Your name is someguy!<br/>";
}
echo "Welcome to my homepage!";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.