Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php code inside variable with html code

I want to add code php to variable with html, for example

$html = '<b></b> <?php echo $lang["text"] ?>';

but it don't interpret php code. What am I doing wrong?

like image 299
cadi2108 Avatar asked Aug 05 '26 21:08

cadi2108


2 Answers

Use string concatenations like this:

$html = '<b></b>' . $lang['text'];

or insert variable in double quoted string like this:

$html = "<b></b>${lang['text']}";

both versions are correct, use the one that you like.

like image 128
c0deMaster Avatar answered Aug 07 '26 12:08

c0deMaster


What you want is called string interpolation (read about how it works for PHP).

Your particular example would be solved using

$html = "<b></b> {$lang['text']}";

String interpolation only happens in double quoted string ("string here").

like image 45
adamse Avatar answered Aug 07 '26 14:08

adamse