Home > Backend Development > PHP Tutorial > if里面怎么传值计算?

if里面怎么传值计算?

WBOY
Release: 2016-06-23 13:28:01
Original
1093 people have browsed it

$conditin = ' 8>0 '; 

if($conditin){ $value = 1;} else { $value = 2; }


$conditin是外部传进来的
怎么判断它是一个正常的条件语句 并让它执行呢?

如果 $conditin= ‘3 大于 1’那要判断成 条件语句异常的

如果 $conditin = ' 8>0 '; 就返回 1

如果 $conditin = ' 8
是不是要用eval ? 但怎么规避风险 ?


回复讨论(解决方案)

eval() 返回 NULL ,除非在执行的代码中 return 了一个值,函数返回该值。 如果在执行的代码中有一个解析错误, eval() 返回 FALSE ,之后的代码将正常执行。无法使用 set_error_handler() 捕获 eval() 中的解析错误。 \

可以这样改:

$str = '8>0';$condition = 'if(' . $str . '){$value = 1;}else{$value = 2;}';eval($condition);var_dump($value);
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template