Download PHP Programming - Web Design and Development - Lecture Slides and more Slides Web Design and Development in PDF only on Docsity!
PHP Intro/Overview
PHP Basics
- PHP code can go anywhere in an HTML
document as long as its in a PHP tag
Commenting
// Comment
# Comment
/* Comment
Comment */
Print and Echo
print “42”;
- Echo is more robust than print, it can print numbers and more than one parameter, separated by commas
echo 42; // This will actually print 42 $x = “is” echo “The answer ”, $x, “ ”, 42;
New lines
- The string variable $var contains a new
line character “Hello there\n sir!”
$var = “Hello there
sir!”;
- Introducing new lines breaks makes it
easer to write SQL queries that work…
SQL Example
$query = “ SELECT max(orderid)
FROM orders
WHERE custid = $ID”;
- If you send this query string to some
database server it is important that the
new line characters are in the string.
Variables in strings
$x = 256
// “My computer has.”
$message = “My computer has $xMB ram.”;
// “My computer has 256MB ram.”
$message = “My computer has {$x}MB ram.”;
Variables in strings
Using { } is very important when trying to
include complex variables into other
strings.
$message = “Mars has a diameter
of {$planets[‘Mars’][‘dia’]}.”;
Constants
define(“PI”, 3.14159);
print PI;
// outputs 3.
Notice that constants don’t have $
Convention: Use all CAPS for constants
BTW, PHP is case sensitive
Expression and Operators
- Same as most high-level languages
$z = 3;
$x = 1;
$y = 5.3;
$z = $x + $y;
$z++;
$x += $y + 10;
$z = $x * 10;
Conditionals (if statements)
- Same as other high-level languages
if ($var < 5) {
print “The variable is less than 5”;
Compound conditionals
if ($x == 1) {
print “x is 1”;
}
elseif ($x == 2) { // Notice elseif is one word
print “x is 2”;
}
else {
print “x is not 1 and not 2”;
}
Loops: break
for ($c = 0; $c < 10; $c++) {
print $c. “ ”;
if ($c == 5)
break;
Type Conversion
$year = 2003; // This is an integer
$yearString = strval($year);
// $yearString is “2003” not an integer