Multiple echos with strstr not works o.o

Discussion in 'PHP' started by thesurface, Jun 27, 2012.

  1. #1
    Why i dont get output okok in bellow example ?

    <?php
    $site = "wp_cont|jqu";
    if (strstr($site, "wp_cont") == TRUE) {
        $x = "ok";
    }
    if(strstr($site, "jqu") == TRUE) {
        $x = "ok";
    }
    echo $x;
    PHP:

     
    thesurface, Jun 27, 2012 IP
  2. akhileshbc

    akhileshbc Active Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    75
    #2
    Because, when the second if condition matches, it would overwrite the $x variable. If you used the concatenation, it would have worked:
    
    <?php
    $site = "wp_cont|jqu";
    if (strstr($site, "wp_cont") == TRUE) {
        $x = "ok";
    }
    if(strstr($site, "jqu") == TRUE) {
        $x .= "ok";
    }
    echo $x;
    
    PHP:
    See that dot(.) operator used in the above code.
     
    Last edited: Jun 27, 2012
    akhileshbc, Jun 27, 2012 IP