Page 1 of 1

problem with if condition

Posted: Sun Jan 18, 2004 11:54 am
by orangeapple
Hi !

This is an extract of my php code :


if ($changecheck != "1")
{
if (!$login || !$passe)
{
echo 'You must enter your login and password.<br />'
.'Please go back and try again.';
exit;
} }

else
{
if (!$login || !$passe || !$npasse || !$npasse2)
{
echo 'You must enter all the fields.<br />'
.'Please go back and try again.';
exit;
}
if ($npasse !== $npasse2)
{
echo 'You have made a mistake while entering the passwords.<br />'
.'Please go back and try again.';
exit;
}
}

which is in the middle of an HTML code. The problem is that each time that the condition choosen by the web user arrives on an "exit", the rest of the HTML code isn't executed. Which command could I use instead of "exit" ? I've tried "break / continue", it doesn't work...

Who can help ?

Posted: Sun Jan 18, 2004 12:03 pm
by markl999
If you want the remaining HTML to be displayed then just remove all the exit's. If you then find that more echo's are displaying than you want (i.e 2 errors messages instead of just one) then you need to rework the logic.

Posted: Sun Jan 18, 2004 12:08 pm
by dull1554
i'd do this

Code: Select all

//bunch of HTML
 if (!$login || !$passe)//personally i'd use !isset() 
{
echo 'You must enter your login and password.<br />'
.'Please go back and try again.';
//other bunch of HTML
exit;
}
i thing thats the easiest way or you could set the bunches of HTML to $vars

ex.
$html1 = <<< EOT
//bunch of html
EOT;

then just echo the var where ever you need it

Posted: Sun Jan 18, 2004 12:22 pm
by orangeapple
Thanks for your answers !

dull1554, you mean that i should write the same 2nd bunch of HTML after each "if" condition which has an exit ?

Is this the standard of php code writting in this case ?

Posted: Sun Jan 18, 2004 12:24 pm
by dull1554
yeah thats exactly what i mean, but what would be easier is this;

Code: Select all

$html = <<< EOT
//the html you want to be after the if statement
EOT;

//then do this
if (!$login || !$passe)
{
echo 'You must enter your login and password.<br />'
.'Please go back and try again.';
echo $html;
exit;
}

Posted: Sun Jan 18, 2004 1:01 pm
by orangeapple
thanks a lot !

could you just explain this please :

= <<< EOT

EOT;

Posted: Sun Jan 18, 2004 9:32 pm
by DuFF
Hahaha, I've beat Nay to it!

The example shown with the <<< is called Heredoc (Nay is obsessed with it). You can find more info here.