Jump to content

[SOLVED] String not being evaluated


swizzer

Recommended Posts

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

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

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.