Bluehost.com Web Hosting $6.95

HOW TO : execute PHP code contained in a string ?

This is a discussion on HOW TO : execute PHP code contained in a string ? within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Hi, Is there a function to execute PHP code contained in a string ? Example : $toto='<? echo "ca marche&...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-02-2006
zemask
 
Posts: n/a
Default HOW TO : execute PHP code contained in a string ?

Hi,

Is there a function to execute PHP code contained in a string ?
Example :

$toto='<? echo "ca marche"; ?>';

thanks for your help !

Regards,

Fred



Reply With Quote
  #2 (permalink)  
Old 02-02-2006
ZeldorBlat
 
Posts: n/a
Default Re: HOW TO : execute PHP code contained in a string ?


zemask wrote:
> Hi,
>
> Is there a function to execute PHP code contained in a string ?
> Example :
>
> $toto='<? echo "ca marche"; ?>';
>
> thanks for your help !
>
> Regards,
>
> Fred


<http://www.php.net/eval>

Reply With Quote
  #3 (permalink)  
Old 02-02-2006
Cujo
 
Posts: n/a
Default Re: HOW TO : execute PHP code contained in a string ?

zemask wrote:

> Is there a function to execute PHP code contained in a string ?
> Example :


> $toto='<? echo "ca marche"; ?>';


Why the hell would you want to do that ???

Have a look at eval()... i NEVER had to use it, though.

regards, f.
Reply With Quote
  #4 (permalink)  
Old 02-03-2006
ZeldorBlat
 
Posts: n/a
Default Re: HOW TO : execute PHP code contained in a string ?


Cujo wrote:
> zemask wrote:
>
> > Is there a function to execute PHP code contained in a string ?
> > Example :

>
> > $toto='<? echo "ca marche"; ?>';

>
> Why the hell would you want to do that ???
>
> Have a look at eval()... i NEVER had to use it, though.
>
> regards, f.


"If eval() is the answer, you're asking the wrong question."

True most of the time, but not necessarily /all/ of the time.

Reply With Quote
  #5 (permalink)  
Old 02-03-2006
Marnok.com
 
Posts: n/a
Default Re: HOW TO : execute PHP code contained in a string ?


"ZeldorBlat" <zeldorblat@gmail.com> wrote in message
news:1138940613.589720.161670@g44g2000cwa.googlegr oups.com...
>
> Cujo wrote:
>> zemask wrote:
>>
>> > Is there a function to execute PHP code contained in a string ?
>> > Example :

>>
>> > $toto='<? echo "ca marche"; ?>';

>>
>> Why the hell would you want to do that ???
>>
>> Have a look at eval()... i NEVER had to use it, though.
>>
>> regards, f.

>
> "If eval() is the answer, you're asking the wrong question."
>
> True most of the time, but not necessarily /all/ of the time.
>


I think eval() is just the answer I was looking for to a problem I had with
my project. I have an array with key -> string, which picks a random array
entry and echoes the string. Sometimes though, I want the string to contain
instructions to do something else. So, I can put my instructions in [] and
eval anything within.

Just saying that because, I'm surprised people are saying "eval is evil"
when I just happened to read this thread, at exactly the time I was thinking
about how to complete this project, and thought, "bingo!!"




Reply With Quote
  #6 (permalink)  
Old 02-06-2006
Albe
 
Posts: n/a
Default Re: HOW TO : execute PHP code contained in a string ?

On Fri, 03 Feb 2006 19:23:40 +0200, Marnok.com
<wizardharry@pottermarnok.com> wrote:

>
> "ZeldorBlat" <zeldorblat@gmail.com> wrote in message
> news:1138940613.589720.161670@g44g2000cwa.googlegr oups.com...
>>
>> Cujo wrote:
>>> zemask wrote:
>>>
>>> > Is there a function to execute PHP code contained in a string ?
>>> > Example :
>>>
>>> > $toto='<? echo "ca marche"; ?>';
>>>
>>> Why the hell would you want to do that ???
>>>
>>> Have a look at eval()... i NEVER had to use it, though.
>>>
>>> regards, f.

>>
>> "If eval() is the answer, you're asking the wrong question."
>>
>> True most of the time, but not necessarily /all/ of the time.
>>

>
> I think eval() is just the answer I was looking for to a problem I had
> with
> my project. I have an array with key -> string, which picks a random
> array
> entry and echoes the string. Sometimes though, I want the string to
> contain
> instructions to do something else. So, I can put my instructions in []
> and
> eval anything within.
>
> Just saying that because, I'm surprised people are saying "eval is evil"
> when I just happened to read this thread, at exactly the time I was
> thinking
> about how to complete this project, and thought, "bingo!!"


eval() is only evil when you're wreckless with it. The key is to be very
sure that you know where the stuff comes from that you're eval'ing. I
think something like this would be safe:
$code = "echo 'Hello World';";
$result = eval(" ?>".$code."<?php ");
echo $result;

However, if $code is unknown or has a possibility of being unknown, you
can run into trouble very fast. In plain English, never eval user input.

..:A
Reply With Quote
  #7 (permalink)  
Old 02-08-2006
Marnok.com
 
Posts: n/a
Default Re: HOW TO : execute PHP code contained in a string ?


"Albe" <albe@ambientatom.co.za> wrote in message
news:op.s4jk252mth8uo6@equus.up.ac.za...
> On Fri, 03 Feb 2006 19:23:40 +0200, Marnok.com
> <wizardharry@pottermarnok.com> wrote:
>
>>
>> "ZeldorBlat" <zeldorblat@gmail.com> wrote in message
>> news:1138940613.589720.161670@g44g2000cwa.googlegr oups.com...
>>>
>>> Cujo wrote:
>>>> zemask wrote:
>>>>
>>>> > Is there a function to execute PHP code contained in a string ?
>>>> > Example :
>>>>
>>>> > $toto='<? echo "ca marche"; ?>';
>>>>
>>>> Why the hell would you want to do that ???
>>>>
>>>> Have a look at eval()... i NEVER had to use it, though.
>>>>
>>>> regards, f.
>>>
>>> "If eval() is the answer, you're asking the wrong question."
>>>
>>> True most of the time, but not necessarily /all/ of the time.
>>>

>>
>> I think eval() is just the answer I was looking for to a problem I had
>> with
>> my project. I have an array with key -> string, which picks a random
>> array
>> entry and echoes the string. Sometimes though, I want the string to
>> contain
>> instructions to do something else. So, I can put my instructions in []
>> and
>> eval anything within.
>>
>> Just saying that because, I'm surprised people are saying "eval is evil"
>> when I just happened to read this thread, at exactly the time I was
>> thinking
>> about how to complete this project, and thought, "bingo!!"

>
> eval() is only evil when you're wreckless with it. The key is to be very
> sure that you know where the stuff comes from that you're eval'ing. I
> think something like this would be safe:
> $code = "echo 'Hello World';";
> $result = eval(" ?>".$code."<?php ");
> echo $result;
>
> However, if $code is unknown or has a possibility of being unknown, you
> can run into trouble very fast. In plain English, never eval user input.
>
> .:A




amen to that, Albe. I can see the potential for mischief. I'll only eval()
from my own data files.


Reply With Quote
  #8 (permalink)  
Old 02-19-2006
Jim Michaels
 
Posts: n/a
Default Re: HOW TO : execute PHP code contained in a string ?


"zemask" <frederes@free.fr> wrote in message
news:43e22058$0$169$a3f2974a@nnrp1.numericable.fr. ..
> Hi,
>
> Is there a function to execute PHP code contained in a string ?
> Example :
>
> $toto='<? echo "ca marche"; ?>';


if that's withina PHP code section, you might just be sending the PHP
interpreter into a tizzy-fit. that ?> might be interpreted as the end of
the code segment and you end up with '; in your browser. try it and see
what happens. but I don't guarantee any good results between versions of
PHP.

>
> thanks for your help !
>
> Regards,
>
> Fred
>
>
>



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 03:46 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0