我怎样才能使这段代码更短?例如使用 foreach。
if($Email == NULL){
$Email = "-";
}
elseif($Age == NULL){
$Age = "-";
}
elseif($Sex == NULL){
$Sex = "-";
}
它必须像这样替换
$search = array("%UserID%", "%RegDate%", "%Name%", "%Email%", "%Age%", "%Gender%");
$replace = array($UserID, $RegDate, $Name, $Email, $Age, $Sex);
$content = str_replace($search, $replace, $content);
编辑:
我现在已经这样了,是否也可以在三元代码中使用 $variable = $row ?顺便说一句,我有一个 variables.php 文件,我在其中使用三元代码进行定义,我已经在那里尝试过,但是因为它之前被使用过,所以它没有用,我没有想到:P
但是这个当前的代码有效,我只是想知道它是否可以更短。
while($row = mssql_fetch_assoc($accountinforesult)){
$UserID = $row['UserID'];
$RegDate = $row['RegDate'];
$Name = $row['Name'];
$Email = $row['Email'];
$Age = $row['Age'];
$Sex = $row['Sex'];
$UserID = isset($UserID) ? $UserID : "-";
$RegDate = isset($RegDate) ? $RegDate : "-";
$Name = isset($Name) ? $Name : "-";
$Email = isset($Email) ? $Email : "-";
$Age = isset($Age) ? $Age : "-";
$Sex = isset($Sex) ? $Sex : "-";
}