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.


9 Responses to “PHP Lesson”

  1. Amit Gupta Says:

    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??

  2. Velox Letum Says:

    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.

  3. Matt Says:

    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.

  4. Velox Letum Says:

    Indeed, but this is just basic as you said. I could also add a trim() to it.

  5. Jonathon Says:

    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. :)

  6. Velox Letum Says:

    Indeed…I posted this quite a while ago, and foreach() would be a better method of going about it. Your code looks fine.

  7. Kylie Batt Says:

    Вы ошибаетесь. Могу это доказать. Пишите мне в PM, поговорим….

    I feel so proud. My first post that isn’t a rant…..

  8. Kylie Batt Says:

    Жаль, что сейчас не могу высказаться - нет свободного времени. Но вернусь - обязательно напишу что я думаю….

    Now, normally we use something like this for ifs:
    if ($variable == ‘Hello’) {
    right? Now what if we have lots […….

  9. Kylie Batt Says:

    Я хорошо разбираюсь в этом. Могу помочь в решении вопроса. Вместе мы сможем найти решение….

    Now, normally we use something like this for ifs:
    if ($variable == ‘Hello’) {
    right? Now what if we have lots […….