Python Forum
Question on a php syntax - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Question on a php syntax (/thread-15343.html)



Question on a php syntax - ebolisa - Jan-14-2019

Hi,

I need to iterate through the following loop but the exec() line is failing to respond. What's the correct way of doing it?

TIA

<?php
//exec(python3 -c 'import gpio; print(gpio.ledsOn(0))'); //works when used from command line

$arrleds = array();

for ($x = 0; $x <= 6; $x++) {
    $arrleds[$x] = exec(python3 -c 'import gpio; print(gpio.ledsOn($x))');
    echo "The led status of ".$x "is: ".$arrleds[$x];
    echo " <br>";
}
?>
EDIT:

Solved with the following changes:

for ($x = 0; $x <= 6; $x++) {

    $cmd = exec("sudo python3 test.py ".$x);
    $arrleds[$x] = $cmd;
    echo "The led status is: ".$arrleds[$x];
    echo " <br>";
}



RE: Question on a php syntax - nilamo - Jan-14-2019

Thanks for coming up with the solution. It looks like wrapping the command in quotes to make it a string, as well as possibly adding sudo, is what fixed it.