Stop a function

thanks for your help.
made a few changes, heres the function

function validateAll($Input_Name, $Regex) {

    $RegArray= array(
    'Name' => '/[a-zA-Z]{2,15}/',
    'Phone_Number' => '/\([0-9]{3}\) [0-9]{3}-[0-9]{4}/',
    'City' => '/[a-zA-Z]{3,20}/',
    'State' => '/[a-zA-Z]{2}/'
    );

    if(preg_match($RegArray[$Regex], $Input_Name)) {    
      return true;
    } else {      
      return false;
    } 
}

so, it only return true/false, but I call it 5 times using (I fixed the variable issue so now the SESSION gets set)

if( validateAll($_POST['First_Name'], 'Name')) { $_SESSION['First_Name'] = $_POST['First_Name']; } else { header('Location: index2.php?error=1'); }
if( validateAll($_POST['Last_Name'], 'Name')) { $_SESSION['Last_Name'] = $_POST['Last_Name']; } else { header('Location: index2.php?error=2'); }
if( validateAll($_POST['Phone_Number'], 'Phone_Number')) { $_SESSION['Phone_Number'] = $_POST['Phone_Number']; } else { header('Location: index2.php?error=3'); }
if( validateAll($_POST['City'], 'City')) { $_SESSION['City'] = $_POST['City']; } else { header('Location: index2.php?error=4'); }
if( validateAll($_POST['State'], 'State')) { $_SESSION['State'] = $_POST['State']; } else { header('Location: index2.php?error=5');  }

but this is the result after entering more than 15 characters in the first field. I cant figure out why the page gets redirected to index2.php?error=5 and not simply index2.php?error=1

Thatā€™s because you are missing a lot of stuff. You need the die and the checking for true & false in order for it to work. Here.

function validateAll($Input_Name, $Regex) {

    $RegArray= array(
        'Name' => '/[a-zA-Z]{2,15}/',
        'Phone_Number' => '/\([0-9]{3}\) [0-9]{3}-[0-9]{4}/',
        'City' => '/[a-zA-Z]{3,20}/',
        'State' => '/[a-zA-Z]{2}/'
    );

    if(preg_match($RegArray[$Regex], $Input_Name)) {

        return true;

    } else {

        return false;

    }

}

$First_Name = validateAll($_POST['First_Name'], 'Name');
$Last_Name = validateAll($_POST['Last_Name'], 'Name');
$Phone_Number = validateAll($_POST['Phone_Number'], 'Phone_Number');
$City = validateAll($_POST['City'], 'City');
$State = validateAll($_POST['State'], 'State');

if($First_Name == true) {

    $_SESSION['First_Name'] = $_POST['First_Name'];

} else {

    header('Location: index2.php?error=1');
    die();

}

if($Last_Name == true) {

    $_SESSION['Last_Name'] = $_POST['Last_Name'];

} else {

    header('Location: index2.php?error=2');
    die();

}

if($Phone_Number == true) {

    $_SESSION['Phone_Number'] = $_POST['Phone_Number'];

} else {

    header('Location: index2.php?error=3');
    die();

}

if($City == true) {

    $_SESSION['City'] = $_POST['City'];

} else {

    header('Location: index2.php?error=4');
    die();

}

if($State = true) {

    $_SESSION['State'] = $_POST['State'];

} else {

    header('Location: index2.php?error=5');
    die();

}
1 Like

ohhhhhhhhhhhhh, are both die(); and exit(); the same?

I thought any if statement was like

if(true) {
   //what happens when its true
} else {
  //what happens when its false
}

and since the function either returns true or false, that could be doneā€¦

No, thatā€™s not entirely true. You are most likely thinking about num_rows or rowCount. In order to get the returned value from your function, you have to compare it to true or false. There is no automac for conditions youā€™ve made yourself. If you take a look at this snippet, this is what it sees.

if(function_name($param1, $param2)) {

    // What you really wanted it to do when it's true.
    // Sees the function is defined.
    // No matter what it originally returns, both returned values will end up being in here.

} else {

    // What you really wanted it to do if it's false.

}

You have to compare what you want in the first condition in order for it to correctly check.

if(function_name($param1, $param2) == true) {

    // What you really wanted it to do when it's true.

} else {

    // What you really wanted it to do if it's false.

}

die() and exit() both are the same. Just depends on user preference. Also, be consistent when you use these 2 functions. Donā€™t use exit() in one file and then use die() in another and then include them both in a single file. This makes it really confusing to determine which one you really want to use.

Ah, I didnā€™t know that either. I was thinking that it would work in the way it was shown above.

ETA: Oh, hang on a second, I thought Iā€™d try it. I have this code:

<?php

function testRet($x) {
  if ($x>5) { 
    return true;
	}
  return false;
  }
  
  for ($i=0; $i<10; $i++ ) {
  if (testRet($i)) { echo $i . " true"; } else { echo $i . " false"; }
  }
?>

And it returns this

0 false1 false2 false3 false4 false5 false6 true7 true8 true9 true

so now Iā€™m not sure what you meant above. My function appears to be able to be used without a direct comparison and work the way I expected it to. Or am I misunderstanding?

I am not eniterly sure how it works too. If you attempt to use it without the for loop, it will most likely not work. But if you throw it in a for loop, it looks to behave how you want it to. It doesnā€™t work without that which is odd.


Wait, I might of been wrong. But I am not entirely sure. When I wrote this, I was on my phone. I didnā€™t test it out and it might of been the OP missing the die that actually was not working. Iā€™ll have to go back and test it out when I have a chance.

I think that bit is certainly correct - I didnā€™t notice that, I scrolled past too quickly and was more intent on banging on about how the var_dump() might have stopped the header relocate working due to previous output.

I am not sure about the Boolean return thing, only noticed that quite a few questions on the web use that without noting any issues.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.