PHP 中的字符串到 int
String to int in PHP
我在 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" 中使用它。为此,我尝试通过函数 intval() 将此 String 更改为 Int(就像您在顶部的程序中看到的那样),但它没有用。我也尝试使用 ord() 和 (int) 函数。结果总是一样的。它显示 "oui".
我的问题是来自 intval() 函数还是来自 shell-exec()?
感谢您的帮助 ;)
我试图在我的解释中尽可能清楚
这一行有错误,因为 =
不是比较运算符:
if ($status = 1) {
应该是:
if ($status == 1) {
如果您还想检查 1
和 $status
是否属于同一类型,请改用运算符 ===
。这是 PHP documentation for comparison operator.
您将需要使用比较运算符。我已经修改了你的 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");
}
?>
我在 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" 中使用它。为此,我尝试通过函数 intval() 将此 String 更改为 Int(就像您在顶部的程序中看到的那样),但它没有用。我也尝试使用 ord() 和 (int) 函数。结果总是一样的。它显示 "oui".
我的问题是来自 intval() 函数还是来自 shell-exec()?
感谢您的帮助 ;) 我试图在我的解释中尽可能清楚
这一行有错误,因为 =
不是比较运算符:
if ($status = 1) {
应该是:
if ($status == 1) {
如果您还想检查 1
和 $status
是否属于同一类型,请改用运算符 ===
。这是 PHP documentation for comparison operator.
您将需要使用比较运算符。我已经修改了你的 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");
}
?>