Hamed Khanezar
Hamed Khanezar

Reputation: 91

strlen not working correctly

I'm creating a simple registration form and my problem is that when I use strlen function it's not working true when I enter number as password it does not show anything and when I enter not number it shows the message even if it's more than 6 characters

 <?php
 if (isset($_POST['signup'])) {
     $fname = $_POST['fname'];
     $lname = $_POST['lname'];
     $phone = $_POST['phone-number'];
     $address = $_POST['address'];
     $password = $_POST['password'];
     $cpassword = $_POST['cpassword'];
 }
 if (isset($password)) {
     if (strlen($password < 6)) {
         $password_err = "password must be at least 6 characters";
     }
 }

 ?>
 <?php
 if (isset($password_err)){
 echo $password_err;
 }
 ?>

Upvotes: 1

Views: 4276

Answers (7)

Spinstaz
Spinstaz

Reputation: 331

I was using PHP 7.4 and the OP's method worked. However, after upgrading to PHP 8, it stopped working.

To make it even more confusing, in PHPStorm, it shows as 'string' after strlen(

So you think that because it shows the little expected data format precursor, that it's going to be ok.

Upvotes: -1

axiac
axiac

Reputation: 72206

The error is in the expression:

strlen($password < 6)

It should be:

strlen($password) < 6

What is the value of strlen($password < 6)?

No matter what is the value of $password, the value of the expression $password < 6 is either TRUE or FALSE (a boolean).

strlen() expects a string as argument. You pass it a boolean. It first converts the argument to string then counts its characters.

How is a boolean converted to a string?

It's easy to find out:

var_dump((string)TRUE)
# string(1) "1"
var_dump((string)FALSE)
# string(0) ""

true is converted to '1', false is converted to '' (the empty string).

Accordingly, strlen() returns either 1 or 0.

Upvotes: 1

HoogleyBoogley
HoogleyBoogley

Reputation: 340

Strlen is being used in the wrong way. You have it like this:

if (strlen($password < 6)) {

When you want to use it like this:

if (strlen($password) < 6)) {

Here is a brief explaination of why it would be like that:

$length = strlen($password); // Will output a number so if password is 'password', it will output '8'.

// So lets add that strlen to the if statement
if ($length < 6) { // This will compare that number that $length output, to 6.
    # Do something
}

Upvotes: 1

Kaloyan
Kaloyan

Reputation: 106

I believe there is a small mistake in the use of the strlen method. The position of one of the brackets is wrong. Change the if statement with the strlen to this:

if (strlen($password) < 6) {
     $password_err = "password must be at least 6 characters";
 }

There should be a closing bracket right after $password.

Upvotes: 1

umair
umair

Reputation: 161

Change

if (strlen($password < 6)) {

To

if (strlen($password) < 6) {

Upvotes: 1

Pradeep Dhawan
Pradeep Dhawan

Reputation: 126

use this

 if (isset($password)) {
     if (strlen($password) < 6) {
         $password_err = "password must be at least 6 characters";
     }
 }

Just a misplacement of close bracket

Upvotes: 1

Kamae
Kamae

Reputation: 551

You are using strlen in a bad way. You should use strlen($variable) and then the comparison you want (in your case, < 6)

So the line on your code should be this:

if (strlen($password) < 6) { ... }

Upvotes: 5

Related Questions