Search code examples
phpstringraspberry-piintshell-exec

String to int in PHP


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


Solution

  • 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.