3

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?

0

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

3

its very important to escape the output. (security basics)

$html = sprintf('<b>%s</b>', htmlspecialchars($lang['text']));

Comments

2
<?php

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

?>

Comments

2

You can't switch from "Output raw text mode" to "Run PHP code mode" in the middle of a string while you are already in "Run PHP code mode"

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

… although why you want an empty bold element is beyond me.

1 Comment

Judging by the question, this answer is not something the poster will understand.. This is a comment rather than an answer imho.
-5

make sure file extension is php.

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

1 Comment

I believe this is even worse than the actual question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.