PHP Lesson
I feel so proud. My first post that isn’t a rant. Now, those who deem my blog worthy of their limited surf time will get a brief lesson in PHP that should minimize your code a bit.
Now, normally we use something like this for ifs:
if ($variable == 'Hello') {
right? Now what if we have lots of variables?
if ($variable == 'Hello' && $variable2 == 'Hello' && $variable3 == 'Hello' && $variable4 == 'Hello' && $variable5 == 'Hello') {
There is an easier way, and it minimizes if sizes.
$variable1 = 'Hello'; // These would be set already
$variable2 = 'Hello';
$variable3 = 'Hello';
$variable4 = 'Hello';
$variable5 = 'Hello';
$varray = array($variable1,
$variable2,
$variable3,
$variable4,
$variable5);
$val = array_fill(0, 5, 'Hello');
if ($varray == $val) {
echo 'Equal';
}
Tada, easy.
As suggested by a commenter, I am also including an example where this technique could be useful in practice.
$varray = array($_POST[’user’],
$_POST[’pass’],
$_POST[’verpass’],
$_POST[’email’],
$_POST[’name’]);
$val = array_fill(0, 5, NULL);
if ($varray != $val) {
// Execute code for form processing
} else {
// Display error
}
This would check if any of the form elements were empty, saving the need for large if conditional statements.
Note: this is outdated. Better methods are easy to come by, just check the comments below.
May 7th, 2005 at 2:14 am
array_fill is useful only when you want to fill in only a single value in all the array items. So for comparing here, I can’t think up of a situation where you’ll create multiple variables that have same values!!! why??
May 7th, 2005 at 9:12 am
It would be very useful in checking that all of a form’s values were
not NULL, as the snippit I posted checks to see if the array has any
difference from the array_fill. You could fill the array with NULL,
then have:
$varray = array($_POST[’user’],
$_POST[’pass’],
$_POST[’verpass’],
$_POST[’email’],
$_POST[’name’]);
$val = array_fill(0, 5, NULL);
if ($varray != $val) {
// Execute code for form processing
}
I hope this helps you understand a bit better.
November 14th, 2005 at 10:18 pm
While this is quite a nice method for basic checking, I doubt its integrity when it comes to realistic data checking… For example, the code above would accept spaces as the input for everything.
November 14th, 2005 at 11:08 pm
Indeed, but this is just basic as you said. I could also add a trim() to it.
December 19th, 2005 at 7:39 pm
Nice code
. However when I’m checking a lot of forms there is an easy way to do it…now I’m not sure if this is correct standards (i’m still learning some, and am glad to have the chance to learn more
)
but :
foreach( $_POST as $test ) {
if( $test == “” ) {
die(”Fill out all forms”);
}
}
Now I’m sure I screwed something up, but I’m sure u can fix it for your use.
December 19th, 2005 at 8:16 pm
Indeed…I posted this quite a while ago, and foreach() would be a better method of going about it. Your code looks fine.
April 11th, 2010 at 8:31 am
Вы ошибаетесь. Могу это доказать. Пишите мне в PM, поговорим….
I feel so proud. My first post that isn’t a rant…..
April 20th, 2010 at 6:13 am
Жаль, что сейчас не могу высказаться - нет свободного времени. Но вернусь - обязательно напишу что я думаю….
Now, normally we use something like this for ifs:
if ($variable == ‘Hello’) {
right? Now what if we have lots […….
May 12th, 2010 at 4:01 pm
Я хорошо разбираюсь в этом. Могу помочь в решении вопроса. Вместе мы сможем найти решение….
Now, normally we use something like this for ifs:
if ($variable == ‘Hello’) {
right? Now what if we have lots […….