0

I have a php variable and I had to insert into it html code with another variable. ie:

myarray['foo'] = "<p><? echo $var; ?></p>"

but in this way it doesnt' work. How could escape it correctly?

3 Answers 3

2

Just do:

myarray['foo'] = "<p>$var</p>"

PHP handles all the parsing for you.

EDIT:

From your comment about the array, you could print the raw contents of the array (not very helpful for an app, but fine for debugging):

myarray['foo'] = "<p>".print_r($var, true)."</p>";
Sign up to request clarification or add additional context in comments.

2 Comments

@davidethell it work even if the variable is a an array variable? like $array->var ?
@eng_mazzy, no it won't. It will try to cast $var to a string which will just show the text "Array" for an array. You would have to loop through the array to do something with each array or else use print_r. I edited the answer to show it.
1
myarray['foo'] = "<p>" . $var . "</p>"

Comments

1

Can't you just do

my_array['foo'] = '<p>'.$var.'</p>';

Comments

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.