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 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler