First, have a read of my reply at this thread:
http://p2p.wrox.com/topic.asp?TOPIC_ID=17719
Quote:
quote:
Now, that all makes sense, but what I dont understand is that if the $action variable isn't set, how can the code in the book get this to work without an If statement?
And the other problem is that the first time the page is opened, $action will not be set, so even register_form() won't execute, meaning that we won't even see the form in the first place!
|
Beginning PHP 4 is a pretty old book, it was written using a very early version of PHP 4. At that time, PHP was much more loose, so the code in the book reflects that. PHP has since evolved and become more explicit.
Code:
switch($action) {
case "register":
create_account();
break;
default:
html_header();
register_form();
html_footer();
break;
}
This code takes into account that $action doesn't exist or contain a value by the default case of the switch loop, which means wrapping in if (isset($action)) isn't an appropriate fix here. An appropripriate fix that offers the same functionality is:
Code:
if (!isset($action))
{
$action = '';
}
switch($action) {
case "register":
create_account();
break;
default:
html_header();
register_form();
html_footer();
break;
}
This gives $action a value by initializing it and let's the original code retain its logic. So what's good about this? This goes back to my talk about validation and knowing what values to expect in your variables, which I mentioned in the thread above. By developing with NOTICE level errors you naturally write more explicit and more secure code.
The truth is Beginning PHP4 is outdated! Most of the examples in that book just don't jive with current best practice.
register_globals is another common question, find answers here:
http://p2p.wrox.com/archive/beginnin...2002-11/17.asp
HTH!
Regards,
Rich
--
[
http://www.smilingsouls.net]
[
http://pear.php.net/Mail_IMAP] A PHP/C-Client/PEAR solution for webmail