-3

How do I write this if statement correctly so that the "||" works? || represents "or" by the way. When i take out "|| >30" I receive no syntax error. But when I do include "|| >30" I receive a syntax error saying that ">" is unexpected.

if (strlen($_POST['password']) < 7 || >30){
            $errors[] = 'Your password must be 7 to 30 characters long.'
Kacy Raye
  • 9
  • 2
  • 5

2 Answers2

0
if (strlen($_POST['password']) < 7 || >30){

should be this I presume:

if (strlen($_POST['password']) < 7 || strlen($_POST['password']) > 30){

Since you asked for a shorter version of this this is the simplest way using the same logic and functions:

$length = strlen($_POST['password']);
if ($length < 7 || $length > 30){
kittycat
  • 14,983
  • 9
  • 55
  • 80
  • I think OP know that and he want to shorten the statement. – Eli Jan 06 '13 at 05:25
  • 1
    Awesome that worked, it says I have to wait 11 minutes before I can accept the answer so I'll make sure to do so. Thank you! – Kacy Raye Jan 06 '13 at 05:26
  • I'm a beginner so I actually didn't know that, but if there is a way to shorten the statement feel free to post it. I don't mind learning shortcuts :) – Kacy Raye Jan 06 '13 at 05:30
  • 1
    @KacyRaye, You welcome. Btw, you should never limit the length of a password as that is a bad security practice. See: http://stackoverflow.com/q/98768/1592648 – kittycat Jan 06 '13 at 05:30
  • 1
    Looks like I'll be removing the max length then lol... – Kacy Raye Jan 06 '13 at 05:43
0

I think the only way to make it easier is to store your password length in a variable like this:

 $pwd_len = strlen($_POST['password'])

 if ($pwd_len < 7 || $pwd_len > 30){

This maybe not useful in your case but will be helpful when you need to refer to your password length many times. So it will save you a lot of time instead of always write strlen($_POST['password'])

Maybe I'm wrong but hope it helps :)

Eli
  • 14,779
  • 5
  • 59
  • 77
  • No you're totally right, thanks I'll keep variables in mind when writing code from now on. I just started like a week ago so I need all the tips I can get :P – Kacy Raye Jan 06 '13 at 06:07