PHP If / Else related question

Discussion in 'PHP' started by nsusa, Nov 29, 2006.

  1. #1
    I am testing a script for one of my websites. I am looking for a certain condition to be met. If it is true, I am currently echoing text to a page. Otherwise (else) I am displaying a different message. Now I need to change this, but are not sure with the right command. Here is the existing code

    
    if (code removed as not needed for this question()) {
        echo "The result is true";
    } else {
        echo "The result is not true. Go somewhere else";
    }
    
    
    PHP:
    The "If" echo should pretty much be replaced with "do nothing" = meaning - just finish displaying the page the code is embedded in. The "else" echo should be replaced with a redirect to another page. How would I do that? Thanks.

    Christoph
     
    nsusa, Nov 29, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Not sure if I understand you correctly, but you can use the ! operator.

    
    
    if (! condition)
    {
       // Do something
    }
    
    PHP:
    ! means "if not". It's what the else would do basically.
     
    nico_swd, Nov 29, 2006 IP
  3. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Well, if there is no code html outputted before the if condition you can use the 'header' redirect. For example:

    if ( this == that )
    {
      // do nothing
    }
    else 
    {
      header("Location: http://www.google.com/");
      exit;
    }
    PHP:
     
    chopsticks, Nov 29, 2006 IP
  4. nsusa

    nsusa Peon

    Messages:
    858
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am getting this error message when I try your suggestion, chopsticks.

    Christoph
     
    nsusa, Nov 29, 2006 IP
  5. nsusa

    nsusa Peon

    Messages:
    858
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Replacing " with ' helped .... ;)

    Thanks.

    Christoph
     
    nsusa, Nov 29, 2006 IP
  6. weknowtheworld

    weknowtheworld Guest

    Messages:
    306
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Instead of the header... you can also use...

    echo "<script>window.open("--link--","_self")</script>";

    This is a better approach using javascript
     
    weknowtheworld, Nov 30, 2006 IP
  7. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #7
    You probably have some html outputted to the page already or something. Placing it before any html is outputted may fix it.
     
    chopsticks, Nov 30, 2006 IP