• Tuesday, 12 August 2014

    PHP Comments | Conditional Statements | Case Switching

    Comments

    While in PHP mode, you can mark certain parts of your code as a comment that should not be executed. There are three ways of doing this: //, /* */, and #. // and # mean "Ignore the rest of this line," whereas /* means "Ignore everything until you see */." Some complications exist with /* and */ that make them less desirable to use.
    <?php
        print "This is printed\n";
        // print "This is not printed\n";
        # print "This is not printed\n";
        print "This is printed\n";
        /* print "This is not printed\n";
        print "This is not printed\n"; */
    ?>
    That chunk of code shows all three types of comments in action, but does not demonstrate the problem with the /* */ form of commenting. If you were to start a /* comment on line one, and end it on the line near the bottom where the other /* comment is started, you would find that the script would fail to work. The reason for this is that you cannot stack up, or "nest," /* */ comments, and attempting to do so will fail spectacularly.
    It is generally best to stick to // for your commenting purposes, simply because it is easy to spot, easy to read, and easy to control.

    Conditional Statements

    PHP allows you to choose what action to take based on the result of a condition. This condition can be anything you choose, and you can combine conditions to make actions that are more complicated. Here is a working example:
    <?php
        $Age = 20;
        if ($Age < 18) {
            print "You're young - enjoy it!\n";
        } else {
            print "You're not under 18\n";
        }    if ($Age >= 18 && $Age < 50) {
            print "You're in the prime of your life\n";
        } else {
            print "You're not in the prime of your life\n";
        }
        if ($Age >= 50) {
            print "You can retire soon - hurrah!\n";
        } else {
            print "You cannot retire soon :( ";
        }
    ?>
    At the most basic level, PHP evaluates if statements left to right, meaning that it first checks whether $Age is greater or equal to 18, then checks whether $Age is less than 50. The double ampersand, &&, means that both statements must be true if the print "You're in the prime of your life\n" code is to be executedif either one of the statements is not true for some reason, "You're not in the prime of your life" is printed out instead. The order in which conditions are checked varies when operator precedence matters.
    As well as &&, there is also || (the pipe symbol printed twice) which means OR. In this situation, the entire statement is evaluated as true if any of the conditions being checked is true.
    There are several ways to compare two numbers. We have just looked at < (less than), <= (less than or equal to), and >= (greater than or equal to). We will be looking at the complete list later, but first I want to mention one important check: = =, or two equals signs put together. That means "is equal to." Therefore 1 == 1 is true, and 1 == 2 is false.
    The code to be executed if the statement is true is in its own block (remember, a block starts with { and finishes with }), and the code to be executed otherwise is in an else block. This stops PHP from trying to execute both the true and false actions.
    One key thing to note is that PHP practices "if statement short-circuiting" this is where PHP will try to do as little conditional work as possible, so it basically stops checking conditional statements as long as it is sure it can stop. For example:
    if ($Age > 10 && $Age < 20)
    If $Age evaluates to 8, the first check ($Age > 10) will fail, so PHP will not bother checking it against 20. This means you can, for example, check whether a variable is set and whether it is set to a certain valueif the variable is not set, PHP will short-circuit the if statement and not check its value. This is good because if you check the value of an unset variable, PHP will flag an error.
    A helpful addition to if statements is the elseif statement, which allows you to chain conditions together in a more intelligent way:
    <?php
        if ($Age < 10) {
            print "You're under 10";
        } elseif ($Age < 20) {
            print "You're under 20";
        } elseif ($Age < 30) {
            print "You're under 30";
        } elseif ($Age < 40) {
            print "You're under 40";
        } else {
            print "You're over 40";
        }
    ?>
    You could achieve the same effect with if statements, but using elseif is easier to read. The downside of this system is that the $Age variable needs to be checked repeatedly.
    If you only have one statement of code to execute, you can do without the braces entirely. It's a readability issue. So, these two code chunks are the same:
    if ($banned) {
        print "You are banned!";
    }if ($banned) print "You are banned!";


    Case Switching

    Your if...elseif blocks can become unwieldy when you have a series of conditions that all test against the same variable, as here:
    <?php
        $Name = "Bob";
        if ($Name = = "Jim") {
            print "Your name is Jim\n";
        } elseif ($Name = = "Linda") {
            print "Your name is Linda\n";
        } elseif ($Name = = "Bob") {
            print "Your name is Bob\n";
        } elseif ($Name = = "Sally") {
            print "Your name is Sally\n";
        } else {
            print "I don't know your name!\n";
        }
    ?>
    PHP has a solution to this: switch/case. In a switch/case block, you specify what you are checking against, then give a list of possible values you want to handle. Using switch/case statements, we can rewrite the previous script like this:
    <?php
        $Name = 'Bob';
        switch($Name) {
        case "Jim":
            print "Your name is Jim\n";
            break;
        case "Linda":
            print "Your name is Linda\n";
            break;
        case "Bob":
            print "Your name is Bob\n";
            break;
        case "Sally":
            print "Your name is Sally\n";
            break;
        default:
            print "I don't know your name!\n";
        }
    ?>
    Switch/case statements are frequently used to check all sorts of data, and they take up much less room than equivalent if statements.
    There are two important things to note in the PHP switch/case statement code. First, there is no word "case" before "default" that is just how the language works. Second, each of our case actions above end with "break;". This is because once PHP finds a match in its case list, it will execute the action of that match as well as the actions of all matches beneath it (further down on your screen). This way of working is taken directly from C, and is generally counterintuitive to how we thinkit is rare that you will want to exclude a break from the end of your cases.
    The default case is executed if PHP doesn't find a match in one of the other cases, or if the case before it was executed and didn't end with a break statement.
    The keyword "break" means "Get out of the switch/case statement," and has the effect of stopping PHP from executing the actions of all subsequent cases after its match. Without the break, our test script would print out this:
    Your name is Bob
    Your name is Sally
    I don't know your name

    Popular Posts

    Contact Form

    Name

    Email *

    Message *