7.PHP Part 1
7.PHP Part 1
Part 1
Today’s Goals
Today’s lecture will cover:
Format of a PHP file
Syntax of PHP code and similarities
between PHP code and JavaScript code
Data types
Introduction
PHP: Hypertext Preprocessor
PHP is a server-side Hypertext Markup Language
(HTML)-embedded scripting language (Sun’s Java
Server Pages and Microsoft’s Active Server Pages
PHP is an HTML-embedded scripting language.
Its syntax is borrowed from C, Java and Perl with a
couple of unique PHP-specific features thrown in.
The goal of the language is to allow web
developers to write dynamically generated pages
quickly.
PHP is dynamically typed, it has no type
declarations. The type of a variable is set every
time the variable is assigned a value
Introduction
What you can do with PHP:
As a server-side scripting language, PHP is naturally
used for
form handling and database access. Because database
access has been a prime focus of PHP development, it
has driver support for 15 different database systems.
PHP supports the common electronic mail protocols Post
Office Protocol 3 (POP3) and
Internet Message Access Protocol (IMAP).
It also supports the distributed object architectures
Component Object Model (COM) and Common Object
Request Broker Architecture (CORBA).
Create a customized user experience for visitors based
on information that you have gathered from them.
Allow creation of shopping carts for e-commerce
websites.
Introduction – Continued
PHP has many benefits:
Open Source.
Flexible for integration with HTML – HTML easily
supports the use of the PHP scripting tags.
Suited for complex projects – database support,
vast libraries, and the power of the server allow
PHP to satisfy very complex programming needs.
Fast at running scripts – Even though PHP is a
scripting language, the architecture of the
scripting engine provides for fast execution.
Platform and O/S portable – PHP runs on a variety
of different platforms and operating systems
Installing PHP
Go to PHP.net - Downloads and download the most
recent version of PHP.
OR
Install most recent version of Wamp server or
xamp server.
Installing MySQL's
MySQL Installation Guide for help on installing
General Format of a PHP File
There are four different 3. HTML Script Tags
Structure formants: <script language="php">
1. Default syntax echo "This is HTML script tags.";
<?php </script>
echo "Default Syntax";
?> 4. ASP Style Tags
<%
2. Short open Tags echo 'This is ASP like style';
<? %>
echo "PHP example with short-
tags";
?>
Example: IsSet($fruit)
Output:
TRUE if $fruit currently has a non-NULL value, FALSE
otherwise
Variable Declarations
Start it with the dollar sign ($) followed by a letter
or an underscore (_) to be a variable name. E.g.
$variable_name = Value;
Variables do not need to be declared before you
use them.
Example: $var1 = 25;
Rules for Declaring Variables:
PHP variables must start with a letter or
underscore "_".
PHP variables may only be comprised of alpha-
numeric characters and underscores. a-z, A-Z, 0-9, or _
.
Variables with more than one word should be
separated with underscores. $my_variable
Variables with more than one word can also be
Outputting Values in PHP
1. Echo “ ….”; 2. Print “ ….”;
Example: Examples:
$outputString = "The $outputString =
"Hello, World!";
answer is ";
print $outputString;
echo $outputString,
42, "!";
3. Printf
printf(literal_string,param1,param2, ...)
Examples:
$day = "Tuesday";
$high = 79;
printf("The high on %7s was %3d", $day, $high);
Outputting Values in PHP –
(Continued)
<!DOCTYPE html> Echoing Variables and Text
<html lang = "en">
Strings:
<head>
<?php
<title> today.php </title>
<meta charset = "utf-8" /> $my_string = "Hello Bob. My name
</head> is: ";
<body> echo "$my_string Bobettta <br />";
<p> echo "Hi, I'm Bob. Who are you?
<?php $my_string <br />";
print "<b>Welcome to my echo "Hi, I'm Bob. Who are you?
home page <br /> <br />"; $my_string Bobetta";
print "Today is:</b> ";
?>
print date("l, F jS");
print "<br />";
?>
</p>
</body>
</html>
PHP Reserved Words
PHP Case sensitivity
Compound types
array
object
Using Scalar Types
$answer = false;
$finished = true;
$age = 31;
Using Scalar Types
(continued)
A float has a decimal point and may or may not have
an exponent
$price = 12.34;
$avog_num = 6.02e23; //6.02x10^23
define("PI", 3.141593);
Example:
The and and && operators perform the same operation, as do or and ||.
The difference between them is that the precedence of and and or is lower than that
of && and ||.
Switch-Statement
The switch statement can be used as an alternative to
the if, elseif, else method.
switch($menu)
{
case 1:
print "You picked one";
break;
case 2:
print "You picked two";
break;
default:
print "You did not pick one or two";
break;
}
Switch-Statement
(continued)
Note that if a break is not encountered at the end of a
case, the processor continues through to the next case.
Example: If $var1=1, it will print both lines.
switch($var1)
{
case 1:
print "The value was 1";
default:
print "Pick another option";
break;
}
While-loop
PHP uses the while- Format:
loop just like
JavaScript. while(condition)
Like the if-statement, {
this format also uses statements to execute
a condition placed }
between two
Example:
parenthesis
As long as the
$count = 1;
condition evaluates
while($count < 72)
to true, the program {
continues to execute print "$count ";
the code between the $count++;
curly brackets in a }
round-robin fashion.
do … while
The do … while loop works the same as a while loop
except that the condition is evaluated at the end of
the loop rather than the beginning
Example:
$count = 1;
do
{
print "$count ";
$count++;
}while($count < 72);
for-loop
In the two previous cases, a counter was used
to count our way through a loop.
This task is much better suited to a for-loop.