How to write PHP tags in PHP string? – PHP

  php

Q(Question):

Hello,

I have the situation where I need to have a string where I need to include a PHP tag. Like $myString = "this is my string <?php echo $andthisismytag; ?>".

Constructing the string is OK without errors but then when I echo it it’s empty where the PHP tag used to be. In fact it is an SQL INSERT INTO string, but I never get as far as to executing the query. The problem is in the actual string.

Any clues?

Thanks!

A(Answer):

I am not sure exactly what you want to do or what do you mean by php tag… looks like you are adding strings and for that all you need is a dot… like this:


$andthisismytag = "sample string"
$myString = "this is my string ".$andthisismytag ;
echo $myString;

This will produce/echo: this is my string sample string

But than again maybe I didn’t understand what are you trying to do.

A(Answer):

Nope, that’s not it.
What I want to do is to put PHP code into a mySQL database, and I need to wrap it in PHP tags.

Say that my SQL attribute will need to contain <?php echo "hello";?>

The string I’m putting my PHP code in is, then, the query string that I need to put it in there.

Is it clearer? 🙂

A(Answer):

Hmm. You’re wanting to store unevaluated PHP code in a MySQL database and then later, when you retrieve that data, have it be parsed by the PHP engine. If you want to do that, you’ll need to call eval() on the data. However, I do not believe this to be a good design. Why must you store the PHP in a database?

A(Answer):

The reason is that I’m building a Joomla site and I want to automatically generate articles with code content, without the user having to manually add them.

A(Answer):

Hmmm … I have done few components and modules for joomla and never needed to use this approach. What do you mean by "automatically generate articles" ?

A(Answer):

hmmm why don’t you just create a php file dynamically from you datbase and then display the result.?

A(Answer):

Yeah, like everyone’s saying… You’re doing it backwards. The database holds raw DATA only. Data is not meant to be executed. Data is just meaningless data. In order to give the data meaning, you take it out of the database and then put it into context. Once this is done, the data becomes information.

You should be more clear as to what you’re after.

LEAVE A COMMENT