Dave's Brain

Browse - Programming Tips - Is there a robust way to check a string for being empty?

Date: 2016nov15
Language: php

Q.  Is there a robust way to check a string for being empty?

A.  These functions handle undefined, null, zero length and ignore leading and trailing space

function isSpace($a) { if ($a == null) return true; if ($a == '') return true; return preg_match('/^\s*$/', $a); } function hasContent($a) { return ! isSpace($a); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.