Variable is resetting?

I am trying to create my own error notification and redirect page in php.
A user will submit a form, then my code will search for unexpected vaules in the page and give back an error message telling the user what to do and redirect them to the original form.
[php]function sendBackMsg ($vasr) {
$sendBackCheck = 1;
$sendBackMessage = $sendBackMessage + “
” + $vasr;
}[/php]

This is the code that I’m using to notify that there is an error and that the user needs to be redirected, and also adding an error message with it.
$SendBackCheck is set to 0 at the top of the php document. After sendBackMsg does its work, it is set to one.
HOWEVER when I check the variable at the end it is reset to 0 somehow…
[php]if ( $sendBackCheck == 1)
{
echo $sendBackMessage;
header(‘Location: error.php’);
exit;
}
[/php]
^it never is 1…

I know sendBackMsg is working correctly because if i put echo $sendBackCheck in the function, it shows that $sendBackCheck has been set to 1. But at the end when i check it in a if statement, $sendBackCheck is always set to 0, even though the only place where I set it to 0 is at the top of the php code.

vars in a function have a local scope to that function to access it outside of the function use the return command to return a value from the function

[php]
function sendBackMsg ($vasr) {
$sendBackCheck = 1;
$sendBackMessage = $sendBackMessage + “
” + $vasr;

return $sendBackCheck;

}
[/php]

but since the variable was created outside the function shouldn’t the function be accesing that ‘global’ variable instead of creating a new local variable?

yes but in this case this ($sendBackCheck = 1;) is created inside the function

[php]function sendBackMsg ($vasr) {
global $sendBackCheck , $sendBackMessage;
$sendBackCheck = 1;
$sendBackMessage = $sendBackMessage + “
” + $vasr;
}[/php]
Alright I changed the code and now the $sendBackCheck variable is staying 1, but no matter what i pass as the parameter for sendBackMsg, $sendBackMessage always stays blank… how do i fix this?

ah in php concatination is done with . not +

try:

[php]$sendBackMessage = $sendBackMessage . “
” . $vasr;[/php]

HOORAY ITS WORKING! THANK YOU!
If i can piggyback one more question onto this…
How come my header function does not work? I put header(‘Location = http://google.com’); as part of the redirect code but it does not redirect the page…
I’m going to put in sleep() later so there’s a delay before the redirect.
here’s the code.
[php]if ( $sendBackCheck == 1)
{
echo $sendBackMessage;
header(‘Location : http://google.com’);
exit;
}[/php]

sure thing,

if there’s ant output before the header is being called it creates and error and will not run I suspect you don’t have errors displayed.

There’s 2 ways around this

  1. make sure the header is called before anything has been sent to the browser
  2. I prefer this way more flexible have <?php ob_start();?> at the top of the file then you can use a header anywhere on the page without any issues. Also flush the buffer by placing ob_flush(); at the bottom of the file.

I tried what you said by putting <?php ob_start();?> above any of my php code, and i put ob_flush(); at the bottom of all my php code, but it didn’t work…

have it without a space after location:

[php]header(‘location: http://google.com’);[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service