I was trying to execute gpio[bash shell] command from PHP. I was executing properly as expected, but when I cross check the same with bash shell pin 0 values got changed. 1. input url :gpio.php?value=4 [working and output displayed as expected toggle happens] 2. bash shell : #gpio read 4 [doesn't change the value, only pin 0 changes]

$num = intval($_GET['value']);
echo $num;
if (isset($num)){
        $read = shell_exec('gpio read ".$num."');
        if($read == 0) {
                echo "OFF";
                $re = shell_exec('gpio write ".$num." 1');}
        if($read == 1) {
                echo "ON";
                $re = shell_exec('gpio write ".$num." 0');}

        $read = shell_exec('gpio read ".$num."');
        if($read == 0) {echo " --> OFF";}
        if($read == 1) {echo " --> ON";}
else {echo "No value Found";}

Let me known were I'm wrong...?

1 Answer 1


My PHP is rusty (to say the least).

I would use

$cmd = "gpio write $num 1";
$re = shell_exec($cmd);

or possibly just

$re = shell_exec("gpio write $num 1");
  • Only pin 0 changes value
    – jOSe
    Commented Feb 10, 2016 at 10:00
  • There is no such thing as pin 0. I suggest you echo to the output the value you are trying to shell exec. That should show what you are doing wrong.
    – joan
    Commented Feb 10, 2016 at 10:23
  • Can you please list pins? I've used python to execute other gpio functions
    – jOSe
    Commented Feb 19, 2016 at 3:50
  • @jOSe The library you use will define the method used to number the GPIO. Check its documentation. Most will let you specify the GPIO by their Broadcom number. See my diagram at the end of abyz.co.uk/rpi/pigpio/index.html for the association of Broadcom GPIO number to pin number on the expansion header.
    – joan
    Commented Feb 19, 2016 at 8:35

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.