473,394 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

php variable continaing php code

Hi There,
I am absolutly stumped by this, any help appreciated.
Due to factors outside of my control I have a php variable containing
php code and I dont know how to evaluate it.

I know its stupid, but php calls a database function (which I dont have
access to) which reads in a template html file and returns it as a
string. Now If I want the template to be a bit more dynamic and contain
php I have a problem.

After calling the database function I end up with: (examaple only)
$var = "<?echo "hello world";?>";
//$var is now equal to the template file - usually html but attempting
to add php

now if I echo that out I get:
<?echo "hello world";?>
but I want:
hello world

Can I call another instance of the php interpreter on php variable
$var?
Would the eval() function work?

Any way to acheive what I am after?

Thanks

Sep 15 '06 #1
3 1129
jd**@xtra.co.nz wrote:
Hi There,
I am absolutly stumped by this, any help appreciated.
Due to factors outside of my control I have a php variable containing
php code and I dont know how to evaluate it.

I know its stupid, but php calls a database function (which I dont
have access to) which reads in a template html file and returns it as
a string. Now If I want the template to be a bit more dynamic and
contain php I have a problem.

After calling the database function I end up with: (examaple only)
$var = "<?echo "hello world";?>";
//$var is now equal to the template file - usually html but attempting
to add php

now if I echo that out I get:
<?echo "hello world";?>
but I want:
hello world

Can I call another instance of the php interpreter on php variable
$var?
Would the eval() function work?

Any way to acheive what I am after?
Use eval() - did you try it? Note that you don't need the <? and ?>
tags. Example:

$var = 'echo "hello world";';
eval($var);

This will display 'hello world' which is what you are wanting.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Sep 15 '06 #2
Chris Hope wrote:
jd**@xtra.co.nz wrote:
>Hi There,
I am absolutly stumped by this, any help appreciated.
Due to factors outside of my control I have a php variable containing
php code and I dont know how to evaluate it.

I know its stupid, but php calls a database function (which I dont
have access to) which reads in a template html file and returns it as
a string. Now If I want the template to be a bit more dynamic and
contain php I have a problem.

After calling the database function I end up with: (examaple only)
$var = "<?echo "hello world";?>";
//$var is now equal to the template file - usually html but attempting
to add php

now if I echo that out I get:
<?echo "hello world";?>
but I want:
hello world

Can I call another instance of the php interpreter on php variable
$var?
Would the eval() function work?

Any way to acheive what I am after?

Use eval() - did you try it? Note that you don't need the <? and ?>
tags. Example:

$var = 'echo "hello world";';
eval($var);

This will display 'hello world' which is what you are wanting.

Also, if you start evalling values from the database, be VERY sure you can
trust them.
If they come from userinput (forms for example) they can contain anything.
Be paranoid.

Regards,
Erwin Moller
Sep 15 '06 #3

Erwin Moller wrote:
Chris Hope wrote:
jd**@xtra.co.nz wrote:
Hi There,
I am absolutly stumped by this, any help appreciated.
Due to factors outside of my control I have a php variable containing
php code and I dont know how to evaluate it.

I know its stupid, but php calls a database function (which I dont
have access to) which reads in a template html file and returns it as
a string. Now If I want the template to be a bit more dynamic and
contain php I have a problem.

After calling the database function I end up with: (examaple only)
$var = "<?echo "hello world";?>";
//$var is now equal to the template file - usually html but attempting
to add php

now if I echo that out I get:
<?echo "hello world";?>
but I want:
hello world

Can I call another instance of the php interpreter on php variable
$var?
Would the eval() function work?

Any way to acheive what I am after?
Use eval() - did you try it? Note that you don't need the <? and ?>
tags. Example:

$var = 'echo "hello world";';
eval($var);

This will display 'hello world' which is what you are wanting.
I don't think eval() will do what you need, since the template can
contain a liberal mix of html and php. You could, however, just dump
the variable contents to a temporary file, and then include() it when
you need the output.
>
Also, if you start evalling values from the database, be VERY sure you can
trust them.
If they come from userinput (forms for example) they can contain anything.
Be paranoid.

Regards,
Erwin Moller
I agree - you may want to consider some sort of templating system for
this. It is quite an overhead but if you do accept user input as
templates, you should make sure they're very restricted rather then
trusted (as you can't really trust them).

I hope this helps,
Vladislav

Sep 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Donna Hawkins | last post by:
I want to use javascript to redirect to a URL which has been passed as a variable (in php). I have searched but cannot find any solution. I think this code is a basic redirect: <script...
7
by: YGeek | last post by:
Is there any difference between declaring a variable at the top of a method versus in the code of the method? Is there a performance impact for either choice? What about if the method will return...
7
by: Klaus Johannes Rusch | last post by:
Is the following code valid and supported by current implementations? function somename() { this.show = function () { document.write("somename called") } } var somename = new somename();...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
9
by: Shapper | last post by:
Hello, I am declaring a variable in my aspx.vb code as follows: Public Class catalogue Public productid As String Private Sub Page_Load ... I have an image button where I call the...
3
by: markaelkins | last post by:
Hi. I am trying to enter a variable in the treenodesrc of a treenode. I am basically trying to send an ID variable into sql to return different records. I've searched everywhere and cannot find the...
13
by: GGawaran | last post by:
First off, Hi everyone new to .asp and am trying to self teach myself. Im trying to figure out what exactly im doing wrong, or maybe what I think I can do, I really cant. The Idea....
1
pbmods
by: pbmods | last post by:
VARIABLE SCOPE IN JAVASCRIPT LEVEL: BEGINNER/INTERMEDIATE (INTERMEDIATE STUFF IN ) PREREQS: VARIABLES First off, what the heck is 'scope' (the kind that doesn't help kill the germs that cause...
0
MMcCarthy
by: MMcCarthy | last post by:
We often get questions on this site that refer to the scope of variables and where and how they are declared. This tutorial is intended to cover the basics of variable scope in VBA for MS Access. For...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.