Mr. R Posted April 22, 2007 Share Posted April 22, 2007 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 More sharing options...
Glyde Posted April 22, 2007 Share Posted April 22, 2007 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 More sharing options...
Mr. R Posted April 22, 2007 Author Share Posted April 22, 2007 Cool, thanks for the advice Glyde. I wouldnt validate a login like that, i was just talking about putting the errors into an array, and i just whipped up a quick example. Link to comment https://forums.phpfreaks.com/topic/48149-validating/#findComment-235374 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.