homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

What am I doing Wrong with this regex
Regular Expression Trouble

 8:24 pm on Nov 1, 2006 (gmt 0)

I'm just trying to check and see if this name is valid. I submit things like "12345" and it evaluates to true...

function checkName ($name)
$status = FALSE;
$pattern = "/^[A-Za-z]+$/";
if (preg_match($pattern, $name));
$status = TRUE;

return $status;


What am I doing wrong?



 8:40 pm on Nov 1, 2006 (gmt 0)

Hi! You have ; in the last char of the IF condition, remove it.

if (preg_match($pattern, $name))
$status = true;


 8:44 pm on Nov 1, 2006 (gmt 0)

I would have never gotten that.....note to self: use debugger next time.
Thanks a million.


 4:57 pm on Nov 2, 2006 (gmt 0)

Another good reason to always use curly braces ;)
if (preg_match($pattern, $name)) { 
$status = TRUE;
Syntax parser would have caught that right away.

You are strongly encouraged to always use curly braces even in situations where they are technically optional. Having them increases readability and decreases the likelihood of logic errors being introduced when new lines are added.


Sound advice!

Clinton Labombard

 7:32 am on Nov 4, 2006 (gmt 0)

There's no reason to use a breakline, though. The entire reason to allow a conditional without curly braces is so you can keep it on one line:

if (preg_match($pattern, $name)) $status = TRUE;

That's actually more legible than with curly braces.


 11:10 am on Nov 4, 2006 (gmt 0)

I think it really depends on what you are used to, and the method that you know best, or that fits your coding style better. I prefer to use curly brackets, because that is how I learned, and the method I like best.

If you are a beginner, though, you could technically use either method, but I believe the curly braces is the better, easier, easier to read method. IMHO.


 8:26 pm on Nov 4, 2006 (gmt 0)

Seeing as nobody else has mentioned it, you could shorten that function quite a bit:

function checkName ($name)
return preg_match('/^[A-Za-z]+$/', $name);

Or, if you want to ensure checkName returns a real boolean, rather than the 0 or 1 preg_match returns:

  • return (bool)preg_match('/^[A-Za-z]+$/', $name);
    using casting [us3.php.net]
  • return !!preg_match('/^[A-Za-z]+$/', $name);
    the double-not trick, which is just like casting to bool
  • return preg_match('/^[A-Za-z]+$/', $name) ? true : false;
    the ternary operator [us3.php.net]
  • return preg_match('/^[A-Za-z]+$/', $name) == 1;
    making use of preg_match's returning 1 or 0

But that's just style, and your preferences undoubtedly vary from my own. :)

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved