...

View Full Version : php if variable is empty default to other text



crmpicco
02-24-2008, 03:16 PM
I am wondering if it is possible to default to a piece of text if a varable is empty in PHP?

Something like this, maybe????


<?php echo stripslashes($row["moreinfo"]) || "None."; ?>

Picco

Blaher
02-24-2008, 03:50 PM
<?php
if (!isset($row["moreinfo"]) || $row["moreinfo"]==NULL)
{
$row["moreinfo"]="None.";
}
echo stripslashes($row["moreinfo"]);
?>

If you know the variable will always be set no matter what, you don't need the !isset($row["moreinfo"]).

crmpicco
02-24-2008, 04:08 PM
Cheers mate: http://www.ayrshireminis.com/mini/en/gallerymini2.php?id=114

:thumbsup:

JohnDubya
02-25-2008, 01:07 AM
<?php
if (!isset($row["moreinfo"]) || $row["moreinfo"]==NULL)
{
$row["moreinfo"]="None.";
}
echo stripslashes($row["moreinfo"]);
?>

If you know the variable will always be set no matter what, you don't need the !isset($row["moreinfo"]).

Or even better:



if (empty($row['moreinfo'])) {
$row['moreinfo'] = 'None.';
}
echo stripslashes($row['moreinfo']);

crmpicco
02-25-2008, 08:55 PM
I was thinking more using a ternary operator to do this.....similarly to what is done in Perl....

StupidRalph
02-25-2008, 09:26 PM
I was thinking more using a ternary operator to do this.....similarly to what is done in Perl....



$row['moreinfo'] = (empty($row['moreinfo'])) ? 'None.' : $row['moreinfo'];
echo stripslashes($row['moreinfo'];




Sidenote: Hey JohnDubya....congrats on becoming a mod. :thumbsup:

JohnDubya
02-25-2008, 09:28 PM
Sidenote: Hey JohnDubya....congrats on becoming a mod. :thumbsup:

Thanks! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum