How to concatenate in PHP

Posted on: January 26th, 2023
By: Tadeo Martinez

You concatenate with a period “.” in PHP

echo '<span>' . get_field('field_name') . '</span>';

If you want to concatenate to a variable, you can do the following:

$button = "";

$button .= '<a class="btn-main d-inline-block ' . esc_attr($a['class']) . '" href="' . esc_attr($a['href']) . '" style="' . esc_attr($a['style']) . '" target="' . esc_attr($a['target']) . '">';

$button .= '<span class="pt-2 pb-2 pl-4 pr-4 d-inline-block" style="border:1px solid white;">';
$button .= $content;

$button .= '</span>';
$button .= '</a>';

Declare the variable with an empty string and then add to it with “.=”

Have any questions or comments? Write them below!


Leave a Reply

Your email address will not be published. Required fields are marked *