Let's talk about how you can terminate a PHP script. A lot of times you'll want to go ahead and exit your script if certain values are true so that you don't continue on with, with processing code. And sometimes you'll do this in a production environment when it's appropriate to, to terminate the script. And a lot of times you'll want to do this when you're debugging or you're in the process of developing your web application. And I'm going to show you two different ways that you can terminate your script. One that you may use in application and the other one you might use in debugging and testing. I'm going to go ahead and open up 0307 project file and here we have some code left over from our previous lesson. And let's talk about how you can exit this script. I have in here a nested loop, I'm going to change that back to just a single loop. And what I'm going to do is I'm going to change this IF Statement to say if I is greater than 24, let's just exit the loop. So I'll say exit and what that will do is that will terminate the PHP code right at the point that the Exit Statement is processed. When a PHP code is terminated, it doesn't just stop processing PHP, it actually tells the web server to completely stop any output to the browser. Which means that if you had HTML code below your PHP code and here you were outputting a bunch of you know HTML code to the browser it would never actually get to the browser. So remember that your Exit Statement halts everything all together. Let's go ahead and see what this looks like. If I open this up in our web browser. And here you can see that we're exiting when I becomes greater than 24. So that's one way that you can stop or terminate your code is by using the Exit Statement. It completely halts the script and stops any output to the browser. This is the statement that you'd probably want to use in production, so in your actual web application. Another way of stopping and terminating your PHP code that is often used in debugging and testing is with the Die Function. This is a built-in function of PHP. The Die Function allows you to in parameters specify a string. So we're going to say this code is stopped and this time when we refresh the browser you can see that we've outputted I until we've reached the Die Function and we've outputted the string to the browser. This is very helpful in debugging because it allows you to test if certain conditions are true and if they are, then to output the values or a specific value. Not only text but also variables. So that's how you can terminate your PHP code. One you would use during your web application is in production and the other one you would probably use during debugging. You could also wrap this Die Function into your own custom function and we'll talk about functions later which would allow you to test if a certain variable or constant is true so that you could easily put your web application into Debug Mode. And then all the Die Statements would function and you could troubleshoot what's going on. And then change the value of that constant or that variable and not have the Die Function used but turn your application back into it's production environment so it's ready to be used. This concludes our lesson on terminating your PHP script.
| Course: | FileMaker and PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 34393 |
| ISBN: | 978-1-61866-077-0 |
| Release Date: | 2012-12-07 |
| Duration: | 7.5 hrs / 97 lessons |
| Work Files: |
Yes |
| Captions: | No |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |