Jump to content

How to fix these "else if" logical branches?


APD1993

Recommended Posts

I am creating a form that collects the details of users and then any errors cause for the error log to have errors noted and added to it.

 

Part of my coding is this:

 

else if (is_numeric($name)||is_int($name) && is_numeric($surname)||is_int($surname))
{
echo "It seems that you have entered numerical values for the First Name and Surname fields. This has been added to the error log.<br><br>";
error_log("<br><br><b><u>Presence Error</b></u><br>The user has entered numerical values for First Name and Surname fields!<br>First Name entered: " . $name . "<br>Surname entered: " . $surname . "<br>Age entered: " . $age . "<br>Number Of Complete Weeks Since Accident entered: " . $weeks . "<br>Time that form was submitted: " . $subtime . "<br><br>", 3, "C:/xampp/htdocs/errorlog.txt");
}
else if (is_numeric($name)||is_int($name))
{
echo "It seems that you have entered a numerical value for the First Name field. This has been added to the error log.<br><br>";
error_log("<br><br><b><u>Presence Error</b></u><br>The user has entered a numerical value for First Name fields!<br>First Name entered: " . $name . "<br>Surname entered: " . $surname . "<br>Age entered: " . $age . "<br>Number Of Complete Weeks Since Accident entered: " . $weeks . "<br>Time that form was submitted: " . $subtime . "<br><br>", 3, "C:/xampp/htdocs/errorlog.txt");
}
else if (is_numeric($surname)||is_int($surname))
{
echo "It seems that you have entered a numerical value for the Surname field. This has been added to the error log.<br><br>";
error_log("<br><br><b><u>Presence Error</b></u><br>The user has entered a numerical value for the Surname field!<br>First Name entered: " . $name . "<br>Surname entered: " . $surname . "<br>Age entered: " . $age . "<br>Number Of Complete Weeks Since Accident entered: " . $weeks . "<br>Time that form was submitted: " . $subtime . "<br><br>", 3, "C:/xampp/htdocs/errorlog.txt");
}

 

so that if a user enters in numerical values when letters need to be entered, then this gets added to the error log. However, even if I enter in a suitable surname (i.e. Jones), the first path (where both the first name and surname are detected as being numerical) runs when I want the path where only the first name contains just digits to be run. Any idea how to fix this?

 

Any help is appreciated! :)

 

Thanks!

Link to comment
Share on other sites

That's a rather paranoid approach to input validation. You really have to log it? What are you going to do with it, send a nasty letter?

 

&& has higher precedence than ||. If your condition had parentheses it'd look like

(numeric $name) or (integer $name and numeric $surname) or (integer $surname)

That's not what you want.

 

I'd tell you to add parentheses but the facts are that form input is always a string and that is_int() checks the type of variable, not its contents. That means it will never return true for something from a form (eg, $_GET or $_POST). So just get rid of them.

numeric $name or numeric $surname

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.