phpeveryday.com

The best tutorial of php, php framework, php strategies, object oriented oriented,


String: Checking for Empty Values

Tag: basic, string   Category: PHP Basic
post: 01 Feb 2008 read: 406


PHP String Tips: PHP have built in function name empty()for checking empty value. But, it is not good idea. Why? Because it will not work for variable have "0" value. PHP will treat "0" as false. So, it will return empty. But, we want "0" is exist, not empty. Solution?

We can use combination isset() and trim() function for checking. The code like this:

<?
$str = " ";

if(!isset($str) || trim($str) == ""){
  echo "empty";
}else{
  echo "not empty";
}

?>

The simple logic: use isset() to verify that exists, then use trim() function to trim whitespace from the edges.




| Give Your Opinion | Recommend
Share and Bookmark to: These icons link to social bookmarking sites where readers can share and discover new web pages.
digg del.icio.us technorati Ma.gnolia BlinkList

Recommended articles by other readers:
Web Services: How PHP Kiss VB.NET? (Part 1)
Chart: How to Build Cool Animation Real Time Chart
Joomla: Fast Road to Understand Component Programming
Email: Send Attachement Mail
mod_rewrite - Part 1: create your "fantasy" URL

What do You Think?
Your Name *:
Email *:
(Will not be published)
Website/URL:
Your Comment *:
* Required


615
posting