How can I make this code shorter? For example with a foreach.
if($Email == NULL){
$Email = "-";
}
elseif($Age == NULL){
$Age = "-";
}
elseif($Sex == NULL){
$Sex = "-";
}
It has to be replaced with like this
$search = array("%UserID%", "%RegDate%", "%Name%", "%Email%", "%Age%", "%Gender%");
$replace = array($UserID, $RegDate, $Name, $Email, $Age, $Sex);
$content = str_replace($search, $replace, $content);
Edit:
I’ve got it like this now is it possible to use the $variable = $row in the ternary code too? Btw I have a variables.php file where I use ternary code to define and I already tried it there but because it was being used earlier it didnt work and I didnt think of it 😛
But this current code works I just wonder if it can be shorter.
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 : "-";
}
Not tested but I believe this should work.
$$v means “the variable with name $v”. If $v = ‘foo’ then $$v is $foo.
Look at “variable variables”: http://php.net/manual/en/language.variables.variable.php