PHP 使用字符串作为运算符

发布于 2024-11-03 12:45:48 字数 153 浏览 1 评论 0原文

假设我有一个字符串,$char。 $char == "*".

我还有两个变量,$a 和 $b,分别等于“4”和“5”。

我如何获得 $a $char $b 的结果,即 4 * 5 ?

谢谢:)

Say I have a string, $char. $char == "*".

I also have two variables, $a and $b, which equal "4" and "5" respectively.

How do I get the result of $a $char $b, ie 4 * 5 ?

Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

嗳卜坏 2024-11-10 12:45:48

您可以按照@konforce的建议使用eval(),但是最安全的路线是这样的:

$left = (int)$a;
$right = (int)$b;
$result = 0;
switch($char){

  case "*":
    $result = $left * $right;
    break;

 case "+";
   $result = $left + $right;
   break;
// etc

}

You can use eval() as suggested by @konforce, however the safest route would be something like:

$left = (int)$a;
$right = (int)$b;
$result = 0;
switch($char){

  case "*":
    $result = $left * $right;
    break;

 case "+";
   $result = $left + $right;
   break;
// etc

}
执笏见 2024-11-10 12:45:48

最安全的方法是 switch 结构:

function my_operator($a, $b, $char) {
    switch($char) {
        case '=': return $a = $b;
        case '*': return $a * $b;
        case '+': return $a + $b;
        etc...
    }
}

safest method is a switch construct:

function my_operator($a, $b, $char) {
    switch($char) {
        case '=': return $a = $b;
        case '*': return $a * $b;
        case '+': return $a + $b;
        etc...
    }
}
自此以后,行同陌路 2024-11-10 12:45:48

最简单但最危险的方法是使用eval

$c = eval("return $a $char $b;");

The easiest but most dangerous method is to use eval.

$c = eval("return $a $char $b;");
梓梦 2024-11-10 12:45:48

避免创建 switch/case 的另一种方法可能是:

<?php 
// Doesn't matter if $a and $b are integers or strings
$a = 100;
$b = 1000;
$operator = ">";

$operations = [
  '='  => $a == $b,
  '>'  => $a > $b,
  '>=' => $a >= $b,
  '<'  => $a < $b,
  '<=' => $a <= $b,
  '<>' => $a <> $b,
];

// It will get the value of $a > $b and the result will be false
$operations[$operator]
?>

最后,如果您愿意,可以将 $operations 数组移动到函数以提高代码的可读性。

<?php 
// Doesn't matter if $a and $b are integers or strings
$a = 100;
$b = 1000;
$operator = ">";

// It will get the value of $a > $b and the result will be false
$result = getOperationResult($a, $b, $operator);

function getOperationResult(string|int $a, string|int $b, string $operator): bool
{
    $operations = [
      '='  => $a == $b,
      '>'  => $a > $b,
      '>=' => $a >= $b,
      '<'  => $a < $b,
      '<=' => $a <= $b,
      '<>' => $a <> $b,
    ];

    return $operations[$operator];
}
?>

Another way to avoid creating a switch/case, could be:

<?php 
// Doesn't matter if $a and $b are integers or strings
$a = 100;
$b = 1000;
$operator = ">";

$operations = [
  '='  => $a == $b,
  '>'  => $a > $b,
  '>=' => $a >= $b,
  '<'  => $a < $b,
  '<=' => $a <= $b,
  '<>' => $a <> $b,
];

// It will get the value of $a > $b and the result will be false
$operations[$operator]
?>

Finally if you prefer, you can move the $operations array, to a function to improve the readability code.

<?php 
// Doesn't matter if $a and $b are integers or strings
$a = 100;
$b = 1000;
$operator = ">";

// It will get the value of $a > $b and the result will be false
$result = getOperationResult($a, $b, $operator);

function getOperationResult(string|int $a, string|int $b, string $operator): bool
{
    $operations = [
      '='  => $a == $b,
      '>'  => $a > $b,
      '>=' => $a >= $b,
      '<'  => $a < $b,
      '<=' => $a <= $b,
      '<>' => $a <> $b,
    ];

    return $operations[$operator];
}
?>
美人如玉 2024-11-10 12:45:48

看一下 eval() 函数。您将需要构建一个正确的 php 命令并在 eval() 内运行以提取结果。

take a look at the eval() function. you will need to build a proper php command and run inside the eval() to extract out the result.

小红帽 2024-11-10 12:45:48

您可以使用eval,但我不建议使用eval

如果存在 case 运算符可以执行任何操作,您应该在使用之前检查运算符是什么

switch($char)
{
  case '*':
    $result= $a * $b;
    break;

  case '+':
    $result= $a + $b;
    break;
}

You can do with eval however I would not suggest using eval.

If there is case operator can by anything you should check what operator is before using

switch($char)
{
  case '*':
    $result= $a * $b;
    break;

  case '+':
    $result= $a + $b;
    break;
}
树深时见影 2024-11-10 12:45:48
<?php
$a = 'alex';
$b = "alex";
$c = "==";
function abc($a,$b,$c){
  $d = 'return ($a '.$c.' $b) ? true : false;';
  return eval($d);
}
if(abc($a,$b,$c)){
  echo "condition true";
}else{
  echo "condition false";
}
// echo $e;
?>
<?php
$a = 'alex';
$b = "alex";
$c = "==";
function abc($a,$b,$c){
  $d = 'return ($a '.$c.' $b) ? true : false;';
  return eval($d);
}
if(abc($a,$b,$c)){
  echo "condition true";
}else{
  echo "condition false";
}
// echo $e;
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文