Jump to content

Validating


Mr. R

Recommended Posts

Hi, i was bored so i was thinking about the different ways i could validate something (login, questions, whatever)

 

I came up with this...

 

<?php

$name1 = "max1";
$name2 = "max";
$name3 = "max";
$error = array();
$array = array();

if ($name1 != "max1") {
$error[1] = "name1 isnt max1. ";

}

if ($name2 != "max2") {
$error[2] = "name2 isnt max2. ";

}

if ($name3 != "max3") {
$error[3] = "name3 isnt max3. ";

}

if ($error == $array) {
echo "Whey!";

}else{
$count = count($error);
        echo "$count problems were found in your answers: <br/>";
foreach ($error as $key => $value) {
	echo "$key. $value <br/>";
}

}

?>

 

Is this a good way to validate?

 

Is there anything on here i could improve on?

 

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/48149-validating/
Share on other sites

Well, for starters, if you were going to do that for a login, you might want to look at strtolower() or strtoupper() or stricmp() to compare to string case-insensitively since you shouldn't deny a login just because their username wasn't the correct case (though you should for passwords).  Anyways, when it comes down to validation there's really no good or bad way to do it.  It comes down to preference and functionality.  Obviously if it doesn't work, I'd say that's a bad way of doing it.  However, when you set errors in an array (as you did below) don't specify the index.  PHP offers the powerful $array[] syntax, so you an simply append an item onto the array.  It's basically the same as the push() function in JavaScript, and the array_push() in PHP.  You can just do $error[] = "name1 isn't max1.";  That way you don't have to specify indexes at all, and it takes a few bytes out of your script size :).

Link to comment
https://forums.phpfreaks.com/topic/48149-validating/#findComment-235370
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.