PHP and Mysql For Dynamic Web Sites: Instructor'S Notes
PHP and Mysql For Dynamic Web Sites: Instructor'S Notes
Instructors Notes
Chapter 1: Introduction to PHP
Copyright 2012 by Larry Ullman
Learning Outcomes
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
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.';
Parse Errors
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. */
Variable Types
Boolean
Integer
Floating point
String
Array
Objects
Resources
Null
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!
Printing Variables
print $some_var;
print "Hello, $name";
print 'Hello, $name'; // Won't work!
String Values
'Tobias'
"In watermelon sugar"
'100'
'August 2, 2011'
Performing Concatenation
$city= 'Seattle';
$state = 'Washington';
$address = $city . ', ' . $state;
// Or:
$city= 'Seattle';
$state = 'Washington';
$address = $city;
$address .= ', ';
$address .= $state;
Numeric Values
8
3.14
10823479
-4.29048
4.4e2
Arithmetic Operators
Operator
Meaning
Addition
Subtraction
Multiplication
Division
Modulus
++
Increment
--
Decrement
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>';
?>
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'
Escape Sequences
Code
Meaning
\"
\'
\\
Backslash
\n
Newline
\r
Carriage return
\t
Tab
\$
Dollar sign
Basic Debugging