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&...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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> |
|
|||
|
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. |
|
|||
|
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. |
|
|||
|
"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!!" |
|
|||
|
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 |
|
|||
|
"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. |
|
|||
|
"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 > > > |