php tutorial - PHP - what is php - PHP Tutorial - php programming - learn php - php code - php script
What is PHP - How PHP works


What is web server

http://www.facebook.com/home.php
- The above URL tells the server facebook.com to run the programhome.php and send back its output
Server-Side web programming

- Dynamically edit, change or add any content to a Web page
- Respond to user queries or data submitted from HTML forms
- Access any data or databases and return the results to a browser
- Customize a Web page to make it more useful for individual users
- Provide security since your server code cannot be viewed from a browser
- contains software that allows it to run server side programs
- sends back their output as responses to web requests
- we use PHP
Lifecycle of a PHP web request

Why PHP?
- as of November 2006, there were more than 19 million websites (domain names) using PHP.

Viewing PHP output

PHP syntax template


Single Page in PHP
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
method="post"
…
click below button to copy the code. php tutorial - team
php validation
if (form has been submitted) {
// validate form
}
if (valid submission) {
// action data
} else {
// (re)display form
}
click below button to copy the code. php tutorial - team
if (form has been submitted) {
// validate form
}
…can be implemented as…
if (isset($_POST[‘submit’])) {
// validate form
}
click below button to copy the code. php tutorial - team
Maintain separation in php code - Write safe code in PHP

Accumulate errors.. - Catch Errors
$errors = 0;
$errmsg = ‘’;
$clean = array();
if (isset($_POST[‘submit’])) {
if ($_POST[‘value’] is VALID) {
$clean[‘value’] = $_POST[‘value’];
} else {
$errors++;
$errmsg .= ‘data not valid because…’;
}
// continue testing other fields..
}
click below button to copy the code. php tutorial - team
Now to action or display..
if (form has been submitted) {
// validate form
}
if (valid submission) {
// action data
} else {
// (re)display form
}
click below button to copy the code. php tutorial - team
if (isset($_POST[‘submit’])) &&
$errors===0) {
// action data
} else {
// (re)display form
}
click below button to copy the code. php tutorial - team
Redisplay form in PHP
// if (re)displaying form: print
// error message if redisplaying
if ($error>0) {
echo “<p>errors: $errmsg</p>";
}
click below button to copy the code. php tutorial - team
<label for=“email">Email:</label>
<input name=“email"
size="40"
value="<?php echo
isset($clean[‘email']) ?
htmlentities($clean[‘email']) :
‘default'; ?>"
id=“email"
type="text“ />
click below button to copy the code. php tutorial - team
