-2
function a(){
//logic here
//else return false
}

function b(){
//logic here
//else return false
}

function c(){
//logic here
//else return false
}

a() ? b() ? c(); 

I want to run a first, if false go to b if false go to c but I got unexpected ';' error. One more doubt : if a() return false will it go to b()? or I need to set like if(a() != true) ?

2 Answers2

1
a() ? b() ? c()

is only valid as the start of

a() ? b() ? c() : ... : ...

which is why PHP complains of an unexpected ;. You can read more about the conditional operator here.


You want

a() || b() || c();

or

a() or b() or c();

PHP, like many languages, short circuit boolean algebra so only as much is evaluated, so it'll stop evaluating the chain when one of the terms returns true.

Community
  • 1
  • 1
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • written the wrong id of duplicate post ... reopened question but still i think its dupe of http://stackoverflow.com/questions/1080247/what-is-the-php-operator-called-and-what-does-it-do – NullPoiиteя Sep 20 '14 at 04:37
  • @NullPoiиteя, It doesn't solve the OP's problem at all. – ikegami Sep 20 '14 at 18:13
0

Proper syntax for operator ?: is $sth? $forTrue : $forFalse;. So if you really want to do this using it, your last line should look like:

a() ? null : (b() ?  null : c());

But I think it looks better and be easier to read if you use if statement.

if (!a()) {
    if(!b()) {
        c();
    }
}
marian0
  • 3,336
  • 3
  • 27
  • 37