0

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)

7 Answers 7

3

You cannot use an if statement 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:

$data .= 'h' . $i . '{' . (!empty($size) ? "font-size: $size" : '') . '}';

You could also benefit from the usage of double quotes to interpolate the variables instead of getting messy with the . operator.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't use if in string concatenation, but you can use shortcode:

for ($i = 1; $i < 6; $i++){ 
        $data .= 'h'.$i.'{'.(!empty($size) ? ('font-size: ' .$size. ';') : '').'}';
}

Comments

1

I think it should be without error:

<?php
    for ($i = 1; $i < 6; $i++){ 
        $data .= 'h'.$i.'{' ;
            if ( !empty ($size) ) {
           $data .= 'font-size: ' .$size. ';';
            }
        $data .='}';
    }


?>

Comments

1
for ($i = 1; $i < 6; $i++) {
    $data .= "h$i{" . (!empty($size) ? "font-size: $size;" : "") . "}";
}

Comments

1

You can't string full-blown if's in-line, so either one of the following should fix this:

for ($i = 1; $i < 6; $i++)
{//ternary
    $data .= 'h'.$i.'{'.( !empty ($size) ? 'font-size: ' .$size. ';' : '').'}';
}

Which is the same as writing:

for ($i = 1; $i < 6; $i++)
{
    $data .= 'h'.$i.'{';
    if (!empty ($size))
    {
        $data .= 'font-size: ' .$size. ';';
    }
    $data .= '}';
}

Well, to be 100% accurate, the ternary operator concats '' if $size is empty. The ternary syntax is as follows: [evaluate statement] ? [if true] : [else]. So the truly full-blown equivalent of that code is:

for ($i = 1; $i < 6; $i++)
{
    $data .= 'h'.$i.'{';
    if (!empty ($size))
    {
        $data .= 'font-size: ' .$size. ';';
    }
    else
    {
        $data .= '';
    }
    $data .= '}';
}

3 Comments

} should be outside if statement
Your else statement has no meaning.
I added that to clarify the ternary operator: ? [if true] : [else], when written in full, you wouldn't include the else, but ?: always needs a right side (else) statement, so transcribing the ternary in full leaves you with a pointless else statement, yes, but it's there
1
for ($i = 1; $i < 6; $i++){ 
    $data .= 'h'.$i.'{'.((!empty ($size))?'font-size: {$size}':'').'}';
}

Explanation:

The following construct is called "ternary if":

$x = ($test == "Y")?"Yes!":"No!";

and is the same as:

if ($test == "Y"){
  $x="Yes!";
} else {
  $x="No!";
}

1 Comment

@Yaroslav: I fixed the typo. Thanks. Now it actually works. I'll comment in my answer.
1

You are in the middle of adding stuff to a string in the line starting with $data.

Two solutions:

1) Stop adding stuff, start your if, add more stuff inside, stop if, then add the rest in a new statement.

$data .= 'h'.$i.'{';

if ( !empty ($size) ) {.
    $data .= 'font-size: ' .$size. ';'.
}.

$data .= '}';
  1. Use the ternary operator.

$data .= 'h'.$i.'{' (!empty($size) ? 'font-size: ' .$size. ';' : ''). '}';

The ternary operator evaluates the first argument to either true or false and returns the second or third argument, just like a function call.

Beware: Using ternary operators too much will make your code unreadable, especially when using it inside each other. You can always replace it with a separated IF construct like above for better readability. Plus the ternary operator cannot be easily extended to do more stuff besides returning one of two values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.