<?php
header("content-type:text/html;charset=utf-8");
define('L1',1);
define('L2',2);
define('L3',4);
define('L4',8);
define('L5',16);
$state = 10;
function showLights()
{
for($i=1;$i<=5;++$i)
{
$light = 'L'.$i;
if($GLOBALS['state'] & constant($light) )
{
echo 'L'.$i.'亮 ';
}
else
{
echo 'L'.$i.'灭 ';
}
}
}
echo "开始灯泡状态:<br>";
showLights();
echo "<br>";
$state = $state & ~L2;
echo "关闭灯泡L2:<br>";
showLights();
echo "<br>";
$state = $state | L1;
echo "打开灯泡L1:<br>";
showLights();
echo "<br>";
function turnOnAll()
{
for($i=1;$i<=5;++$i)
{
$light = 'L'.$i;
$GLOBALS['state'] = $GLOBALS['state'] | constant($light);
}
}
function turnOffAll()
{
for($i=1;$i<=5;++$i)
{
$light = 'L'.$i;
$GLOBALS['state'] = $GLOBALS['state'] & ~constant($light);
}
}
echo "打开所有灯:<br>";
turnOnAll();
showLights();
echo "<br>";
echo "关闭所有灯:<br>";
turnOffAll();
showLights();
echo "<br>";
$state = $state | L3;
echo "打开灯泡L3:<br>";
showLights();
echo "<br>";