PHP Tutorials - Conditional Statements & variables !!

Started by ram, Jul 21, 2008, 10:45 AM

Previous topic - Next topic

ram

Hi [you],

Variables

In this PHP Tutorial you will learn about Variables, different types of Variables like Identifiers, Variables, Statements and Constants.

Identifiers:

Identifier is the name of the data, identifiers also can be used as a label for a set of commands.

There are rules for identifiers naming, rules are:

• The first character has to be a letter or an underscore (_);
• The following characters can be any combination of, letters, digits, and undersore.

Example:

The following are valid identifiers:

   identifier
    _identifier
    __identifier
    _another_identifier
    another_identifier


Variables:

A variable is a data container, it can contain data of any of the types stated before.
A variable can contain only one value at a time.
A variable is an identifier preceded by a dollar sign ($).
Variable names are case sensitive, for example, $var is different from $Var.
Variables can be embedded directly in strings, for example, "Value \$var is embedded here"

If there is no space between the variable name and the rest of the string, variable can be embedded like in the following example:

    <?php
    $var = 100;
    echo "There are {$var}000 books in the college library";
    ?>


Statements:

A statement is a command that is executed, for example the echo function causes an expression to be sent to the standard output.

Statements has to be ended with a semicolon.

Constants:

A constant is a data container like variable, but the data value of the constant doesn't change.
To create a constant use the construct define().

Example:


    <?php
    define("MY_CONSTANT", 100);
    echo MY_CONSTANT;
    ?>


define() takes two arguments, the first is a string, which is the constant name, and the another is the constant value.

There is a little trick about constants, they can be defined with any name, but they can't be used unless they follow the identifier naming rules.

PHP Tutorials - Conditional Statements

In this PHP Tutorials you will learn about Conditional Statements - if statement, if-else statement, Alternative if-else statement and switch statement.

if statement:

if syntax is as follows:

    if (an expression that return Boolean value) {
        Code to be executed.
    }


PHP evaluates the Boolean expression to true or false, if the evaluation result is true, then the code inside the curly braces is executed, else it will be ignored.

Example:

    <?php
        $x = 5;
        if($x < 10) { // returns true
            echo $x; // Will be executed
        }
    ?>


if-else statement:

if-else syntax is as follows:

    if (an expression that return Boolean value) {
        Code to be executed if the above expression returned true.
    } else {
        Code to be executed if the above expression returned false.
    }


PHP evaluates the Boolean expression to true or false, if the evaluation result is true, then the code inside the if curly braces is executed, else the code inside the else curly braces is executed.

Example:

    <?php
        $x = 5;
        if($x > 10) { // Returns false
            echo $x; // Won't be executed
        }
        else { // The else code will be executed
            $x += 7;
            echo $x; // Will print 12
        }

    ?>

Many conditions can be checked with if-else statement, the syntax for using if-else with more than one condition is:

    if (an expression that return Boolean value) {
        Code to be executed if the above expression returned true.
    } elseif (another expression that return Boolean value){
        Code to be executed if the above expression returned false.
    }
    // Multiple elseif block can be added here
    else {
        Code to be executed if the above expression returned false.
    }


PHP evaluates the Boolean expression for the if block first to true or false, if the evaluation result is true, then the code inside the if curly braces is executed, else it evaluates the next elseif blocks, if any of the elseif blocks returned true, then its code is executed, if no elseif condition returned true, then the else block is executed.

Example:

    <?php
        $x = 5;
        if($x > 10) { // Returns false
            echo $x; // Won't be executed
        }
        else if (x == 5) { // The else code will be executed
            $x += 7;
            echo $x; // Will print 12
        }
        else { // Will be neglected
            $x++;
            echo $x;
        }
    ?>


Alternative if-else statement:

An alternative way to write if-else statement is like the following:
condition? Value_if_true : Value_if_false

Example:

    <?php
        $x = 5;
        $y = $x > 3? $x : $x + 5; // $y = 5
        echo $y;
    ?>

switch statement:

if-else statement that needs more than one condition can replaced by case statement.

Example:

    <?php
        $x = 2;
        if ($x == 1) {
            echo 'One';
            break;
        }
        else if (x == 2) {
            echo 'Two';
            break;
        }
    else {
            echo 'not one or two';
        }
    ?>


The previous code can be substituted with the following code:

    <?php
        $x = 2;
        switch ($x) {
            case 1:
                echo 'One';
                break;
            case 2:
                echo 'Two';
                break;
            default:
                echo 'Not one or two';
        }
    ?>


Example:

    <?php
        $x = 2;
        if($x == 1 || $x == 2) {
        echo 'One or two';
        break;
        }
            else {
            echo 'Not one or two';
        }
    ?>


The previous code can be substituted with the following code:[/color]

    <?php
        $x = 2;
        switch ($x) {
            case 1:
            case 2:
                echo 'One or two';
                break;
            default:
                echo 'Not one or two';
        }
    ?>