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 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 Declare variable $name Scripting delimiters Single-line comment Function print outputs the value of variable $name
6 Basic Syntax of PHP Simple PHP program
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
9 <? $actor=”Sean Connery”; echo $actor; ?>
10 Basic Syntax of 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 = $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 <?php $Salary = 15000; $TaxRate = 20; $Pension = 3; $BeforePensionIncome = $Salary - (($Salary / 100) * $TaxRate); $AfterPensionIncome = $BeforePensionIncome - (($BeforePensionIncome/100)*$Pension); echo "Before Pension Deductions:$BeforePensionIncome "; echo "After Pension Deductions:$AfterPensionIncome"; ?> </BODY
16
17 Basic Syntax of PHP Type Casting $NewVariable = 13; $NewVariable = (string) $NewVariable ; $NewVariable = 13; $NewVariable = (string) $NewVariable ; $NewVariable = (integer) $NewVariable ;
18 Basic Syntax of PHP gettype 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 Assign a string to variable $testString Assign a double to variable $testDouble Assign an integer to variable $testInteger
20 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 Use type casting to cast variable $data to different types
22 Type conversion
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 nameConstant value
24 Basic Syntax of PHP Constant that contains text – enclose the constant value with quotation marks, example : define(“INDEPENDENCEDAY”, “31 st 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:
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 Connect Server <? $link = mysql_connect("localhost")or die("Connect Error: ".mysql_error()); print "Successfully connected.\n"; mysql_close($link); ?>
30 Inserting Data into Database form_insert_record.php Birthdays Insert Form Insert Record <? print "Enter name: \n";
31 Inserting Data into Database print "Enter Birthday: \n"; print " \n"; ?>
32 Inserting Data into Database insert.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 Database display.php (Title Here) <?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 Database print "There are $num_rows records. "; print " \n"; while ($get_info = mysql_fetch_row($result)){ print " \n"; foreach ($get_info as $field) print "\t $field \n"; print " \n"; } print " \n"; mysql_close($link); ?>
35 Deleting Data from Database form_delete.php Birthdays Delete Form <? $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 Database print "There are $num_rows records. "; print " \n"; while ($get_info = mysql_fetch_row($result)){ print " \n"; foreach ($get_info as $field) print "\t $field \n"; print " \n"; } print " \n"; mysql_close($link); ?>
37 Deleting Data from Database Enter Line Number to Delete:
38 Deleting Data from Database delete.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); ?>