简体   繁体   中英

?: in old version of PHP causing parse error

I've been using the SimpleImage class for image manipulation but it doesn't work on one particular site the version of PHP on the server is 5.1.6 - so six years old

Parse error: syntax error, unexpected ':' in…

the lines in question which cause the error being

$height = $height ?: $width; 
$quality = $quality ?: $this->quality;
$filename = $filename ?: $this->filename;

Is there a workaround for this?

The ternary operator shorthand $val1 ?: $val2 was introduced in PHP 5.3 , and is identical to $val1 ? $val1 : $val2 $val1 ? $val1 : $val2

Because you're missing an argument in your ternary operator, it should be this syntax:

$height = $height ? $height : $width;
$quality = $quality ? $quality : $this->quality;
$filename = $filename ? $filename : $this->filename;

There should be 2 variables following the ? symbol, separated by a : symbol. The first variable is what is set if the condition (before the ?) is true. The second variable is what is set if the condition is false.

EDIT:

The syntax ?: is only available since PHP version 5.3, make sure you're running php 5.3 or greater.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM