Ternary Operator not working in godaddy php (parsererror) [closed]
Closed 6 years ago.
-
开发者_StackOverflow中文版
- Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
- This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
<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?
Thanks!
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'));
The syntax ? :
you are using is PHP 5.3 only.
Set a default value:
$conditions = ($this->input->post()) ? $this->input->post() : array('tutor'=>$this->session->userdata('user_id'));
The accepted answer is correct, but misses one important point:
$x = $a ? : $b; // valid in PHP 5.3
should indeed be replaced with
$x = $a ? $a : $b; // valid in older versions of PHP
However, when you're dealing with functions, not variables, be aware of side-effects:
$conditions = ($this->input->post())
? ($this->input->post())
: array('tutor'=>$this->session->userdata('user_id'));
In the above case, if $this->input->post()
returns a truthy value, the function will be executed again, which may not be what you want. You can see this more clearly by expanding the ternary operator to its full form:
if ($this->input->post()) {
$conditions = $this->input->post();
} else {
$conditions = array('tutor' => $this->session->userdata('user_id'));
}
You can see that the function is executed on lines one and two above. If you don't want that, try this instead:
if (!$conditions = $this->input->post()) {
// Single equal sign in an if condition: make assignment, and check
// whether the result is truthy.
$conditions = array('tutor' => $this->session->userdata('user_id'));
}
This is a fairly common pattern in my own code. This will execute the function $this->input->post()
only once. If the result is truthy, that result is stored in $conditions
. If the result is not truthy, the code inside the if
condition is run. This assigns the fallback value to $conditions
. The benefit is that, in either case, $this->input->post()
is run only once.
精彩评论