The following code gives syntax error because of IF. How can I fix this without removing IF statement?
<?php
for ($i = 1; $i < 6; $i++){
$data .= 'h'.$i.'{'.
if ( !empty ($size) ) {.
'font-size: ' .$size. ';'.
}.
'}';
}
?>
Error:
Parse error: syntax error, unexpected 'if' (T_IF)
You cannot use an
ifstatement within an expression, because it is syntactically wrong and the statement itself doesn’t return a value. Use the ternary?:operator which can be nested there:You could also benefit from the usage of double quotes to interpolate the variables instead of getting messy with the
.operator.