AAA
AAA

Reputation: 3168

Is this the correct way to do two equal statements in one?

I am doing this equal statement and it's not working.

if ( $1 === $one ) && ( $2 == $two ) {
    require("one.php");
} elseIF($2 === $two) {
    require("two.php");
}else{ 
    require("three.php");
    die("");
}

And this is the error message i get:

   PHP Parse error:  syntax error, unexpected T_BOOLEAN_AND 

Upvotes: 0

Views: 92

Answers (5)

SW4
SW4

Reputation: 71240

You need to do:

if (( $1 === $one ) && ( $2 == $two )) {

Or:

if ( $1 === $one  &&  $2 == $two ) {

Remember to think of it as algebra in where you put brackets, the 'IF@ statement needs to be evaluated as a whole with all criteria you wish to evaluate being wrapped within ( and ). Depending on how complex you wish to make your evaluations (build it formulaically) you can then wrap subsequent criteria in brackets, e.g.

x || y
x && y
x && (y || z)
x || (y && z)

Upvotes: 2

Teekin
Teekin

Reputation: 13299

No, the problem is in the first line.

do:

if ($1 === $one && $2 == $two) { ...

The "if" condition is always within a single "()". You can nest them, but the if-condition stops after the first one is closed. So if (($1 === $one) && ($2 == $two)) is valid as well, but in this case there's no need for nesting it unless you find that it improves readability.

A bit of criticism to the side:

Those variable names are very bad, since they don't explain anything about what's going on. Same with the file names.

And pretty please, with sugar on top, fix your indenting (check out draevor's answer for decent indenting). Get used to proper indenting right away, since it will only get more difficult to get used to a changed practice later. When someone else starts to work with your code, they'll curse your name if you keep indenting like that. It really does matter for readability.

Upvotes: 1

deviousdodo
deviousdodo

Reputation: 9172

if (( $1 === $one ) && ( $2 == $two )) {
    require "one.php";
} elseif ($2 === $two) {
    require "two.php";
} else { 
    require "three.php";
    die("Some message");
}

Upvotes: 1

John Rix
John Rix

Reputation: 6703

You need to wrap the two conditions in your first IF statement in additional brackets:

if (( $1 === $one ) && ( $2 == $two ))

Upvotes: 1

Tobias
Tobias

Reputation: 9380

Should be:

if (( $1 === $one ) && ( $2 == $two )) {
    require("one.php");
} elseIF($2 === $two) {
    require("two.php");
} else { 
    require("three.php");
    die("");
}

Upvotes: 2

Related Questions