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”);
}
?>
I get echoed:
**Invalid Email!1
<i>
</i>//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
<i>
</i><?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 '<br>';
//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
?>
global
global
[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]
<i>
</i><?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 '<br>';
//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
?>
<i>
</i><?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 '<br>';
//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
?>
<i>
</i>//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
<i>
</i><?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 '<br>';
//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
<i>
</i><?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 '<br>';
//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
?>
<i>
</i><?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 '<br>';
//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
?>
<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
<i>
</i><?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 '<br>';
//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
?>
<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
$email_Error_Mssg = ''; //Local Variable.
<i> </i>if(!filter_var($data,FILTER_VALIDATE_EMAIL))
<i> </i>{
<i>
</i><?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 '<br>';
//Why following IF condition not triggering ?
if(!empty($email_Error_Mssg))
{
die("B. $email_Error_Mssg");
}
?>
``<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>
``
<i>
</i><?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];
}
?>
<i>
</i><?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
}
?>
0.1.9 — BETA 4.24