分配给布尔值的变量不会回显到 PHP 中的网页
variable assigned to a boolean won't echo to the webpage in PHP
我有一些 PHP 代码应该检查是否分配了 $_GET 全局中的某个值。如果不是,它会自动输入一个预定值。如果有,它会为变量分配一个默认值:
<?php
if(!isset($spaceObjects)){
$spaceObjects = 0;
} else{
$spaceObjects = $_GET['spaceObjects'] == true;
}
if(!isset($numOfStars)){
$numOfStars = 100;
} else{
$numOfStars = intal($_GET['numOfStars']);
}
if(!isset($starTwinkles)){
$starTwinkles = 0;
} else{
$starTwinkles = ($_GET ['starTwinkles'] == true);
}
if(!isset($numOfMeteorsMax)){
$numOfMeteorsMax = 8;
} else{
$numOfMeteorsMax = intval($_GET['numOfMeteorsMax']);
}
if(!isset($twinklePausing)){
$twinklePausing = 1;
} else{
$twinklePausing = ($_GET['twinklePausing'] == true);
}
if(!isset($nightCycle)){
$nightCycle = 0;
} else{
$nightCycle = ($_GET['nightCycle'] == true);
}
if(!isset($music)){
$music = 1;
} else{
$music = ($_GET['musicEnabled'] == true);
}
if(!isset($nightSounds)){
$nightSounds = 0;
} else{
$nightSounds = ($_GET['nightSounds'] == true);
}
if(!isset($nightSoundsVol)){
$nightSoundsVol = 0.1;
} else{
$nightSoundsVol = intval($GET['nightSoundsVol']);
}
?>
let controlVariables = {
spaceObjects: <?php echo $spaceObjects;?>,
numOfStars: <?php echo $numOfStars;?>,
starTwinkles: <?php echo $starTwinkles;?>,
numOfMeteorsMax: <?php echo $numOfMeteorsMax;?>,
disableTwinklingWhileSpaceObjectGenerated: <?php echo $twinklePausing;?>,
nightCycle: <?php echo $nightCycle;?>,
music: <?php echo $music;?>,
nightSounds: <?php echo $nightSounds;?>,
nightSoundsVol: <?php echo $nightSoundsVol;?>
};
奇怪的是 PHP 在某些 echo
语句中没有给我任何信息,导致 JS 抛出语法错误。
为什么不打印 true
或 false
PHP 在某些字段上什么也没有?
尝试将 echo 替换为条件以输出特定文本而不是变量本身。
Echoed boolean 为真时输出 1,当变量为假时不输出任何内容。
例如:
let controlVariables = {
spaceObjects: <?php echo ($spaceObjects)? 'true' : 'false';?>,
...
}
我有一些 PHP 代码应该检查是否分配了 $_GET 全局中的某个值。如果不是,它会自动输入一个预定值。如果有,它会为变量分配一个默认值:
<?php
if(!isset($spaceObjects)){
$spaceObjects = 0;
} else{
$spaceObjects = $_GET['spaceObjects'] == true;
}
if(!isset($numOfStars)){
$numOfStars = 100;
} else{
$numOfStars = intal($_GET['numOfStars']);
}
if(!isset($starTwinkles)){
$starTwinkles = 0;
} else{
$starTwinkles = ($_GET ['starTwinkles'] == true);
}
if(!isset($numOfMeteorsMax)){
$numOfMeteorsMax = 8;
} else{
$numOfMeteorsMax = intval($_GET['numOfMeteorsMax']);
}
if(!isset($twinklePausing)){
$twinklePausing = 1;
} else{
$twinklePausing = ($_GET['twinklePausing'] == true);
}
if(!isset($nightCycle)){
$nightCycle = 0;
} else{
$nightCycle = ($_GET['nightCycle'] == true);
}
if(!isset($music)){
$music = 1;
} else{
$music = ($_GET['musicEnabled'] == true);
}
if(!isset($nightSounds)){
$nightSounds = 0;
} else{
$nightSounds = ($_GET['nightSounds'] == true);
}
if(!isset($nightSoundsVol)){
$nightSoundsVol = 0.1;
} else{
$nightSoundsVol = intval($GET['nightSoundsVol']);
}
?>
let controlVariables = {
spaceObjects: <?php echo $spaceObjects;?>,
numOfStars: <?php echo $numOfStars;?>,
starTwinkles: <?php echo $starTwinkles;?>,
numOfMeteorsMax: <?php echo $numOfMeteorsMax;?>,
disableTwinklingWhileSpaceObjectGenerated: <?php echo $twinklePausing;?>,
nightCycle: <?php echo $nightCycle;?>,
music: <?php echo $music;?>,
nightSounds: <?php echo $nightSounds;?>,
nightSoundsVol: <?php echo $nightSoundsVol;?>
};
奇怪的是 PHP 在某些 echo
语句中没有给我任何信息,导致 JS 抛出语法错误。
为什么不打印 true
或 false
PHP 在某些字段上什么也没有?
尝试将 echo 替换为条件以输出特定文本而不是变量本身。 Echoed boolean 为真时输出 1,当变量为假时不输出任何内容。 例如:
let controlVariables = {
spaceObjects: <?php echo ($spaceObjects)? 'true' : 'false';?>,
...
}