1

I currently have this code:

if (strlen(trim($username) < 4)) {
$error='Username should be between 4 and 10 characters.';
}

if (strlen(trim($username) > 10)) {
$error='Username should be between 4 and 10 characters.';
}

I wish to reduce that into a simpler statement, like this (but that obviously doesn't work):

if (strlen(trim($username) < 4 >10))... // parse error
Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
Alegro
  • 7,534
  • 17
  • 53
  • 74

4 Answers4

3

This syntax is incorrect, you should use || operator:

if (strlen(trim($username)) < 4 || strlen(trim($username)) > 10) {
    $error='Username should be between 4 and 10 characters.';
}
vvolkov
  • 1,172
  • 8
  • 10
3

You're essentially just checking if a number is within a specified range, so another option would be filter_var(), although a little scary:

if(!filter_var(strlen(trim($username)), FILTER_VALIDATE_INT, array('options' => array('min_range' => 4, 'max_range' => 10))))
{
    $error='Username should be between 4 and 10 characters.';
}
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • Thanks, MrCode. useful code, but probably for some another situation. – Alegro Jan 09 '13 at 18:55
  • Ugh, 3 function calls and two arrays created? I'm all for the idea of using the tools that a language provides, but don't we have a responsibility to balance that with efficiency? Just because a tool's there doesn't mean it's the right tool for the task. – Madbreaks Jan 09 '13 at 18:55
  • 3
    @Madbreaks I haven't bench tested this but I doubt this would have any performance impact compared to the other way. In terms of performance you should improve what is actually slow, not what is already very fast. More readable code is better than hard to read but micro-optimised code. In reality I would probably go with the simple if statement over this (hence why I upvoted them) but here on SO it's always good to see what other options there are. – MrCode Jan 09 '13 at 19:15
  • You feel this code is "more readable"? Than what? Op explcitly asked for condensed code. The scrollbar on your snippet should be an obvious indicator that you're not meeting the question's guidelines. Regardless, you make some valid points. Thanks for the discourse. – Madbreaks Jan 09 '13 at 20:14
2

Here you go, use of the || (or) operator will help.

Also take note how I assigned the username to variables to prevent the use of your trim() and strlen() functions being called multiple times. That's just wasteful.

Code

$username = trim('bob');
$username_length = strlen($username);
if ($username_length < 4 || $username_length > 10)
{
    echo 'Username should be between 4 and 10 characters.';
}
phpisuber01
  • 7,585
  • 3
  • 22
  • 26
  • This kind of code bloat is completely unnecessary and totally avoidable. You may be going for readability, but there's a tradeoff and code like this is often more difficult to maintain than code that's succinct and well commented. – Madbreaks Jan 09 '13 at 18:53
  • 3
    @Madbreaks I see your point and it has some validity, but do remember an Example like this is meant be verbose in nature so the OP can learn from it without decoding cryptic coding standards. In procedural code like this, there is really no way to be much less verbose without (like I said) making multiple needless calls to a function. – phpisuber01 Jan 09 '13 at 18:57
  • Op *explicitly* asked for *condensed* syntax – Madbreaks Jan 09 '13 at 20:11
  • He did condense the overuse of the `strlen` function. Why any of these posts are getting down-votes is ridiculous. Just because one is more condensed than the other doesn't actually make it more right, and in some cases (not this one) will make it less readable and maintainable. – Josh Jan 09 '13 at 23:01
  • 1
    @Josh Ahhh, so I'm not the only one who notices the trolls on StackOverflow these days.. Some people get way to nit-picky. – phpisuber01 Jan 10 '13 at 14:08
-1

Well you could do:

(strlen(trim($username)) < 4 || strlen(trim($username)) > 10) && $error='Username should be between 4 and 10 characters.';

But it'd be a lot more efficient to define the trimmed username length first:

$len = strlen(trim($usename));
($len < 4 || $len > 10) && $error = "Bad username";
Madbreaks
  • 19,094
  • 7
  • 58
  • 72
  • Why `&&` ? Is it `And` ? – Alegro Jan 09 '13 at 18:48
  • They're essentially the same, with slightly different precedences. See here: http://stackoverflow.com/questions/2803321/and-vs-as-operator – Madbreaks Jan 09 '13 at 18:49
  • Great! If you feel I answered your question, please consider accepting it. – Madbreaks Jan 09 '13 at 18:56
  • 4
    I personally wouldn't go with the short circuit `&&` option, it's less readable, less maintainable and not very intuitive, particularly for junior developers and it offers no benefits other than saving the `{ }` block. If you look at some open source coding standards, this type of coding is listed undesirable and may be rejected. – MrCode Jan 09 '13 at 19:26
  • 1
    @MrCode Suggest staying away from Perl. ;) And *in your opinion* it's less readable, and less maintainable and not very intuitive. I completely disagree. And to quote **you**: *"here on SO it's always good to see what other options there are."* – Madbreaks Jan 09 '13 at 20:04