mazlix mazlix - 1 year ago 67
PHP Question

Ternary Operator not working in godaddy php (parsererror)

<br />↵<b>Parse error</b>: syntax error, unexpected ':' in <b>.../ajax.php</b> on line <b>87</b>

LINE 87:
$conditions = ($this->input->post()) ? : array('tutor'=>$this->session->userdata('user_id'));

line 87 works fine on localhost, but when I use godaddy I get that error. Is there something I need to set in php.ini or something to get Ternary operators to work?


Answer Source

The ternary operator (as the name suggests) usually expects 3 arguments

$var = $expr ? $trueValue : $falseValue;

With PHP5.3 its allowed to omit $trueValue. In this case its $expr is used for it

$var = $expr ? : $falseValue;
// same as
$var = $expr ? $expr : $falseValue;

You probably don't have PHP5.3 on your server. As you can see in my example its quite easy to fix this and make it ready for pre-5.3

$conditions = ($this->input->post()) 
            ? ($this->input->post()) 
            : array('tutor'=>$this->session->userdata('user_id'));