Toshi
Toshi

Reputation: 6360

var_dump() returns NULL somehow

I made a function to validate the e-mail address to vaidate and check if they are matched or not.

But it doesn't seem to work because when I var_dump().

I got the null value such as: NULL string(13) [email protected]. Could you give me some advice to fix this? I'm completely stuck.

function email_validate_n_match($value)
{

    if( $value == '') return;
    if( preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/", $value) ) {

        static $count = 0;

        if($count == 0) {
            $value1 = $value;
        } else if($count == 1) {
            $value2 = $value;
        }

        var_dump($value2);

        // if ($value1 == $value2) {
            //  return;
        // }else{
            //  $this->clear = false;
            //  return $this->tag_st . 'Doesn't match.' . $this->tag_ed;
        // }

        $count++;

        return;

    } else {

        $this->clear = false;

        return $this->tag_st . 'Put the correct email address.' . $this->tag_ed;
    }
}

EDIT:

Thanks for the answers.

When I put this

static $count = 0;

if($count == 0) {
    $value1 = $value;
    echo '0';
} else if($count == 1) {
    $value2 = $value;
    echo '1';
}

it outputs 01. On the other hand,

If I remove static, I get 00.

so I think this $count is working, but I'm still confused why I got NULL result above.

Upvotes: 0

Views: 1715

Answers (2)

Kosh
Kosh

Reputation: 6334

why taking the time to validate the email while PHP has a function to do that for you? for example:

if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
 echo "valid";
}else{
 echo "not Valid";
}

here you can find more.

PHP Filters

Upvotes: 1

piergiaj
piergiaj

Reputation: 629

You set $count = 0, then set $value1 = $value, but never set $value2 to anything because $count is not equal to 1, so $value2 is null, which is why var_dump gives you null. You then increase count to 1 using $count++, but the next time it runs, count will be set back to 0.

Upvotes: 1

Related Questions