PHP variables

Discussion in 'PHP' started by aklead, Mar 3, 2009.

  1. #1
    Hello,

    I am kind of a newbie to PHP. How can I include a conditional statement within a variable?

    EX:

    
        $example = "example";
    
        $mainvariable= "This is just an".
        if ($example){echo $example;}
        ." code for my problem";
    
    Code (markup):

    I keep getting the error:

    Parse error: syntax error, unexpected T_IF

    Any help or direction would be fantastic!

    Thanks in advance,
     
    aklead, Mar 3, 2009 IP
  2. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #2
    Why you want to add IF/ELSE to a variable ? :eek: It's just not possible !
    What you want to get by doing this ?
     
    ActiveFrost, Mar 3, 2009 IP
  3. ILiketohelp

    ILiketohelp Guest

    Messages:
    756
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What exactly are you trying to do? I can help.
     
    ILiketohelp, Mar 3, 2009 IP
  4. aklead

    aklead Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I was trying to write an advanced search feature and wanted to modify the $sql variable depending on which advanced search variables were included in the search.
     
    aklead, Mar 3, 2009 IP
  5. ILiketohelp

    ILiketohelp Guest

    Messages:
    756
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Doesn't make sense... your if statement is setup wrong though...

    if ( $example = "example") { Then do this } Else { Do this }
     
    ILiketohelp, Mar 3, 2009 IP
  6. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #6
    <?php
    $example = "Big Example";
    $mainvariable= "This is just an ";
    if (isset($example)) {
         echo $example." code for my problem";
    } else {
    exit(); // exit script if $example is empty
    }
    ?>
    PHP:
    Merging variables with a string :
    1. $merged = $var1."string";
    2. $merged2 = $var1, "string";
     
    ActiveFrost, Mar 3, 2009 IP
  7. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #7
    I think this is what you mean :

    1. $mainvariable is the main variable.
    2. If $example is not empty, add the string 'example' to the $mainvariable.
    3. Then, complete the $mainvariable with string 'code for my problem'

    
    $example = "example";
    
    $mainvariable = "This is just an";
    
    if ($example != '') { $mainvariable .= $example; }
    
    $mainvariable .=" code for my problem";
    
    PHP:
    - ads2help
     
    ads2help, Mar 3, 2009 IP
  8. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #8
    You want what's called the "ternary operator".

    $example = 'bird';
    $mainvariable = 'Can the animal fly? ' . ($example == 'bird' ? 'Yes' : 'No');
    Code (markup):
    No need for "if" in this context; just follow your test with a question mark and the result you want if the test is true, and then a colon and the result you want if the test is false. To avoid associative ambiguity just put the whole thing inside a set of parentheses.
     
    SmallPotatoes, Mar 3, 2009 IP
  9. mehdiali

    mehdiali Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    all above are true but last one is the best :)
    $example = 'bird';
    $mainvariable = 'Can the animal fly? ' . ($example == 'bird' ? 'Yes' : 'No');//short if and else
     
    mehdiali, Mar 4, 2009 IP
  10. aklead

    aklead Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thank you all for your help! This has been a very informative thread!
     
    aklead, Mar 4, 2009 IP