Nothing Special   »   [go: up one dir, main page]

PHP and Mysql For Dynamic Web Sites: Instructor'S Notes

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 25

VISUAL QUICKPRO GUIDE

PHP and MySQL for


Dynamic Web Sites 4th Edition

Instructors Notes
Chapter 1: Introduction to PHP
Copyright 2012 by Larry Ullman

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Learning Outcomes

Create a basic PHP script


Execute a PHP script
Send data to the Web browser
Write comments in PHP
Demonstrate how to use variables
Work with string variables, including concatenation and
a few string functions

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Learning Outcomes
Work with numeric variables, including arithmetic and
formatting
Work with constants
Know how PHP treats the two quotation mark types
differently
Recognize common escape sequences
Implement some basic debugging techniques

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Basic PHP Script


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8"/>
<title>Basic PHP Page</title>
</head>
<body>
<p>This is standard HTML.</p>
<?php
?>
</body>
</html>

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Running a PHP Script


Must have a PHP-enabled Web server (e.g., Apache)
Place the PHP script in the Web directory
Access the file in your browser through a URL
(http)

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Sending Data to the


Browser
echo 'Hello, world!';
echo "What's new?";
print 'Hello, world!';
print "What's new?";

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Sending Data to the


Browser
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Using Echo</title>
</head>
<body>
<!-- Script 1.3 - second.php -->
<p>This is standard HTML.</p>
<?php
echo 'This was generated using PHP!';
?>
</body>
</html>

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Sending HTMLto the


Browser
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf8"/>
<title>Hello, World!</title>
</head>
<body>
<?php
echo '<p>Hello, <b>world</b>!</p>';
?>
</body>
</html>

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Escaping Characters
echo "She said, "How are you?""; // Error!
echo 'I'm just ducky.'; // Error!
echo 'She said, "How are you?"';
echo "I'm just ducky.";
echo "She said, \"How are you?\"";
print 'I\'m just ducky.';

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Parse Errors

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Writing Comments
<!-- HTML comment -->
<?php
# Single line PHP comment.
// Another type of single line PHP comment
/* This is a comment
that can go over
multiple lines. */

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Variable Types

Boolean
Integer
Floating point
String

Array
Objects
Resources
Null

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Variable Names

Start with a $
Contain only letters, numbers, and the underscore
The first character after the $ cannot be a number
Are case-sensitive
Use a consistent naming scheme!

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Printing Variables
print $some_var;
print "Hello, $name";
print 'Hello, $name'; // Won't work!

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

String Values

'Tobias'
"In watermelon sugar"
'100'
'August 2, 2011'

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Creating and Using Strings


$first_name = 'Tobias';
$today = 'August 2, 2011';
$var = "Define \"platitude\", please.";
$var = 'Define "platitude", please.';
echo $first_name;
echo "Hello, $first_name";

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Performing Concatenation
$city= 'Seattle';
$state = 'Washington';
$address = $city . ', ' . $state;
// Or:
$city= 'Seattle';
$state = 'Washington';
$address = $city;
$address .= ', ';
$address .= $state;

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Numeric Values

8
3.14
10823479
-4.29048
4.4e2

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Creating and Using


Numbers
$n = 3.14;
$n = round ($n); // 3
$n = 3.142857;
$n = round ($n, 3); // 3.143
$n = 20943;
$n = number_format ($n); // 20,943
$n = 20943;
$n = number_format ($n, 2); // 20,943.00

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Arithmetic Operators
Operator

Meaning

Addition

Subtraction

Multiplication

Division

Modulus

++

Increment

--

Decrement

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Performing Arithmetic
<?php # Script 1.8 - numbers.php
// Set the variables:
$quantity = 30; // Buying 30 widgets.
$price = 119.95;
$taxrate = .05; // 5% sales tax.
// Calculate the total:
$total = $quantity * $price;
$total = $total + ($total * $taxrate); // Calculate and add the tax.
// Format the total:
$total = number_format ($total, 2);
// Print the results:
echo '<p>You are purchasing <b>' . $quantity . '</b> widget(s) at a cost of <b>$'
. $price . '</b> each. With tax, the total comes to <b>$' . $total . '</b>.</p>';
?>

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Creating and Using


Constants
define ('NAME', value);
define ('USERNAME', 'troutocity');
define ('PI', 3.14);
echo "Hello, USERNAME"; // Won't work!
echo 'Hello, ' . USERNAME;

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Quotation Marks
$var
echo
echo
echo
echo

= 'test';
"var is equal to $var";
'var is equal to $var';
"\$var is equal to $var";
'\$var is equal to $var'

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Escape Sequences
Code

Meaning

\"

Double quotation mark

\'

Single quotation mark

\\

Backslash

\n

Newline

\r

Carriage return

\t

Tab

\$

Dollar sign

LEARN THE QUICK AND EASY WAY!

VISUAL QUICKPRO GUIDE

Basic Debugging

Always run PHP scripts through a URL


Know the version of PHP youre using
Enable display_errors
Check the HTML source code
Trust the error message
Take a break!

LEARN THE QUICK AND EASY WAY!

You might also like