【问题标题】:String to int in PHP [duplicate]PHP中的字符串到int [重复]
【发布时间】:2018-08-11 11:52:06
【问题描述】:

我在我的树莓派中用 python 编写了一个程序,它使用了 gpio 17。我的目标是读取这个 gpio 的状态,与这个程序分开,运行一个“if”并在本地网站上显示结果。为此,我使用 apache2 和 PHP(版本 7),我是这种语言的初学者。这是我使用的程序:

<?php
 $read = shell-exec ('gpio read 0');
 $status = intval($read);
 if ($status = 1) {
    print ("oui"); 
 }    
 else {
    print ("non");
 } 
?>

这个程序不起作用,因为如果我理解的话,我获得的 $read 的值是一个字符串,我需要一个 Int 才能在我的“If”中使用它。为此,我尝试通过函数 intval() 将此 String 更改为 Int (就像您在顶部的程序中看到的那样),但它不起作用。我也尝试使用 ord() 和 (int) 函数。结果总是一样的。它显示“oui”。

我的问题是来自 intval() 函数还是来自 shell-exec() ?

感谢您的帮助;) 我尽量在我的解释中做到最清楚

【问题讨论】:

    标签: php string raspberry-pi int shell-exec


    【解决方案1】:

    这行有错误,因为= 不是比较运算符:

     if ($status = 1) {
    

    应该是:

     if ($status == 1) {
    

    如果您还想检查1$status 是否属于同一类型,请改用运算符===。这是PHP documentation for comparison operator.

    【讨论】:

      【解决方案2】:

      您将需要使用比较运算符。我已经修改了你的 if 条件来比较值是否相同。

      <?php
       $read = shell-exec ('gpio read 0');
       $status = intval($read);
       if ($status === 1) { //Use === to check if they are the same type and value
        print ("oui"); 
       }    
       else {
        print ("non");
       } 
      ?>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-03-06
        • 2011-08-09
        • 2011-10-23
        • 1970-01-01
        相关资源
        最近更新 更多