调用php函数()在每个图标前显示名称

调用php函数()在每个图标前显示名称,php,raspberry-pi,gpio,Php,Raspberry Pi,Gpio,我想在图像on.png或off.png之前显示每个继电器的名称。 我有两页: switch.php and functions.php php,显示每个继电器的打开或关闭图标,读取gpio状态。functions.php显示每个中继的名称 我不知道如何在switch.php中调用像r$I()这样的函数来显示以下内容: relay0<img id='button_0' src='images/off.png' alt='off'/><br> relay1<img i

我想在图像on.png或off.png之前显示每个继电器的名称。 我有两页:

switch.php and functions.php
php,显示每个继电器的打开或关闭图标,读取gpio状态。functions.php显示每个中继的名称

我不知道如何在switch.php中调用像r$I()这样的函数来显示以下内容:

relay0<img id='button_0' src='images/off.png' alt='off'/><br>
relay1<img id='button_1' src='images/off.png' alt='off'/>
relay0
继电器1
以下是我的脚本:

// switch.php
<?php
 $status = array(0, 0, 0, 0, 0, 0, 0);

 for ($i = 0; $i < count($status); $i++) {
    //set the pin's mode to output and read them
    system("gpio mode ".$i." out");
    exec ("gpio read ".$i, $status[$i], $return );
    if ($status[$i][0] == 0 ) {
        echo ("<img id='relay_".$i."' src='images/off.png' alt='off'/><br>");
    }
    if ($status[$i][0] == 1 ) {
        echo ("<img id='relay_".$i."' src='images/on.png' alt='on'/><br>");
    }    
 }
//switch.php

如果继电器的ID与不需要function.php脚本的按钮的ID匹配,请尝试以下操作:

function r0() {
    echo 'relay0';
}

function r1() {
    echo 'relay1';
}


$i = 1;
$fn = "r$i"; // string containing function name
$fn(); // this will call function 'r1'

call_user_func($fn); // this will also call 'r1'
switch.php

<?php
 $status = array(0, 0, 0, 0, 0, 0, 0);

 for ($i = 0; $i < count($status); $i++) {
    //set the pin's mode to output and read them
    system("gpio mode ".$i." out");
    exec ("gpio read ".$i, $status[$i], $return );
    if ($status[$i][0] == 0 ) {
    echo ("relay$i<img id='relay_".$i."' src='images/off.png' alt='off'/><br>");
    }
    if ($status[$i][0] == 1 ) {
    echo ("relay$i<img id='relay_".$i."' src='images/on.png' alt='on'/><br>");
    }    
 }

假设您的
function.php
文件将来会被更多功能所取代,您可以这样做:

function r0() {
    echo 'relay0';
}

function r1() {
    echo 'relay1';
}


$i = 1;
$fn = "r$i"; // string containing function name
$fn(); // this will call function 'r1'

call_user_func($fn); // this will also call 'r1'

希望这有帮助:)

您可以使用变量函数

$function = 'r'.$i; //set function name
$function(); //call the function

更多信息:

谢谢你,辛克罗尼斯!它可以帮助我,但每个功能将为每个继电器调用不同的图标(继电器0灯,继电器1加热器,…)谢谢Marios。我试试看。
$function = 'r'.$i; //set function name
$function(); //call the function