SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: if not working?

  1. #1
    SitePoint Addict psychedelic's Avatar
    Join Date
    Feb 2002
    Location
    Boston
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    if not working?

    sometimes i do this:
    PHP Code:
    if ($action=add) {
       
    // .... execute this code
    }
    if (
    $action=edit) {
       
    // ... execute this code

    and it'll do both actions. any ideas? am i doing something idioticallky wrong?

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: if not working?

    use "else if" if you only want one branch to be conditionally executed, and you should use "==" instead of "=" to test for equality:
    PHP Code:
    if ($action==add) {
       
    // .... execute this code
    }
    else if (
    $action==edit) {
       
    // ... execute this code


  3. #3
    SitePoint Addict psychedelic's Avatar
    Join Date
    Feb 2002
    Location
    Boston
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you! it worked.

  4. #4
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One more thing to point out: since you're checking to see if a variable is equal to a string, you need to utilize quotes around the string (either 'single' or "double" quotes):
    PHP Code:
    <?php
    if($action=='add'){
         
    // .... execute this code
    }
    elseif(
    $action=='edit'){
         
    // ... execute this code
    }
    else{
         
    // ... execute this code
    }
    ?>


    -Colin
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •