dongying7847 2019-07-05 12:56
浏览 74
已采纳

如果变量中的逻辑(谓词表达式)

I'm passing the comparison operator as a variable to the PHP Cli:

./test.php -k "command" -c ">0"

The command in -k products a result and I've stored it in $result

The problem I'm getting is I want to pass the logic and comparison operator as variables, is this possible?

$result = 2;
$logic = ' >0';

if ( $result $logic ) echo "true"; 

But I get:

PHP Parse error: syntax error, unexpected '$logic' (T_VARIABLE)

Any ideas?

  • 写回答

3条回答 默认 最新

  • duanban4769 2019-07-05 13:03
    关注

    It's not possible to do it that way, but you can do it using the eval method, like this:

    $result = 2;
    $logic = ' >0';
    
    
    eval('$logicResult = ' . $result . $logic .';');
    if ( $logicResult ) echo "true"; 
    

    The eval method is not recommended, as it might introduce security flaws in your app.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 问一下,定向解包是什么意思
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。
  • ¥15 各位 帮我看看如何写代码,打出来的图形要和如下图呈现的一样,急
  • ¥30 c#打开word开启修订并实时显示批注
  • ¥15 如何解决ldsc的这条报错/index error
  • ¥15 VS2022+WDK驱动开发环境
  • ¥30 关于#java#的问题,请各位专家解答!