PHP multi-line echo not putting variables within the correct divs?

I am working on a PHP loop for WordPress and populating the content with variables.

Currently I have an echo to produce the HTML blocks for the loop that looks like this:

Read More
echo '<div class="col-sm-4 retailer" data-state="'.$state.'">
        <div class="locationName">'.the_title().'</div>
        <div class="locationAddress">'.the_field("address").'</div>
      </div>';

However, in the outputted HTML the 2 variables(the_title and the_field(“address”)) are being placed below the content block instead of within their respective divs like this:

<div class="col-sm-4 retailer" data-state="Alaska">
    <div class="locationName"></div>
    <div class="locationAddress"></div>
</div>Location #1 81234 Sample Dr.

Can anyone tell me why the variables aren’t being contained to the correct divs? Thanks!

Related posts

Leave a Reply

2 comments

  1. Here is the solution that ended up working for me based on tips from @ErkiA and @Twisted1919:

    echo '<div class="col-sm-4 retailer" data-state="'.$state.'">
            <div class="locationName">'.get_title().'</div>
            <div class="locationAddress">'.get_field("address").'</div>
          </div>';
    

    get_title() grabs the title of the WordPress post and get_field("address") retrieves the value from a custom field I’ve set up using Advanced Custom Fields.

  2. $title =the_title()
    $address = the_field("address");
    echo "<div class='col-sm-4 retailer' data-state='$state'>
            <div class='locationName'>$title</div>
            <div class='locationAddress'>$address</div>
          </div>";