Here's a trivial example of using PHP to generate the text for the header:
1 2 3 4 5 6 7 8 910111213
<html> <head><title>Mixing HTML and PHP</title></head> <body> <div class='main'> <h1><?phpprint"It's such a perfect day!";?> </h1> <p>Some paragraph text.</p> </div> </body> </html>
Mixing in PHP (Bad Example)
1 2 3 4 5 6 7 8 91011
<html> <head><title>Mixing HTML and PHP</title></head> <body> <div class='main'><?phpprint"<h1>It's such a perfect day!</h1>";?> <p>Some paragraph text.</p> </div> </body></html>
Commenting your code
We all write code for a reason, we may need at times to share WHY something is the way it is.
123456789
<?php//Single line comment# Also a single line commentphpinfo();//comment after code/* Multi line block, you can add as many lines as you want, but don't write a book */?>
Commenting your code (bad)
1 2 3 4 5 6 7 8 9101112131415
<?php/* We are formating phone numbers by stripping all characters then putting parens around the first 3 numbers adding a space then grouping the next 3 numbers adding a dash then the last 4 numbers */functionformatPhoneNumber($phone_string){if(preg_match('/^\+\d(\d{3})(\d{3})(\d{4})$/',$data,$matches)){$result="($matches[1]) $matches[2]-$matches[3]";return$result;}}?>
- Commenting your code (good)
1 2 3 4 5 6 7 8 910111213
<?php/* The business has a requirement that all phone numbers be formatted with the (123) 123-1234 format */functionformatPhoneNumber($phone_string){if(preg_match('/^\+\d(\d{3})(\d{3})(\d{4})$/',$data,$matches)){$result="($matches[1]) $matches[2]-$matches[3]";return$result;}}?>
What is the difference between Integers and Floats?
Integers are whole numbers Floats are fractional numbers (i.e.
1, 2, 3 vs. 1.5, 2.3, 6.6)
What is a String?
A group of characters enclosed in either single or double quotes
Quotes must match
1234
"This is a string"'This is also a string'"This is 'actually' a string"'This is another "example"'
The Here Document method of quoting strings
1 2 3 4 5 6 7 8 910
print<<<EOFLorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus vulputate, arcu a pellentesque viverra, elit metus pretium dui, nec congue ligula velit vitae nunc. Aliquam est elit, faucibus vitae tincidunt sed, venenatis vitae urna. Duis dignissim vel odio ac convallis. Suspendisse sodales viverra ante, in consectetur nulla finibus a. Integer faucibus auctor ipsum, nec tincidunt metus mattis id. Morbi non leo tristique, facilisis neque a, volutpat purus. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos.EOF;
If there is any PHP code in a web page, the file on the web server should be named with a .php extension, not .html
PHP variable names must begin with a dollar sign $
1
$email=$_POST['email'];
A variable name must be at least one character in length
The first character after the dollar sign $ can be a letter or an underscore _, and characters after that can be a letter, an underscore, or a number
Spaces and special characters other than _ and $ are not allowed in any part of a variable name
$_POST Superglobal
$_POST is a special variable, a superglobal built into PHP that holds form data, and is available throughout an entire script
$_POST is an array and you access the form data by using the name attributes as indexed keys into the array
An example would help. Here's a simple form:
fullname.html
1 2 3 4 5 6 7 8 9101112131415161718
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"xml:lang="en"lang="en"><head><metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/><title>Simple Fullname form</title></head><body><h2>Simple Fullname</h2><formaction="fullname.php"method="post">
First Name:
<inputtype="text"id="firstname"name="firstname"/><br/>
Last Name:
<inputtype="text"id="lastname"name="lastname"/><br/><inputtype="submit"value="Report Full Name"name="submit"/></form></body></html>
The two name attributes in the form are: "firstname" and "lastname". You access the data in those name attributes by using "firstname" and "lastname" as indexed keys into the $_POSTsuperglobal:
fullname.php
1 2 3 4 5 6 7 8 9101112131415
<html><head> <title>Full Name</title></head><body> <h2>Full Name</h2><?php$first_name=$_POST['firstname'];$last_name=$_POST['lastname'];echo"Hi ".$first_name." ".$last_name.". Thanks for submitting the form!";?></body></html>
We can use the same form above for entering a full name along with the database we created in the Demo Lab, however we will have to modify the PHP script to use the database
The first thing we will need to do is connect to the database in our PHP script using mysqli_connect()
12
$dbc=mysqli_connect('localhost','student','student','kemarks1')ordie('Error connecting to MySQL server.');
Next, we build our query and store it to a variable
12
$query="INSERT INTO fullname (first_name, last_name) "."VALUES ('$first_name', '$last_name')";
Then we pass the query to the database using mysqli_query()
<html><head> <title>Full Name</title></head><body> <h2>Full Name</h2><?php$first_name=$_POST['firstname'];$last_name=$_POST['lastname'];$dbc=mysqli_connect('localhost','student','student','kemarks1')ordie('Error connecting to MySQL server.');$query="INSERT INTO fullname (first_name, last_name) "."VALUES ('$first_name', '$last_name')";$result=mysqli_query($dbc,$query)ordie('Error querying database.');mysqli_close($dbc);echo"Hi ".$first_name." ".$last_name.". Thanks for submitting the form!";?></body></html>