Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading