Mariton
Mariton

Reputation: 621

How do you string a variable together along with a string in PHP?

So I have a variable in PHP that I would like to print along with an html tag but I am very new to PHP and I can't seem to get the syntax right

My goal is to output/print the following

<div class="post-block" style="background-image:url(Whatever image here);">

My code is below:

    $feature_posts = the_sub_field('feature_image_post');

      if ( get_sub_field('post-feature') == "" )  {       
        echo '<div class="post-block" style="background-image:url(' .$feature_posts");>';
        echo '</div>'; 
      } 

Upvotes: 0

Views: 58

Answers (2)

Igor Paiva
Igor Paiva

Reputation: 683

You can make that.

<?php for($i = 0; $i <= 100; $i++){ ?>
    <div class="post-block" style="background-image:url(<?php echo the_sub_field('feature_image_post') ?>);"></div>

<?php } ?>

This a sample how you can do.

Upvotes: 0

Tavetsu
Tavetsu

Reputation: 11

You are missing the concatenation operator and the closing single quote after your variable:

$feature_posts = the_sub_field('feature_image_post');

if ( get_sub_field('post-feature') == "" )  {       
    echo '<div class="post-block" style="background-image:url(' . $feature_posts . ')">';
    echo '</div>'; 
} 

To keep the format more readable you can use double quotes to allow variables in your string. Just have to remember escaping the quotes within the markup:

$feature_posts = the_sub_field('feature_image_post');

if ( get_sub_field('post-feature') == "" )  {       
    echo "<div class=\"post-block\" style=\"background-image:url($feature_posts)\">";
    echo '</div>'; 
}

Upvotes: 1

Related Questions