Skip to main content
December 2, 2006
Question

php + should I use is_null or !empty

  • December 2, 2006
  • 3 replies
  • 293 views
Hi I read in another topic in this forum that if they were wanting to show a link if available that they used:
<?php
if ( is_null($row_rsMyRecordset['event_link']) ) {
echo $row_rsMyRecordset['event_text'];
} else {
echo "<a
href=".$row_rsMyRecordset['event_link'].">".$row_rsMyRecordset['event_text']."</a>";
}
?>


I have used this and it works on my local server, but I read the php manual and wanting to follow correct methods of use:
if (!empty($row_blah['email'])) echo 'Email Us';

I hadn't heard of using is_null before so this is why I am wondering the best method.
Thanks for your time.
This topic has been closed for replies.

3 replies

Inspiring
December 3, 2006
jjjhbj111 wrote:
> If I do a regex check on the form the user submits, this should prevent a
> '0.0' being placed?

Yes.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/
December 3, 2006
Thanks for the quick reply David,
I am using this for the company email address the user inputs.
If there is an email address the 'Email Us' appears with a link to another page to allow viewers to contact them using a contact form passing the id rather than the email address.
I tried to place a '0.0' in the email field and it did show up as 'Email Us'
If I do a regex check on the form the user submits, this should prevent a '0.0' being placed?
I know there are no full guarantees with these checks, but elimination is a help.
Thank you so much for giving me a full description on these two tests, you explain it so well and I appreciate it.
Inspiring
December 2, 2006
jjjhbj111 wrote:
> I hadn't heard of using is_null before so this is why I am wondering the best
> method.

is_null() tests whether the value of a variable is NULL. It also returns
true if the variable doesn't exist, but generates a warning notice (if
error_reporting is set to the highest level) that there is no such variable.

empty() tests whether a variable fits any of the following criteria:

0 or '0'
0.0 (but not '0.0')
NULL
FALSE
'' (an empty string)
an empty array
a variable declared, but without a value

In other words, is_null() tests for only one thing: whether the variable
is NULL. empty() has a broader definition.

Which you should use depends on what you actually need to test for. You
need to be careful when using empty() with variables that might contain
a number because 0 is considered to be empty.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/