This could be very helpful, for example if you want to use a module or a template, you may have a php script in a string, just like that:
Hello echo $username; ?> . You are wellcome !
If you loads that text in a string, and echo it to the html page, it will not be executed by php.
<?php
$string = "Hello ?> . You are wellcome !";
echo $string;
?>
$string = "Hello ?> . You are wellcome !";
echo $string;
?>
Hello ?> . You are wellcome !
If you have only a php script in a string, you just use eval() to execute:
<?php
$string = "echo $username; ";
eval ( $string ) ;
?>
$string = "echo $username; ";
eval ( $string ) ;
?>
<?php
echo $username;
?>
echo $username;
?>
<?php
$username = "SMITH" ;
$string = "Hello . You are wellcome !";
// This will search for the php script in the string:
$pos = strpos( $tmp ,"" ) ;
$pos2 = strpos ( $tmp , "?>" , $pos ) ;
$scr = substr($tmp,$pos + 5 , $pos2 - $pos -3 ) ;
//This will execute the script.
echo substr($tmp,0,$pos);
eval( " $scr " );
echo substr($tmp,$pos2+2);
?>
$username = "SMITH" ;
$string = "Hello . You are wellcome !";
// This will search for the php script in the string:
$pos = strpos( $tmp ,"" ) ;
$pos2 = strpos ( $tmp , "?>" , $pos ) ;
$scr = substr($tmp,$pos + 5 , $pos2 - $pos -3 ) ;
//This will execute the script.
echo substr($tmp,0,$pos);
eval( " $scr " );
echo substr($tmp,$pos2+2);
?>
Display:
Hello SMITH . You are wellcome !