/    Sign up×
Community /Pin to ProfileBookmark

Why IF Condition Is Overlooked By php Interpreter ?

Folks,

Why is not my final IF condition getting triggered ? The php seems to miss that whole condition. Overlooks it. Why ?

[code]
<?php

$email = ‘asd’; //Testing with Invalid Email.

function validate_email($data = NULL)
{
$email_Error_Mssg = ”; //Local Variable.
if(!filter_var($data,FILTER_VALIDATE_EMAIL))
{
return $email_Error_Mssg = ‘Invalid Email!’;
//die; //Script still flows beyond this line. Remove this line.
}
else
{
return $filtered_email = $data;
}
}

echo validate_email($email);
echo ‘1’;

//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die(“B. $email_Error_Mssg”);
}

?>
[/code]

I get echoed:
**Invalid Email!1**

to post a comment
PHP

13 Comments(s)

Copy linkTweet thisAlerts:
@developer_webauthorApr 17.2021 — Oh! I know why this did not trigger!
<i>
</i>//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}


Because the variable is local variable and I am trying to echo it outside it's function. In that case, why did php not give error ? My error alerting is not ON but I still get error undefined variable when I fail to define a variable.

Now how to fix this so I get the IF triggering ? Need to turn the local variable into global variable somehow. Show me how to do it. Somebody.

Cheers!
Copy linkTweet thisAlerts:
@developer_webauthorApr 17.2021 — This is my attempt.

RESULT: FAIL.

<i>
</i>&lt;?php

$email = 'asd'; //Testing with Invalid Email.

function validate_email($data = NULL)
{
$email_Error_Mssg = ''; //Local Variable.
if(!filter_var($data,FILTER_VALIDATE_EMAIL))
{
$email_Error_Mssg = 'Invalid Email!';
global $email_Error_Mssg;
return $email_Error_Mssg;
//die; //Script still flows beyond this line. Remove this line!
}
else
{
return $filtered_email = $data;
}
}

echo validate_email($email);
echo '1';
echo '&lt;br&gt;';

//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}

?&gt;


I want to see result this getting echoed:

**Invalid Email!

1**


Instead, I see this getting echoed:

**1**
Copy linkTweet thisAlerts:
@NogDogApr 17.2021 — If you do it that way -- which I would not, since global is bad -- I believe you have to declare the global in the function definition before you do anything else. In any case, I'd recommend passing a variable by reference:
[code=php]
function validate_email($data, &$errorMessage) { // note the & before the second parameter
if('whatever you want to do to validate it is false') {
$errorMessage = 'Hey, you cannot do that';
return false;
}
return true;
}

if(validate_email($email, $error)) {
// do stuff with email
} else {
die($error);
}
[/code]
Copy linkTweet thisAlerts:
@developer_webauthorApr 18.2021 — @NogDog#1630509

I am going through your code now.

As for me not declaring the var global. But I did. Look:

**global $email_Error_Mssg;**

Check here:

https://www.webdeveloper.com/d/393752-why-if-condition-is-overlooked-by-php-interpreter/3
Copy linkTweet thisAlerts:
@developer_webauthorApr 18.2021 — @NogDog#1630509

NogDog,

Working now.

I just switched this:

**$email_Error_Mssg = 'Invalid Email!';

global $email_Error_Mssg;**


to this:

**global $email_Error_Mssg;

$email_Error_Mssg = 'Invalid Email!';**


WORKING UPDATE:
<i>
</i>&lt;?php

$email = 'asd'; //Testing with Invalid Email.

function validate_email($data = NULL)
{
$email_Error_Mssg = ''; //Local Variable.
if(!filter_var($data,FILTER_VALIDATE_EMAIL))
{
global $email_Error_Mssg;
$email_Error_Mssg = 'Invalid Email!';

<i> </i> return $email_Error_Mssg;
<i> </i> //die; //Script still flows beyond this line. Remove this line!
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> return $filtered_email = $data;
<i> </i>}
}

echo validate_email($email);
echo '1';
echo '&lt;br&gt;';

//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}

?&gt;


If you like my update then do LIKE this POST.

Based on my fix, it seems before we define a variable, we have to write 'global' before it. Can you confirm this is true ?

EDIT:

No good confirming this NogDog,

Look at this code, I 'global' after defining the var and it is working too.
<i>
</i>&lt;?php
//NOTE: WORKING.
$email = 'fake-invalid-email'; //Testing with Invalid Email.

function validate_email($data = NULL)
{
$email_Error_Mssg = ''; //Local Variable.
global $email_Error_Mssg;
if(!filter_var($data,FILTER_VALIDATE_EMAIL))
{
//global $email_Error_Mssg;
$email_Error_Mssg = 'Invalid Email!';

<i> </i> return $email_Error_Mssg;
<i> </i> //die; //Script still flows beyond this line. Remove this line!
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> return $filtered_email = $data;
<i> </i>}
}

echo validate_email($email);
echo '1';
echo '&lt;br&gt;';

//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}

?&gt;

Copy linkTweet thisAlerts:
@developer_webauthorApr 18.2021 — Anyone can respond to my previous post.
Copy linkTweet thisAlerts:
@developer_webauthorApr 19.2021 — @Sempervivum

I need your opinion.

Look at this code that was not working. I was testing the email filter code with an invalid email.

I wanted to see result where following got echoed:

**Invalid Email!

1**


Instead, I saw this getting echoed:

**1**

It seemed to me this part at the bottom wasn't triggering:
<i>
</i>//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}


FULL ORIGINAL CODE
<i>
</i>&lt;?php
//NOTE: NOT WORKING.
$email = 'fake-invalid-email'; //Testing with Invalid Email.

function validate_email($data = NULL)
{
$email_Error_Mssg = ''; //Local Variable.

<i> </i>if(!filter_var($data,FILTER_VALIDATE_EMAIL))
<i> </i>{
<i> </i> $email_Error_Mssg = 'Invalid Email!';
<i> </i> return $email_Error_Mssg;
<i> </i> die; //Script still flows beyond this line. Remove this line!
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> return $filtered_email = $data;
<i> </i>}
}

echo validate_email($email);
echo '1';
echo '&lt;br&gt;';

//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}


Then I realized, that bottom part of the code (IF) was triggering alright but I was trying to echo a Local Variable **$email_Error_Mssg** outside the function. Hence, it was not getting echoed since I did not convert it to **global**.

Anyway, I later then tried converting or declaring that local variable into a global one like so:
<i>
</i>&lt;?php
//NOTE: NOT WORKING.
$email = 'fake-invalid-email'; //Testing with Invalid Email.

function validate_email($data = NULL)
{
$email_Error_Mssg = ''; //Local Variable.

<i> </i>if(!filter_var($data,FILTER_VALIDATE_EMAIL))
<i> </i>{
<i> </i> $email_Error_Mssg = 'Invalid Email!'; //A
<i> </i> global $email_Error_Mssg; //B
<i> </i>
<i> </i> return $email_Error_Mssg;
<i> </i> //die; //Script still flows beyond this line. Remove this line!
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> return $filtered_email = $data;
<i> </i>}
}

echo validate_email($email);
echo '1';
echo '&lt;br&gt;';

//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}

?&gt;


**QUESTION 1.**

Note the two comment lines "//A", "//B".

**$email_Error_Mssg = 'Invalid Email!'; //A

global $email_Error_Mssg; //B**


Did I get this the wrong way round (upside down) ?

Was it supposed to be like this:

**global $email_Error_Mssg; //B

$email_Error_Mssg = 'Invalid Email!'; //A**


Yes or no ?

Don't forget to answer this question. Like so:

**Answer 1: blah blah.**

Anyway, the code mentioned just above was not working either.

2 codes a fail so far.

Anyway, I switched the lines then.

From this:

**$email_Error_Mssg = 'Invalid Email!'; //A

global $email_Error_Mssg; //B**


To this:

**global $email_Error_Mssg; //B

$email_Error_Mssg = 'Invalid Email!'; //A**


And now the code seems to be working.

I get echoed like I originally wanted. Get echoed:

**Invalid Email!

1**


Here is the working code. Let's call this "Working Code 1".

Note the two comment lines **"//B", "//A".** Lines 12, 11.

**Working Code 1**
<i>
</i>&lt;?php
//NOTE: WORKING.
$email = 'fake-invalid-email'; //Testing with Invalid Email.

function validate_email2($data = NULL)
{
$email_Error_Mssg = ''; //Local Variable.

<i> </i>if(!filter_var($data,FILTER_VALIDATE_EMAIL))
<i> </i>{
<i> </i> global $email_Error_Mssg; //B
<i> </i> $email_Error_Mssg = 'Invalid Email!'; //A
<i> </i>
<i> </i> return $email_Error_Mssg;
<i> </i> //die; //Script still flows beyond this line. Remove this line!
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> return $filtered_email = $data;
<i> </i>}
}

echo validate_email2($email);
echo '1';
echo '&lt;br&gt;';

//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}

?&gt;

**Note the two comment lines "//B", "//A".** Lines 12, 11.


-------------------------------------------------------------------------

QUESTION 2.

Now check this following code out. It is working too. It is a slight modification to the code just mentioned above (Working Code 1).

On this new code, I have commented-out line 11:

**//global $email_Error_Mssg; //B**

And I have added that line of code to line 8 instead, just a few lines above.

Like so:
<i>
</i>$email_Error_Mssg = ''; //Local Variable.
global $email_Error_Mssg; //B

<i> </i>if(!filter_var($data,FILTER_VALIDATE_EMAIL))
<i> </i>{
<i> </i> //global $email_Error_Mssg; //B
<i> </i> $email_Error_Mssg = 'Invalid Email!'; //A


So now 2nd working code looks like this ...

**Working Code 2**
<i>
</i>&lt;?php
//NOTE: WORKING.
$email = 'fake-invalid-email'; //Testing with Invalid Email.

function validate_email2($data = NULL)
{
$email_Error_Mssg = ''; //Local Variable.
global $email_Error_Mssg; //B

<i> </i>if(!filter_var($data,FILTER_VALIDATE_EMAIL))
<i> </i>{
<i> </i> //global $email_Error_Mssg; //B
<i> </i> $email_Error_Mssg = 'Invalid Email!'; //A
<i> </i>
<i> </i> return $email_Error_Mssg;
<i> </i> //die; //Script still flows beyond this line. Remove this line!
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> return $filtered_email = $data;
<i> </i>}
}

echo validate_email2($email);
echo '1';
echo '&lt;br&gt;';

//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}

?&gt;


Is this 2nd working code better than the 1st working code or not ?

That is Question 2.

Don't forget to answer this question. Like so:

**Answer 2: blah blah.**
--------------------------------------



Question 3.

This time, from the above 2nd working code, I switched the lines just before the IF (inside the function).

The 2nd working code was like this:
<i>
</i>$email_Error_Mssg = ''; //Local Variable.
global $email_Error_Mssg; //B

<i> </i>if(!filter_var($data,FILTER_VALIDATE_EMAIL))
<i> </i>{


I switched it to this:
<i>
</i>global $email_Error_Mssg; //B
$email_Error_Mssg = ''; //Local Variable.

<i> </i>if(!filter_var($data,FILTER_VALIDATE_EMAIL))
<i> </i>{


This code is working too. Let's call this Working Code 3.

Here it is ...

**Working Code 3**
<i>
</i>&lt;?php
//NOTE: WORKING.
$email = 'fake-invalid-email'; //Testing with Invalid Email.

function validate_email2($data = NULL)
{
global $email_Error_Mssg; //B
$email_Error_Mssg = ''; //Local Variable.

<i> </i>if(!filter_var($data,FILTER_VALIDATE_EMAIL))
<i> </i>{
<i> </i> //global $email_Error_Mssg; //B
<i> </i> $email_Error_Mssg = 'Invalid Email!'; //A
<i> </i>
<i> </i> return $email_Error_Mssg;
<i> </i> //die; //Script still flows beyond this line. Remove this line!
<i> </i>}
<i> </i>else
<i> </i>{
<i> </i> return $filtered_email = $data;
<i> </i>}
}

echo validate_email2($email);
echo '1';
echo '&lt;br&gt;';

//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}

?&gt;



-------

QUESTION 4.

Which working code should I stick too and why that one over the other 2 working code ?

Can you give your rankings to the 3 working codes, where top best on top, something like so ....

Working Code 3

Working Code 2

Working Code 1
Copy linkTweet thisAlerts:
@SempervivumApr 19.2021 — 
  • 1. When using global, place it at the beginning of the function body.

  • 2. Many of PHP's built in functions use this procedure:


  • ``<i>
    </i>function validate_email2($data = NULL)
    {
    $result = filter_var($data,FILTER_VALIDATE_EMAIL))
    {
    return $result;
    }
    else
    {
    return false;
    }
    }
    $result = validate_email2($data);
    if ($result === false)
    {
    // errorhandling here
    }
    else
    {
    // email address in $result valid, continue
    }<i>
    </i>
    `</CODE>
    Having simplified this so far, it is obvious that there is no benefit in defining an additional function.

    If you like to define the error message inside your function, follow Nogdog's proposal and hand it over as reference to the variable.

    Another option would be to return both, the boolean result and the value, email or error message:
    <CODE>
    `<i>
    </i>function validate_email2($data = NULL)
    {
    $result = filter_var($data,FILTER_VALIDATE_EMAIL))
    if ($result !== false)
    {
    return [true, $result];
    }
    else
    {
    return [false, 'Wrong email address'];
    }
    }
    $result = validate_email2($data);
    if ($result[0] === false)
    {
    // errorhandling here, e. g.:
    echo $result[1];
    }
    else
    {
    // email address in $result valid, continue
    $email_address = $result[1];
    }<i>
    </i>
    ``
    Copy linkTweet thisAlerts:
    @developer_webauthorApr 22.2021 — @Sempervivum#1630573

    Your first code doesn't work. Do double check.

    Your second code also showed error and so I fixed it to this following. Do read the code COMMENTS IN CAPS:
    <i>
    </i>&lt;?php

    $email = 'nogdog'; //ADDED THIS NEW LINE.
    function validate_email2($data = NULL)
    {
    $result = filter_var($data,FILTER_VALIDATE_EMAIL); //REPLACED THE EXTRA ')' WITH ';'.
    if ($result !== false)
    {
    return [true, $result];
    }
    else
    {
    return [false, 'Wrong email address'];
    }
    }
    $result = validate_email2($email); //REPLACED '$data' with '$email'.
    if ($result[0] === false)
    {
    // errorhandling here, e. g.:
    echo $result[1];
    }
    else
    {
    // email address in $result valid, continue
    $email_address = $result[1];
    }

    ?&gt;

    Above code, number 2 of your's, is now working.

    Thanks anyway.

    Can you fix your first code ? It is beyond me.
    Copy linkTweet thisAlerts:
    @developer_webauthorApr 22.2021 — @sempervivum

    I think I managed to fix your code. My update below.

    Note the CAPS in comment.
    <i>
    </i>&lt;?php

    function validate_email2($data = NULL)
    {
    if($result = filter_var($data,FILTER_VALIDATE_EMAIL)) //I ADDED THE 'if(', I SUSPECT YOU MISSED TO TYPE IT.
    {
    return $result;
    }
    else
    {
    return false;
    }
    }
    $result = validate_email2($data);
    if ($result === false)
    {
    // errorhandling here
    }
    else
    {
    // email address in $result valid, continue
    }

    ?&gt;


    Do check my previous post to you, Sempervivum.
    Copy linkTweet thisAlerts:
    @katesmith1304Apr 25.2021 — Since they are not bothering to respond, what version of firmware are you running now? I’m on 1.0.6.07 which is the latest it has prompted me to install

    [canlimacizle.red](https://canlimacizle.red)

    [vidmateApp.win](https://vidmateapp.win/)
    Copy linkTweet thisAlerts:
    @developer_webauthorApr 25.2021 — @katesmith1304#1630837

    What has firmware got to do with any of this ?

    trying to hack into my computer ?
    ×

    Success!

    Help @developer_web spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 4.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...