我在raspberry pi中用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“中使用它。为此,我尝试将这个字符串更改为Int,这要归功于intval()函数(就像您在程序顶部看到的那样),但是它没有工作。我还尝试使用ord()和(int)函数。结果总是一样的。它显示的是"oui“。
我的问题是来自intval()函数还是来自shell-exec()?
谢谢你的帮助;)我尽力在我的解释中尽可能清楚
发布于 2018-08-11 11:55:30
这一行有一个错误,因为=不是比较运算符:
if ($status = 1) {它应该是:
if ($status == 1) {如果还想检查1和$status的类型是否相同,请使用操作符===。这是用于比较操作符的PHP文档。
发布于 2018-08-11 11:55:31
您将需要使用比较运算符。我修改了您的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");
}
?>https://stackoverflow.com/questions/51799385
复制相似问题