douyi8760 2018-08-11 11:52
浏览 79
已采纳

PHP中的字符串到[重复]

This question already has an answer here:

I wrote a program in python in my raspberry pi which use the gpio 17. My goal is to read the status of this gpio separably to this program, to run a "if" and to display the result in a local website. For this, I use apache2 and PHP (version 7), I'm beginner in this language. This is the program I use :

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

This program don't work because if I understand, the value of $read I obtain is a string and I need an Int to use it in my "If". For it, I tried to change this String to an Int thanks to the function intval() (like you can see in the program in the top) but it didn't work. I tried to use also the ord() and the (int) function. The result is always the same. It display "oui".

Is my problem come from the intval() function or is it maybe come from shell-exec() ?

Thanks for your help ;) I tried to be the clearest possible in my explanation

</div>
  • 写回答

2条回答 默认 最新

  • dopgv00024 2018-08-11 11:55
    关注

    You have an error at this line, since = is not a comparison operator:

     if ($status = 1) {
    

    It should be:

     if ($status == 1) {
    

    If you also want to check that 1 and $status are of the same type, use the operator === instead. Here is the PHP documentation for comparison operator.

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

报告相同问题?

悬赏问题

  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应