Question

I have an isset check in php, with if statements checking to see if various functions have returned false. The thing is, it is not quitting after the first false return.

I am sure I am missing something basic in the php syntax.

<?php
$name =  $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];

function nameCheck($name)
{
    if (strlen($name)<1 || strlen($name)>40)
    {
        return false;
    }
    else
    {
        return true;
    }
}

function emailCheck($email)
{
    $regex = "/^[*\w]{1,25}+@[*\w]{1,20}+\.[*\w]{1,10}$/";
    if (preg_match($regex, $email))
    {
        return true;
    }
}

function emailMatch($email, $email2)
{
    if ($email === $email2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

if (isset($_POST['submit']))
{
    if (!nameCheck($name))
    {
        echo '<script type="text/javascript"> alert("Fail name!")</script>';
        return false;
    }
    elseif (!emailCheck($email))
    {
        echo '<script type="text/javascript"> alert("Fail email check!")</script>';
        return false;
    }
    elseif (!emailMatch($email,$email2))
    {
        echo '<script type="text/javascript"> alert("Fail email match!")</script>';
        return false;
    }
    else
    {
    mail ("email@email.com", "this", "message");
    echo '<script type="text/javascript"> alert("Success!")</script>';
    }
}
?>

I am using the alert boxes to text the code. Not only will every alert box show up, but even the success one! What am I doing wrong? tx

This is just code I am playing with to study for an exam, I do not actually need to use it on a website, so it's just practice.

Was it helpful?

Solution

Your code at the moment is effectively:

if (! check) {
    echo();
    return;
}

if (! check) {
    echo();
    return;
}

But return outside of a function isn't going to do anything - there's nowhere for PHP to return control to.

What you need to do is switch those checks so that they're formatted as:

if (isset($_POST['submit']))
{
    if (!nameCheck($name))
    {
        die('<script type="text/javascript"> alert("Fail name!")</script>');
    } 
    elseif
    ......
    } 
 else
    {
    mail ("email@email.com", "this", "message");
    echo '<script type="text/javascript"> alert("Success!")</script>';
    }

Using die will print out the contents of the string, and stop your code being executed right away.

As to functions....

if you're going to be using their return values, you need to make sure that they always return something. At the moment, emailCheck is:

function emailCheck($email)
{
    $regex = "/^[*\w]{1,25}+@[*\w]{1,20}+\.[*\w]{1,10}$/";
    if (preg_match($regex, $email))
    {
        return true;
    }
}

If there's no match, then it will return null; though when you check that in your if statement, that will evaluate to false, so your check won't notice. When you call return, your function immediately stops running, and control passes back to the code that called it, so you can write your check functions as:

function myCheck($parameter) {
    if ($checkhere == 'something') {
        return true;     // returns true to the calling function
                         // stops running the code in this function
    } 
    // The only way this will be run is if the check has failed, so we 
    // don't need an else
    return false;
}

It's exactly the same as using an else, but slightly less typing - it's a difference in style which way you choose.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top