0

I am using this code that returns an array:

$values = get_field('sp_specialism', $prev_id);var_dump($values);

The output of the array is:

array(3) { [0]=> string(4) "Asia" [1]=> string(6) "Canada" [2]=> string(9) "Caribbean" } 

I am trying to loop through the array and output Asia Canada Caribbean as list items

if($values) { echo '<ul>'; foreach($values as $value) { echo '<li>' . $value . '</li>'; } echo '</ul>'; } 

However my if statement is erroring out. Not sure why this happens.

I am using this within another piece of code like this:

$string = "<span class=\"bkcards-exp\">" . get_field('sp_location', $prev_id) . "</span><span class=\"bkcards-left\">" . if($values) { echo '<ul>'; foreach($values as $value) { echo '<li>' . $value . '</li>'; } echo '</ul>'; } . "</span>" 
5
  • Can you specify the error? Commented Aug 27, 2022 at 9:02
  • PHP Parse error: syntax error, unexpected 'if' (T_IF) Commented Aug 27, 2022 at 9:05
  • I am using this inside a $string variable. could that be the issue ? Commented Aug 27, 2022 at 9:06
  • this error may mean that you are missing a semicolon in your code? What do you mean by $string variable? Commented Aug 27, 2022 at 9:14
  • ok I have added the full code that this statement is being used in Commented Aug 27, 2022 at 13:09

1 Answer 1

1

You can't concatenate an if statement with a string. Instead, you can divide the string into multiple different statements as shown in the following snippet.

$string = '<span class="bkcards-exp">' . 
               get_field('sp_location', $prev_id) . 
          '</span><span class="bkcards-left">';

if($values) { 
    $string .= '<ul>'; 
    foreach($values as $value) { 
        $string .= '<li>' . $value . '</li>'; 
    } 
    $string .= '</ul>';
}

$string .= '</span>';
Sign up to request clarification or add additional context in comments.

2 Comments

But I need the output to be assigned to $string because it's being used in another part of the code
Using echo is not what I would like here....

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.