swizzer Posted November 27, 2008 Share Posted November 27, 2008 I'm creating a form class, and at one point i need to show the information before confirming, using a function showInfo(). I want to be able to accept a $format string, as to be able to change the format if one is provided Works function showInfo($format = ''){ if ($format == '') { $theformat = '<div> {$field->value} </div>';} else { $theformat = $format;} foreach ($this->fields as $field){ echo "<div> {$field->value} </div>"; } } Shows ' {field->value}' on the page instead the the actual value function showInfo($format = ''){ if ($format == '') { $theformat = '<div> {$field->value} </div>';} else { $theformat = $format;} foreach ($this->fields as $field){ echo "$theformat"; } } $this->fields is an array with instances of fields, the property i'm trying to get is 'value' Appreciate it ! Link to comment https://forums.phpfreaks.com/topic/134562-solved-string-not-being-evaluated/ Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 You'll need to eval the string. Link to comment https://forums.phpfreaks.com/topic/134562-solved-string-not-being-evaluated/#findComment-700643 Share on other sites More sharing options...
DarkWater Posted November 27, 2008 Share Posted November 27, 2008 You'll need to eval the string. What? Your problem is that you're using single quotes. Variables are not interpolated inside ' ', only " ". Link to comment https://forums.phpfreaks.com/topic/134562-solved-string-not-being-evaluated/#findComment-700647 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 The way I read it, instaed of simply echo'ing... echo "$theformat"; He wants the string contained within echo $theformat evaluated. Link to comment https://forums.phpfreaks.com/topic/134562-solved-string-not-being-evaluated/#findComment-700649 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 Which indeed would evaluate if this line.... $theformat = '<div> {$field->value} </div>'; Used double quotes. Link to comment https://forums.phpfreaks.com/topic/134562-solved-string-not-being-evaluated/#findComment-700652 Share on other sites More sharing options...
swizzer Posted November 27, 2008 Author Share Posted November 27, 2008 the problem with using "" originally, is that {$field->value} has no value the first time. So I do need to use the eval code like thorpe said, I just need to understand eval() because it needs some kind of eval_code string which i need to figure out. Thanks to both of you Link to comment https://forums.phpfreaks.com/topic/134562-solved-string-not-being-evaluated/#findComment-700654 Share on other sites More sharing options...
swizzer Posted November 27, 2008 Author Share Posted November 27, 2008 Solution function showInfo($format = ''){ if ($format == '') { $theformat = 'echo \'<div>\'; echo $field->value; echo \'</div>\';';} else { $theformat = $format;} foreach ($this->fields as $field){ eval($theformat); } } Link to comment https://forums.phpfreaks.com/topic/134562-solved-string-not-being-evaluated/#findComment-700656 Share on other sites More sharing options...
trq Posted November 27, 2008 Share Posted November 27, 2008 I think you'd be better of simply using str_replace and a placeholder. eg; function showInfo($format = ''){ if ($format == '') { foreach ($this->fields as $field) { echo "<div> {$field->value} </div>"; } } else { foreach ($this->fields as $field) { echo str_replace('%s', $field->value, $format); } } } Then you calling code might look like.... $obj->showInfo('<b>%s</b>'); eval is ually always the wrong solution as it can easily allow unwanted code execution. Link to comment https://forums.phpfreaks.com/topic/134562-solved-string-not-being-evaluated/#findComment-700659 Share on other sites More sharing options...
swizzer Posted November 27, 2008 Author Share Posted November 27, 2008 yeah that's an even cleaner solution, and the calls are much easier, thanks! Link to comment https://forums.phpfreaks.com/topic/134562-solved-string-not-being-evaluated/#findComment-700670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.