If you want something you've never had, you must be willing to do something you've never done!

Powered by Blogger.

Php Programming Tutorial "Parameter Errors" 1

Cerita Semalam Aaz | 10:44 PM | ,


If you tried to call preferences.php without the query string you may have noticed the strange behavior. View the page source and pay attention to the text inserted as the attribute's values.
That was caused by PHP trying to "GET" parameters that you didn't pass. To ilustrate this error use this parameter_error.phpprogram:
<?php
echo $_GET['parameter'];
?>
You should have received a message like this:

Notice: Undefined index: parameter in /var/www/html/webbeginner/parameter_error.php on line 2
If you didn't receive this message then edit the php.ini file and make sure the display_errors directive is set to On and theerror_reporting directive is set to E_ALL. In a production machine exposed to the internet set the former to Off. Restart Apache for the directives to take effect.
To avoid this error and have the side effect benefit of default colors in the first page run use the isset() test. It returns True if the passed variable was declared or False if it was not. In our case the variables are the parameters in the $_GET array. Here is the patched preferences_2.php:
<?php
# If the background-color parameter was not passed...
if (!isset($_GET['background-color']))
   # set the $bgcolor variable to the default color lime.
   $bgcolor = "green";
else # If it was passed get it from the $_GET array
   $bgcolor = $_GET['background-color'];

if (!isset($_GET['font-color']))
   $color = "yellow";
else
   $color = $_GET['font-color'];
?>
<html>
<body style="background-color:<?php echo $bgcolor; ?>">
<form method="GET" style="color:<?php echo $color; ?>">

<p>What are your preferences?</p>

<p>
Font color:
<input type="text" name="font-color"
value="<?php echo $color; ?>"></p>

<p>
Background Color: 
<input type="text" name="background-color"
value="<?php echo $bgcolor; ?>"></p>

<input type="submit" value="Submit this form">

</form>
</body>
</html>
Everything in a PHP line after # or // is a commnent. Use it to document your programs. The ! before the isset() function reverts the returned value. It is the same as if we could write (we can't in PHP) NOT isset()

1 Responses So Far:

Related Post

 
Back To Top