[RESOLVED] Display alt image if $_SESSION not set

liunx

Guest
If $_SESSION['image'] is not set, photo.jpg is displayed. That works OK.

If $_SESSION['image'] is set, $_SESSION['image'] is displayed. No problem here.

But if the form has been submitted and no value has been assign to $_SESSION['image'], no image is displayed.


if (!isset($_SESSION['image']) || isset($_SESSION['image']) == '')
echo "<img src='http://www.phpbuilder.com/board/archive/index.php/photo.jpg'>";
else
echo " <img src=http://www.phpbuilder.com/board/archive/index.php/" . $_SESSION['image']>";if (!isset($_SESSION['image']) || $_SESSION['image'] == '')
echo "<img src='http://www.phpbuilder.com/board/archive/index.php/photo.jpg'>";
else
echo " <img src=http://www.phpbuilder.com/board/archive/index.php/" . $_SESSION['image']>";

if your one you compared the result of the isset function with ''


However, I would tend to use PHP's empty function

if (!isset($_SESSION['image']) || empty($_SESSION['image']))
echo "<img src='http://www.phpbuilder.com/board/archive/index.php/photo.jpg'>";
else
echo " <img src=http://www.phpbuilder.com/board/archive/index.php/" . $_SESSION['image']>";I came up with your first solution shortly after I made my post:
if (!isset($_SESSION['image']) || $_SESSION['image'] == '')

But I think I will also like to use the empty function. It makes it look like I know what I am doing. :)

Thanks,

Sam
;If you use the empty() function, you don't need to use the isset() function as well. If it's not set then it's empty. Contrariwise, if it's not empty then it must be set.
if (!isset($_SESSION['image']) || empty($_SESSION['image'])) is therefore equivalent to if (empty($_SESSION['image']))I see what you are talking about. Thanks Weedpacket. I am making that change to my script now.

Sam
;
 
Back
Top