Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Is it possible to use logical operators in the "then" part of the if/then statement in PHP?

This is my code:

if ($TMPL['duration'] == NULL) {
$TMPL['duration'] = ('120' or '124' or '114' or '138'); }
else {
$TMPL['duration'] = ''.$TMPL['duration']; }
share|improve this question
 
What would be the logical meaning of you then statement ? I don't understand what you are trying to achieve ... –  Xaltar May 1 '13 at 21:03
 
use the pipe sign? | –  arminb May 1 '13 at 21:04
 
I think he means elseif? –  Pankrates May 1 '13 at 21:04
 
Not sure I understand... do you mean to combine multiple IF statements with OR (||)? –  Brock Hensley - FKA dirt May 1 '13 at 21:04
 
Sorry for not be that clear. What I'm trying to do is if duration is equal to NULL, set duration equal to any of those numbers by choosing one at random. Is that possible? –  Roku May 1 '13 at 21:19
add comment

closed as not a real question by pilsetnieks, bensiu, hjpotter92, Vishal, gaige May 2 '13 at 3:29

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

up vote 3 down vote accepted

Use else if.

$a = 1;

if($a === 1) {
    // do something
} else if ($a === 2) {
    // do something else    
}

Note that in most case the switch statement is better for that, like:

switch($a) {
    case 1:
        // do something
        break;

    case 2:
        // do something else
        break;
}

or:

switch(TRUE) {
    case $a === 1 :
        // do something else    
        break;

    case $b === 2 :
        // do something else
        break;
}
share|improve this answer
 
What I'm trying to do is if duration is equal to NULL, set duration equal to any of those numbers by choosing one at random. Is that possible? –  Roku May 1 '13 at 21:25
 
use this: pastebin.com/6sNhh83G –  hek2mgl May 1 '13 at 21:29
add comment

Are you aiming for a switch?

switch($TMPL['duration']) {
    case NULL:
    case '120':
    case '124':
    case '114':
    case '138':
        <do stuff>
        break;
    default:
        $TMPL['duration'] = ''.$TMPL['duration'];
}
share|improve this answer
add comment

Also you can do something like this utilizing in_array:

if ($TMPL['duration'] === NULL
    || in_array($TMPL['duration'], array('120','124','114','138')) {
    // Do something if duration is NULL or matches any item in the array
} else {
    // Do something if duration is not NULL or does not match any item in array
}
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.