Question:
Is it advisable to utilize eval() in PHP, especially when a more elegant alternative, such as the one provided below, is available?
$type = "enum('a','b','c')"; // Option one $type_1 = preg_replace('#^enum\s*\(\s*\'|\'\s*\)\s*$#', '', $type); $result = preg_split('#\'\s*,\s*\'#', $type_1); // Option two eval('$result = '.preg_replace('#^enum#','array', $type).';');
Answer:
While eval() has a reputation for being undesirable, it's not inherently evil. Its dynamic evaluation capabilities can be advantageous. However, caution is necessary due to the following potential pitfalls:
Rule of Thumb:
The above is the detailed content of When Should You Avoid Using `eval()` in PHP?. For more information, please follow other related articles on the PHP Chinese website!