这可能不是正确的术语,"全局"。
我想弄清楚的是这个。
我有这样的东西:
<?php if (empty($nest)) {
$mothercrying = 'all day long';
echo 'Mother is crying '.$mothercrying;
} if (!empty($nest)) {
echo 'Mother is NOT crying '.$mothercrying;
} ?>
有没有办法在第一个if
中声明$mothercrying
,以便我也可以在第二个中使用它?
请注意,我不能在两个if
语句之前声明$mothercrying
,因为我正在使用的内容实际上比这长几百行。
您可以通过两种方式定义常量,如果您在单独的文件中定义常量,则必须包含要使用的文件:
define('MOTHER_CRYING', 'all day long');
或
global $mothercrying;
$mothercrying = 'all day long';
使用会话
session_start();
if (empty($nest)) {
$mothercrying = 'all day long';
$_SESSION['cry']= $mothercrying;
echo 'Mother is crying '.$_SESSION['cry'];
} if (!empty($nest)) {
if(isset($mothercrying)){
$_SESSION['cry']= $mothercrying;
}else{
$_SESSION['cry']='all day long';
}
echo 'Mother is NOT crying '.$_SESSION['cry'];
}
这不是一个"全球性"问题。两种封信都是相互排斥的。第二个if
应该只是一个else
变量必须在if
语句之前定义。如果第二个限制为真,则第一个只能是假的,并且不会定义您的变量!