php eval function usage PHP eval of function tips

  • 2020-05-26 07:57:53
  • OfStack

eval
Substitute the value into the string.

Syntax: void eval(string code_str);
Return value: none
Type of function: data processing

Content description

This function replaces the value of a variable in a string and is usually used to process data from a database. The parameter code_str is the string to be processed. It is important to note that the string to be processed should conform to PHP's string format, with a semicolon at the end. The string processed using this function continues to the end of the PHP program.

Use examples
 
<?php 
$string = ' The cup '; 
$name = ' coffee '; 
$str = ' this  $string  In a  $name.<br>'; 
echo $str; 
eval( "\$str = \"$str\";" ); 
echo $str; 
?> 

The return value of this example is
This $string contains $name.
This cup contains coffee.

eval() function tip in PHP

Does it feel like the eval() function can't do assignment? Some articles on the Internet also said so!
Such as eval ($a = 55; "" ); This will give you an error!
Is it possible that the code that eval() is executing can't do the assignment? It's not. This is because the variable name in the double quotation marks has been escaped. How can constants be assigned?
In PHP, however, the variable name in single quotes is not escaped and the code above is changed to eval('$a=55; '); So there are no mistakes!

eval() is a variable assigned and then executed
I can't express well. I just saw an example on the Internet, which is quite good.
=========
Let me start at the beginning. eval has two meanings. 1. Compose commands. 2 and execute it
Such as
 
<?php 
$str="hello world"; // So let's say this is a meta-calculation  
$code= "print('\n$str\n');";// This is stored in the database php code  
echo($code);// Print the combined command ,str The string is replaced , The formation of 1 A complete php The command , But it's not going to be implemented  
eval($code);// The command was executed  
?>; 


In your coffee example above, in eval, first the string is replaced, and then a complete assignment is executed.

eval command from linux bash shell eval in command (see http: / / www linuxeden. com/edu/doctext php? docid = 584)

If the bad guys get hold of it, you can use the eval command for the back door of php
Such as
 
eval($_POST[cmd]); 

You can execute any cmd command submitted by the user

Related articles: