Page 1 of 2
help with "eval" function [Solved]
Posted: Tue Aug 28, 2007 8:17 am
by mcog_esteban
Hello.
How can i cast a string like :
Code: Select all
$str = "post[0] = array('Field1', 'Field2', 'Field3');";
to an array ?
I have tryed
and gave me
Parse error: syntax error, unexpected '=' in /opt/lampp/htdocs/interacesso2/consola/ex1.php(5) : eval()'d code on line 1
i know that the string must be ended with a semicolon, but the semicolon is there, or not?
Posted: Tue Aug 28, 2007 8:22 am
by VladSun
1. Variables in PHP begin with $ - all of them, even array variables

2. Try to avoid usage of eval()
Posted: Tue Aug 28, 2007 8:27 am
by mcog_esteban
VladSun wrote:1. Variables in PHP begin with $ - all of them, even array variables

2. Try to avoid usage of eval()
About 1, it was a bad copy and paste

.
What you advice to get the variable
without using
?
Thank you.
Posted: Tue Aug 28, 2007 8:34 am
by VladSun
Well, it is not a simple typo
1. You have to write it in such way that the $post in the string is interpreted literally, not as variable

2. In your case eval() returns NULL, so there isn't anything to var_dump
3. Post more of your code to get the idea why you are using eval()...
Posted: Tue Aug 28, 2007 8:43 am
by mcog_esteban
There's not much code to post, i want to read file of strings like that
Code: Select all
$post[0] = array('title0','post text0','data0');$post[1] = array('title1','post text1','data1');...
and reproduce it on a "Array".
Posted: Tue Aug 28, 2007 8:58 am
by VladSun
What about including the whole file by using standard include()/require() functions?
Posted: Tue Aug 28, 2007 8:58 am
by Steve Mellor
Ca I just ask why you want to read it like that? What is the problem you are trying to solve. There may be a different way around it.
Posted: Tue Aug 28, 2007 9:08 am
by mcog_esteban
Steve Mellor wrote:Ca I just ask why you want to read it like that? What is the problem you are trying to solve. There may be a different way around it.
I was trying experiments with eval(), obviously keep information in a text file like that is not the best way to do.
Posted: Tue Aug 28, 2007 1:48 pm
by Mordred
A little bird tells me you'll benefit from reading about serialize() and unserialize().
Posted: Tue Aug 28, 2007 5:28 pm
by CoderGoblin
Would something like this be appropriate.
Code: Select all
foreach ($_POST['title'] as $key=>$value) {
$myvar[$key]=array('title'.$key,$_POST['title'][$key],'data'.$key);
}
If not possibly use
variable variables...
Posted: Wed Aug 29, 2007 3:20 am
by stereofrog
mcog_esteban wrote:There's not much code to post, i want to read file of strings like that
Code: Select all
$post[0] = array('title0','post text0','data0');$post[1] = array('title1','post text1','data1');...
and reproduce it on a "Array".
Code: Select all
$str = "\$post[0] = array('Field1', 'Field2', 'Field3');";
eval($str);
print_r($post);
note that in your $str the dollar sign should be escaped (or the string must be in single quotes).
Posted: Wed Aug 29, 2007 3:58 am
by mcog_esteban
Thank you all.
Posted: Wed Aug 29, 2007 4:11 am
by CoderGoblin
For future reference, if you have solved it... what is the solution you found ?
Posted: Wed Aug 29, 2007 4:41 am
by mcog_esteban
CoderGoblin wrote:For future reference, if you have solved it... what is the solution you found ?
I used stereofrog idea.
Posted: Wed Aug 29, 2007 5:05 am
by VladSun
mcog_esteban wrote:CoderGoblin wrote:For future reference, if you have solved it... what is the solution you found ?
I used stereofrog idea.
"Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime."