Printing PHP var in PHP string var

I’m trying to print out the value of a variable within another string.

Here is what I’m trying to do:

$output = '<section class="cards cards--$alignment background--black">'

$alignment has the value of left or right. The above renders when inspecting:

<section class="cards cards--$alignment background--mint">

I have also tried:

$output = ' <section class="cards cards--'.$alignment.' background--black">'

But this renders:

<section class="cards cards-- background--black">

And have also tried:

$output = ' <section class="cards cards--{$alignment} background--black">'

But this renders:

<section class="cards cards--{$alignment} background--mint">

I have tried all methods I’ve seen on other SO questions, but can’t get it printing the value?

>Solution :

This code of yours should work

$output = ' <section class="cards cards--'.$alignment.' background--black">'

If it doesn’t then the $alignment variable is just empty or doesn’t exist, try an "echo $alignment" before your $output line to see if you get something.

Leave a Reply