help with "eval" function [Solved]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

help with "eval" function [Solved]

Post 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

Code: Select all

var_dump(eval($str));
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?
Last edited by mcog_esteban on Wed Aug 29, 2007 4:39 am, edited 1 time in total.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

1. Variables in PHP begin with $ - all of them, even array variables ;)
2. Try to avoid usage of eval()
There are 10 types of people in this world, those who understand binary and those who don't
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post 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 :D.

What you advice to get the variable

Code: Select all

$post
without using

Code: Select all

eval()
?
Thank you.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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()...
There are 10 types of people in this world, those who understand binary and those who don't
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post 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".
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post by VladSun »

What about including the whole file by using standard include()/require() functions?
Last edited by VladSun on Tue Aug 28, 2007 8:58 am, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
Steve Mellor
Forum Commoner
Posts: 49
Joined: Thu Aug 02, 2007 8:18 am

Post 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.
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post 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.
User avatar
Mordred
DevNet Resident
Posts: 1579
Joined: Sun Sep 03, 2006 5:19 am
Location: Sofia, Bulgaria

Post by Mordred »

A little bird tells me you'll benefit from reading about serialize() and unserialize().
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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...
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post 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).
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post by mcog_esteban »

Thank you all.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

For future reference, if you have solved it... what is the solution you found ?
mcog_esteban
Forum Contributor
Posts: 127
Joined: Tue Dec 30, 2003 3:28 pm

Post by mcog_esteban »

CoderGoblin wrote:For future reference, if you have solved it... what is the solution you found ?
I used stereofrog idea.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Post 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."
There are 10 types of people in this world, those who understand binary and those who don't
Post Reply