Utkarsh Vishnoi
Utkarsh Vishnoi

Reputation: 149

Multiple logical operators in if..else staterment in php

firstname and lastname are being submitted from a HTML form.

In PHP i want to check that both of them must be atleast 3 characters long and maximum upto 18 characters. How can i check it in an if else statement

Example code is below

$f = $_POST['firstname'];
$l = $_POST['lastname'];
if((strlen($f) < 3 || strlen($f) > 16) && (strlen($l) < 3 || strlen($l) > 16))
{
    echo "Firstname and lastname must be between 3 and 16 characters";
    exit();
}

It is not working what i have done wrong.....

Thanks for help and I hope you understand my problem

Upvotes: 1

Views: 5999

Answers (2)

Lo&#239;c
Lo&#239;c

Reputation: 11942

if((strlen($f) < 3 || strlen($f) > 16) || (strlen($l) < 3 || strlen($l) > 16))

Replace && in the middle by ||

What you did only throws the error when both aren't valid. You want to throw it when one of them is.

Upvotes: 5

Kavin Smk
Kavin Smk

Reputation: 800

try this..

if((strlen($f) <= 3 && strlen($f) >= 18 ) && (strlen($l) <= 3 && strlen($l) >= 18 ))
{
echo "Firstname and lastname must be between 3 and 16 characters";
}
else
{
//query here..
}

Upvotes: 0

Related Questions