Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

?: 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?

like image 746
Tomate Avatar asked Mar 25 '26 23:03

Tomate


2 Answers

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

like image 73
Izkata Avatar answered Mar 28 '26 12:03

Izkata


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.

like image 26
Phil Cross Avatar answered Mar 28 '26 11:03

Phil Cross