English 中文(简体)
如何避免将等值用于需要实施的特定变量
原标题:How to avoid using eval for a particular string of variables that needs to be executed
  • 时间:2010-12-09 08:52:34
  •  标签:
  • php
  • eval

我有这样的情况,使用<代码>eval():

$result = eval("return ".$value1.$operator.$value2.";");

如果经营者来自变数或干.。

如果不使用<代码>eval,我将如何取得相同结果? 是否可能?

它不是一种安全关切,因为价值/操作器是固定用户,但是,如果(http://www.php.net/manual/en/Function.eval.php#82239”rel=“nofollow”的话,在《个人生活手册》中发表“评论是没有任何意义的。 此外,如果我想在某个时候尝试用“Facebook s HipHop I”来取代“eval<>/code”的所有用途。

最佳回答
if(strcmp($operator, "+") == 0) {  
  return   $value1 + $value2;
}  
else if(strcmp($operator, "*") == 0) {  
return   $value1 * $value2;  
}  
...  

As @Gumbo mentioned, you can also use switch()

问题回答

当然,我不相信经营者,但你可以以职能名称这样做:

function add($x, $y) {
    return $x + $y;
}

$value1 = 1;
$value2 = 2;
$func = "add";

$result = $func($value1, $value2);

即便如此,你也能够发挥内在作用:

$func =  array_sum ;
$result = $func(array($value1, $value2));




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...