I can read a GPIO pin's status as such:

     exec ( "gpio read 7", $status );
     print_r ( $status );

this works fine. But I want to read the status from within a php if statement, eg:

if (isset($_GET['trigger']) && $_GET['trigger'] == 1) {
         exec ( "gpio read 7", $status );
         print_r ( $status );
         exec ( "gpio write 7 1" );

Though exec ('gpio write 7 1'); works, the read and print does not.

Would anyone know what I am doing wrong?

  • how do you know the write works and the read and print don't? Mar 14, 2015 at 5:13
  • @SteveRobillard the LED I have attached to that GPIO is triggered. The read probably works but is still not being printed to the the screen like it does normally.
    – MeltingDog
    Mar 14, 2015 at 5:17
  • you cannot read from an OUT pin, you should use different pin and configure pin you would like to read as input using gpio mode <pin> in
    – mpromonet
    Mar 14, 2015 at 9:36
  • @mpromonet I'm going to disagree with that -- you can check the value of an out pin.
    – goldilocks
    Mar 14, 2015 at 14:16
  • 1
    @goldilocks : mea maxima culpa. I did not checked, and you're right gpio read <pin> works on an out pin.
    – mpromonet
    Mar 14, 2015 at 14:42

2 Answers 2


exec adds the returned value to the end of the array $status. So first time, required value is in $status(0). Next time, old value is still in $status(0), but new value is in $status(1), etc for further loops. Fix is to use unset($status) each time round the loop so that exec always adds new value in $status(0).


Here's what I came up with when I had this same problem. This post helped me figure out what the issue was.

function closevalve ($valve) {
        $status[0] = 2;
        while ($status[0] != 1) {
            system ( "gpio write ".$valve." 1" );
            exec ( "gpio read ".$valve, $statusa );
            $status[0] = $statusa[0];
        echo date("h:i:s A")." - ".whichpin($valve)." Off<br>";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.