Jump to content

What's wrong with this statement?


LukeV

Recommended Posts

Alright so I wanted to do a check on a username input. I wanted to make sure that the username inputted is greater then 4 and less then 15 characters.So, let $username be the variable which holds the username.

if (strlen($username) < 4 || >15) {$error .= "Username must be 4-15 characters."}

But when I do this I get:

Parse error: syntax error, unexpected '>'
So what's wrong with the above statement? Looks fine to me, but then again I'm no php expert ^^Thanks.
Link to comment
Share on other sites

$len = strlen($username); // so we only call strlen onceif ( ($len < 4) || ($len >15)) {$error .= "Username must be 4-15 characters."}#notice the nested parens

Gotta do each comparison the long way.Was it old-fashioned BASIC that let you do it the way LukeV tried it? Something did or does, I'm pretty sure.

Link to comment
Share on other sites

"or" is not an operation you can perform interchangably the way you think. You need to supply two complete expressions on it's sides. And ">15" is not a valid expression without an argument on the left. You need something like:

if (($len = strlen($username)) < 4 || $len >15) {$error .= "Username must be 4-15 characters."}

[edit]Damn... Deirdre's Dad beated me to it... what's interesting is that we both called the variable $len :) . [/edit]

Link to comment
Share on other sites

Ah ok I see what you mean. Thanks guys, will keep this in mind :)EDIT: I'm used to coding in Java. I think that syntax is acceptable in Java no? I could be wrong. I'm not an expert in Java either :) Anyways it just sounded right to me at first, but now I know better.

Link to comment
Share on other sites

what's interesting is that we both called the variable $len :)
We've read the same manuals. Sometimes I even name a string $str. And I usually iterate with $i. (I mean, don't go thinking we're soulmates, or anything.)Ooh. What does this tell you? When I write a script that needs to break into functions, the first one I call is main();
Link to comment
Share on other sites

(I mean, don't go thinking we're soulmates, or anything.)
Haven't thought about it even for a sec. Don't you worry :) . That just caught my sight, that's all. Besides, other than using $i for iterations, I don't do the rest of the things you said :) .
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...