Post on 13-Dec-2015
1
PHP
Introduction to PHP Basic Syntax of PHP
Variables, Constant, Data Type,Type Casting
Getting Data From Client PHP Database Manipulation
2
Introduction PHP
PHP: Hypertext Preprocessor Originally called “Personal Home Page
Tools” Popular server-side scripting technology Open-source
Anyone may view, modify and redistribute source code
Supported freely by community Platform independent
3
Basic Syntax of PHP
Basic application Scripting delimiters
<? php ?> Must enclose all script code
Variables preceded by $ symbol Case-sensitive
4
Basic Syntax of PHP
End statements with semicolon Comments
// for single line /* */ for multiline
Filenames end with .php by convention
5
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
3
4 <!-- Fig. 26.1: first.php -->
5 <!-- Our first PHP script -->
6
7 <?php
8 $name = "LunaTic"; // declaration
9 ?>
10
11 <html xmlns = "http://www.w3.org/1999/xhtml">
12 <head>
13 <title>A simple PHP document</title>
14 </head>
15
16 <body style = "font-size: 2em">
17 <p>
18 <strong>
19
20 <!-- print variable name’s value -->
21 Welcome to PHP, <?php print( "$name" ); ?>!
22 </strong>
23 </p>
24 </body>
25 </html>
Declare variable $name
Scripting delimiters
Single-line comment
Function print outputs the value of variable $name
7
Basic Syntax of PHP
Variable
Variable –area of memory set aside to store information, and it is assigned a particular identifier by the programmer.
Begin with a dollar ‘$’ sign. To assign a value to variable ,use the equals
sign or assignment ‘= ’ operator.
$author = “William Shakespear”;
8
Basic Syntax of PHP
Case-sensitive :$author = “William
Shakespear”;
$Author = “William Shakespear”;
Above codes actually create two separate variables
10
Basic Syntax of PHP
Data Types
Data type Description int, integer Whole numbers (i.e., numbers without a decimal point). float, double Real numbers (i.e., numbers containing a decimal point). string Text enclosed in either single ('') or double ("") quotes. bool, Boolean True or false. array Group of elements of the same type. object Group of associated data and methods. Resource An external data source. NULL No value. Fig. 26.2 PHP data types.
11
Basic Syntax of PHP
String Data Type
Holds textual information or words, and can even hold full sentences.
Everything stored inside quotation marks automatically becomes text, even numeric data.
$CarType= “Cadillac”;
$EngineSize=“2.6”;
12
Basic Syntax of PHP
String Concatenation
The process of adding one string to another –example : Attach one string to the end of another.
Use .(period) as the concatenation operator.
$Car= $CarType.$EngineSize;
Cadillac2.6
13
Basic Syntax of PHP
Numeric Data Type
Holds two different numeric data types- integers (whole numbers) & doubles (floating point numbers) .
No quotation marks.
$an_integer= 33;
$another_integer = -5797
$a_double= 4.567;
$another_double = -23.2
14
Basic Syntax of PHP
Simple Mathematical Operations
Numerical operators can use to perform mathematical operations.
+ , * , - , / , % (8%5=3)
15
<HTML>
<BODY>
<?php
$Salary = 15000;
$TaxRate = 20;
$Pension = 3;
$BeforePensionIncome = $Salary - (($Salary / 100) * $TaxRate);
$AfterPensionIncome = $BeforePensionIncome - (($BeforePensionIncome/100)*$Pension);
echo "Before Pension Deductions:$BeforePensionIncome<BR>";
echo "After Pension Deductions:$AfterPensionIncome";
?>
</BODY
</HTML>
17
Basic Syntax of PHP
Type Casting
$NewVariable = 13;
$NewVariable = (string) $NewVariable ;
$NewVariable = 13;
$NewVariable = (string) $NewVariable ;
$NewVariable = (integer) $NewVariable ;
18
Basic Syntax of PHPgettype and settype gettype()– to determine the current data type of
variable.gettype($number);
To display :$number = 5;
echo gettype($number);
Output :integer
settype()– to specifically set the data type.$number = 10;
settype($number, “string”);
19
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4 <!-- Fig. 26.3: data.php -->
5 <!-- Demonstration of PHP data types -->
6
7 <html xmlns = "http://www.w3.org/1999/xhtml">
8 <head>
9 <title>PHP data types</title>
10 </head>
11
12 <body>
13
14 <?php
15
16 // declare a string, double and integer
17 $testString = "3.5 seconds";
18 $testDouble = 79.2;
19 $testInteger = 12;
20 ?>
21
Assign a string to variable $testString
Assign a double to variable $testDouble
Assign an integer to variable $testInteger
20
22 <!-- print each variable’s value -->
23 <?php print( $testString ); ?> is a string.<br />
24 <?php print( $testDouble ); ?> is a double.<br />
25 <?php print( $testInteger ); ?> is an integer.<br />
26
27 <br />
28 Now, converting to other types:<br />
29 <?php
30
31 // call function settype to convert variable
32 // testString to different data types
33 print( "$testString" );
34 settype( $testString, "double" );
35 print( " as a double is $testString <br />" );
36 print( "$testString" );
37 settype( $testString, "integer" );
38 print( " as an integer is $testString <br />" );
39 settype( $testString, "string" );
40 print( "Converting back to a string results in
41 $testString <br /><br />" );
42
43 $data = "98.6 degrees";
Print each variable’s value
Call function settype to convert the data type of variable $testString to a double.
Call function settype to convert the data type of variable $testString to an integer.
Convert variable $testString back to a string
21
44
45 // use type casting to cast variables to a
46 // different type
47 print( "Now using type casting instead: <br />
48 As a string - " . (string) $data .
49 "<br />As a double - " . (double) $data .
50 "<br />As an integer - " . (integer) $data );
51 ?>
52 </body>
53 </html>
Use type casting to cast variable $data to different types
23
Basic Syntax of PHP
Constants An identifier , which takes a value that cannot be
changed.
The Define Keyword Need a special keyword define. Don’t need to be prefixed with a dollar sign. Constant names are by convention all in upper case.
define(“FREEZINGPOINTCENTIGRADE”, 0);
Constant name Constant value
24
Basic Syntax of PHP Constant that contains text – enclose the constant value
with quotation marks, example :define(“INDEPENDENCEDAY”, “31st August”);define(“COUNTRY”, “Malaysia”);
You can use echo() to display constants on web pages.
You can also append them to text, by using the concatenate operator.
echo “Independence Day is on” . INDEPENDENCEDAY ;
25
Basic Syntax of PHP
Always make sure that constant name appears outside the quotation marks.
echo “Independence Day is on INDEPENDENCEDAY”;
OUTPUT = Independence Day is the INDEPENDENCEDAY
PHP also has its own built-in constants, example:echo PHP_OS;
(use to reflect values of OS that PHP is running on)
26
Getting Data From Client
Notes can be obtained from:http://metalab.uniten.edu.my/~faridah/getting_data_from_client.doc
27
PHP Database Manipulation
PHP supports for a wide range of databases.
The following databases are
currently supported:
Adabas DInterBase
PostgreSQL dBase FrontBase SQLite Empress mSQL Solid Direct MS-SQL
Sybase Hyperwave MySQL Velocis IBM DB2 ODBC Unix dbm Informix Oracle (OCI7 and OCI8) Ingres
28
What is MySQL?
MySQL is a small database server MySQL is ideal for small and medium
applications MySQL supports standard SQL MySQL compiles on a number of
platforms MySQL is free to download and use
29
Connecting to Database
<html><head><title>Connect Server</title></head><body><?$link = mysql_connect("localhost")or die("Connect Error: ".mysql_error());print "Successfully connected.\n";mysql_close($link);?></body></html>
30
Inserting Data into Database
form_insert_record.php
<html><head><title>Birthdays Insert Form</title></head><body><table width="300" cellpadding="5" cellspacing="0" border="2"><tr align="center" valign="top"><td align="left" colspan="1" rowspan="1" bgcolor="64b1ff"><h3>Insert Record</h3><form method="POST" action="insert.php"><?print "Enter name: <input type=text name=name size=20><br>\n";
31
Inserting Data into Database
print "Enter Birthday: <input type=text name=birthday size=10><br>\n";print "<br>\n";print "<input type=submit value=Submit><input type=reset>\n";?></form></td></tr></table></body></html>
32
Inserting Data into Databaseinsert.php<?$name=$_POST['name']; $birthday=$_POST['birthday']; $db="mydatabase";$link = mysql_connect("localhost");if (! $link)die("Couldn't connect to MySQL");mysql_select_db($db , $link)or die("Couldn't open $db: ".mysql_error());//mysql_query ("INSERT INTO birthdays (name, //birthday) VALUES ('Peggy', 'June4')");mysql_query ("INSERT INTO birthdays (name, birthday) VALUES ('$name','$birthday')");echo "Record Inserted";mysql_close($link);?>
33
Displaying Data from Databasedisplay.php<html><head><title>(Title Here)</title></head><body><?php$db="mydatabase";$link = mysql_connect("localhost");if (! $link)die("Couldn't connect to MySQL");mysql_select_db($db , $link)or die("Couldn't open $db: ".mysql_error());$result = mysql_query( "SELECT * FROM birthdays" )or die("SELECT Error: ".mysql_error());$num_rows = mysql_num_rows($result);
34
Displaying Data from Databaseprint "There are $num_rows records.<P>";print "<table width=200 border=1>\n";while ($get_info = mysql_fetch_row($result)){ print "<tr>\n";foreach ($get_info as $field) print "\t<td><font face=arial size=1/>$field</font></td>\n"; print "</tr>\n";}print "</table>\n";mysql_close($link);?></body></html>
35
Deleting Data from Databaseform_delete.php<html><head><title>Birthdays Delete Form</title></head><body><?$db="mydatabase";$link = mysql_connect("localhost");if (! $link)die("Couldn't connect to MySQL");mysql_select_db($db , $link)or die("Couldn't open $db: ".mysql_error());$result = mysql_query( "SELECT * FROM birthdays" )or die("SELECT Error: ".mysql_error());$num_rows = mysql_num_rows($result);
36
Deleting Data from Databaseprint "There are $num_rows records.<P>";print "<table width=200 border=1>\n";while ($get_info = mysql_fetch_row($result)){ print "<tr>\n";foreach ($get_info as $field) print "\t<td><font face=arial size=1/>$field</font></td>\n"; print "</tr>\n"; }print "</table>\n";mysql_close($link);?><br>
37
Deleting Data from Database<form method="POST" action="delete.php"><pre>Enter Line Number to Delete: <input type="text" name="id" size="5"><input type="submit" value="Submit"><input type="reset"></pre> </form></body></html>
38
Deleting Data from Databasedelete.php<?$id=$_POST['id'];$db="mydatabase";$link = mysql_connect("localhost");if (! $link)die("Couldn't connect to MySQL");mysql_select_db($db , $link)or die("Couldn't open $db: ".mysql_error());mysql_query("DELETE FROM birthdays WHERE id=$id");echo "Record Updated";mysql_close($link);?>