Jump to content


Photo

How to end the php script


  • Please log in to reply
6 replies to this topic

#1 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 08 March 2006 - 12:15 PM

I've written a php script that contains a IF statement, if the output is False I need the script to stop/end at that point (ie not proceed to the end of the script).

I've already echod a nice warning informing the person of the problem, I just need to know how to end it.

Is there something like end() or stop() that can be used? (if so how)

Thanks in advance!

#2 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 08 March 2006 - 12:18 PM

quit()
die()
or you can do a <meta refresh and forward somewhere else
or a bunch of other things... what is best for you?
[!--quoteo(post=352808:date=Mar 8 2006, 07:15 AM:name=master82)--][div class=\'quotetop\']QUOTE(master82 @ Mar 8 2006, 07:15 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I've written a php script that contains a IF statement, if the output is False I need the script to stop/end at that point (ie not proceed to the end of the script).

I've already echod a nice warning informing the person of the problem, I just need to know how to end it.

Is there something like end() or stop() that can be used? (if so how)

Thanks in advance!
[/quote]


#3 master82

master82
  • Members
  • PipPipPip
  • Advanced Member
  • 182 posts

Posted 08 March 2006 - 12:33 PM

Thanks for the reply.

I dont need to refresh, so I'll be using either quit() or die().

do I simply just put:

if(expression) {
echo "warning message here";
quit();
} else {

or do i need to put something in the brackets (maybe the name of the php file?)



#4 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 08 March 2006 - 12:40 PM

This might me easier:



if(expression) {
die('warning message here');
}
else{
// Execute some other exciting code here!
}

or do i need to put something in the brackets (maybe the name of the php file?)
[/quote]


#5 greycap

greycap
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 08 March 2006 - 09:03 PM

[!--quoteo(post=352825:date=Mar 8 2006, 06:40 AM:name=AV1611)--][div class=\'quotetop\']QUOTE(AV1611 @ Mar 8 2006, 06:40 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
This might me easier:
if(expression) {
die('warning message here');
}
else{
// Execute some other exciting code here!
}

or do i need to put something in the brackets (maybe the name of the php file?)
[/quote]


You can skip the else { }.

#6 AV1611

AV1611
  • Members
  • PipPipPip
  • Advanced Member
  • 997 posts

Posted 08 March 2006 - 09:59 PM

I left it purely for illustrative purposes...

#7 lessthanthree

lessthanthree
  • Members
  • PipPipPip
  • Advanced Member
  • 85 posts
  • LocationUK

Posted 08 March 2006 - 11:18 PM

You might actually want to use break; instead of die() or exit().

Reason being, if you have other code to execute afterwards (even if it is just HTML to "complete" the page)
break will finish it off whereas die and exit will both kill the script exactly where it is.

If you are trying to make your webpages pass validation then it's pretty much essential that you don't exit a script without finishing the necasary html. In this case, break; is the best option. Of course, if it doesn't matter, and nothing else needs to be executed / printed, then die() and exit() are both fine.

Phil.
call me a safe bet, i'm betting i'm not




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users