I have the following code

<a href="<'. Url::to(['site/campaign']) .'" class="pull-right text-green"> '. HtmlPurifier::process($camp['CampaignStatus'] < 1 ? 'Intiated' : (($camp['CampaignStatus'] < 2 ? 'Drafted' )))  . '</a> 

but it's giving me the following error:

ParseError syntax error, unexpected ')'

Can anyone see what's going wrong here or how to fix it?

Dani AI

Generated

posted a snippet that produced "ParseError syntax error, unexpected ')'", and correctly pointed out the root cause: the inner ternary in the expression was missing its "false" branch. A PHP ternary always needs both the true and false expressions (the part after ? and the part after :). Leaving the false branch out makes the parser hit a ) where it was still expecting an expression.

A minimal broken example (same class of error) looks like:

$label = $status < 1 ? 'Started' : ($status < 2 ? 'Pending');

The inner conditional has no : alternative, so PHP throws the parse error. Fix it by supplying the final value:

$label = $status < 1 ? 'Started' : ($status < 2 ? 'Pending' : 'Complete');

For clarity and maintainability prefer moving logic out of a long inline expression. An equivalent, easier-to-read approach:

if ($status < 1) {
    $label = 'Started';
} elseif ($status < 2) {
    $label = 'Pending';
} else {
    $label = 'Complete';
}

echo \yii\helpers\Html::a(\yii\helpers\HtmlPurifier::process($label), ['site/campaign'], ['class' => 'pull-right text-green']);

Quick troubleshooting tips: run php -l filename.php to lint the file; temporarily assign subexpressions to variables to isolate the error; and watch for missing colons, quotes or parentheses. Also avoid nesting ternaries for more than two branches—use if/elseif/else or switch for readability. Finally, correct small typos in labels (for example, "Initiated") so the output is clean.

Recommended Answers

All 4 Replies

You're missing your second option in the second ternary operator:

($camp['CampaignStatus'] < 2 ? 'Drafted' : <SHOULD BE SOMETHING HERE> )

Thanks for your help hericle . I just a newbie into programming world. It would really helpful if you can give me an example what you mean by <SHOULD BE SOMETHING HERE>

Solved it.

Good to hear.
For clarification for any others that find this, the initial code was an if statement without an elseif.
$camp['CampaignStatus'] < 2 ? 'Drafted'

It was incomplete as it asked

if ($camp['CampaignStatus']  < 2 ) {
        return 'Drafted';
} else if() {

}

which won't compile in any language.

commented: Always good to see an explanation for the benefit of others. +1 +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.