problem with if condition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

problem with if condition

Post 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 ?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

Post 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 ?
User avatar
dull1554
Forum Regular
Posts: 680
Joined: Sat Nov 22, 2003 11:26 am
Location: 42:21:35.359N, 76:02:20.688W

Post 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;
}
User avatar
orangeapple
Forum Commoner
Posts: 70
Joined: Tue Jan 06, 2004 1:24 pm
Location: Geneva / Switzerland

Post by orangeapple »

thanks a lot !

could you just explain this please :

= <<< EOT

EOT;
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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.
Post Reply