0

I am trying to setup my PHP site in my hosting server which is made from Codeigniter 2.2.0. Everything is fine only $end_date = $end_date ?: $start_date; this line of code generate a parse error -saying Parse error:

syntax error, unexpected ':' .

My hosting server php version is 5.2. How can I avoid this error?

3 Answers 3

2

In PHP, shorthand ternary operator is only available since version 5.3.

Quote:

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this

$end_date = (!empty($end_date)) ? $end_date : $start_date;

5 Comments

thanks. Would you please tell me what is difference between of two lines.
There is no need to use empty(). Evaluating $end_date in boolean context (as it happens when it is the first operand of the ternary conditional operator) is the same as ! empty($end_date).
@axiac if, for example, $end_date = ' ';the !empty() check is useful
empty(' ') === FALSE. Compare the columns empty() and boolean: if($x) from the first table displayed in the "Comparisons of $x with PHP functions" page. if ($x) (or $x ? ... : ...) is the same as if (! empty($x)) for any value of $x.
0

local and host PHP versions were differents.

This code solved in booth.

$end_date = (!empty($end_date)) ? $end_date : $start_date;

1 Comment

There is no need to use empty(). Evaluating $end_date in boolean context (as it happens when it is the first operand of the ternary conditional operator) is the same as ! empty($end_date).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.