1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

how i use if condition in php

Discussion in 'PHP' started by manojsadh, Mar 24, 2007.

  1. #1
    can any one here please tell me how i use

    if condition in php


    thnx in advance
     
    manojsadh, Mar 24, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
  3. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if
    The if construct is one of the most important features of many languages, PHP included. It allows for conditional execution of code fragments. PHP features an if structure that is similar to that of C:


    if (expr)
    statement




    As described in the section about expressions, expression is evaluated to its Boolean value. If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE - it'll ignore it. More information about what values evaluate to FALSE can be found in the 'Converting to boolean' section.

    The following example would display a is bigger than b if $a is bigger than $b:


    <?php
    if ($a > $b)
    echo "a is bigger than b";
    ?>



    Often you'd want to have more than one statement to be executed conditionally. Of course, there's no need to wrap each statement with an if clause. Instead, you can group several statements into a statement group. For example, this code would display a is bigger than b if $a is bigger than $b, and would then assign the value of $a into $b:


    <?php
    if ($a > $b) {
    echo "a is bigger than b";
    $b = $a;
    }
    ?>



    If statements can be nested indefinitely within other if statements, which provides you with complete flexibility for conditional execution of the various parts of your program.


    ----------------------
    else
    Often you'd want to execute a statement if a certain condition is met, and a different statement if the condition is not met. This is what else is for. else extends an if statement to execute a statement in case the expression in the if statement evaluates to FALSE. For example, the following code would display a is bigger than b if $a is bigger than $b, and a is NOT bigger than b otherwise:


    <?php
    if ($a > $b) {
    echo "a is bigger than b";
    } else {
    echo "a is NOT bigger than b";
    }
    ?>


    The else statement is only executed if the if expression evaluated to FALSE, and if there were any elseif expressions - only if they evaluated to FALSE as well (see elseif).
    ----------------------------------

    elseif
    elseif, as its name suggests, is a combination of if and else. Like else, it extends an if statement to execute a different statement in case the original if expression evaluates to FALSE. However, unlike else, it will execute that alternative expression only if the elseif conditional expression evaluates to TRUE. For example, the following code would display a is bigger than b, a equal to b or a is smaller than b:


    <?php
    if ($a > $b) {
    echo "a is bigger than b";
    } elseif ($a == $b) {
    echo "a is equal to b";
    } else {
    echo "a is smaller than b";
    }
    ?>



    There may be several elseifs within the same if statement. The first elseif expression (if any) that evaluates to TRUE would be executed. In PHP, you can also write 'else if' (in two words) and the behavior would be identical to the one of 'elseif' (in a single word). The syntactic meaning is slightly different (if you're familiar with C, this is the same behavior) but the bottom line is that both would result in exactly the same behavior.

    The elseif statement is only executed if the preceding if expression and any preceding elseif expressions evaluated to FALSE, and the current elseif expression evaluated to TRUE.

    ----------------------------
    if..endif syntax
    The `alternative' way to write if/elseif/else statements, using if(); elseif(); else; endif; cannot be efficiently implemented without adding a large amount of complexity to the 3.0 parser. Because of this, the syntax has been changed: Example D-5. Migration: old if..endif syntax

    if ($foo);
    echo "yep\n";
    elseif ($bar);
    echo "almost\n";
    else;
    echo "nope\n";
    endif;

    Example D-6. Migration: new if..endif syntax

    if ($foo):
    echo "yep\n";
    elseif ($bar):
    echo "almost\n";
    else:
    echo "nope\n";
    endif;

    Notice that the semicolons have been replaced by colons in all statements but the one terminating the expression (endif).
     
    jitesh, Mar 24, 2007 IP
  4. manojsadh

    manojsadh Banned

    Messages:
    111
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thnx jitesh


    can u tell me any book of php whill will be very helpfull for me
     
    manojsadh, Mar 30, 2007 IP
  5. PHPGator

    PHPGator Banned

    Messages:
    4,437
    Likes Received:
    133
    Best Answers:
    0
    Trophy Points:
    260
    #5
    SitePoint has a book I would recommend to anyone wanting to learn PHP. I believe it is called "How to build a PHP and MySQL Driven Website". I believe by Kevin Yank?

    Sorry, i'm sure you can go there to find it. It also allows you to download the first 4 chapters for free before purchasing. If you understand the first four chapters of the book, which if you take your time and read thoroughly and follow the examples you should... then you will be well on your way to understanding how to impliment the example into things you want to do with your websites.
     
    PHPGator, Mar 30, 2007 IP
  6. ccoonen

    ccoonen Well-Known Member

    Messages:
    1,606
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    160
    #6
    I always liked the PHP cookbook - gives realworld examples :)
     
    ccoonen, Mar 30, 2007 IP