http://ajpatelit.wordpress http://ajpatelit.wordpress php Hypertext Preprocessor Mr. Amit Patel Dept. of I.T. http://ajpatelit.wordpress.com http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
phpHypertext Preprocessor
Mr. Amit PatelDept. of I.T.
http://ajpatelit.wordpress.comhttp://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP files can contain text, HTML, JavaScript code, and PHP code
PHP code are executed on the server, and the result is returned to the browser as plain HTML
PHP files have a default file extension of ".php"
What is a PHP File?
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
What Can PHP Do? PHP can generate dynamic page content. PHP can create, open, read, write, and close files on the
server. PHP can collect form data. PHP can send and receive cookies. PHP can add, delete, modify data in your database. PHP can restrict users to access some pages on your website. PHP can encrypt data.
With PHP you are not limited to output HTML. You can output images, PDF files, and even Flash movies. You can also output any text, such as XHTML and XML.
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Why PHP?
PHP runs on different platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP has support for a wide range of databases PHP is free. Download it from the official PHP resource:
www.php.net PHP is easy to learn and runs efficiently on the server
side
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
<html><body><h1>My first PHP page</h1>
<?phpecho “Welcome Alpha!";?>
</body></html>
Basic PHP SyntaxA PHP script starts with <?php and ends with ?>:
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Comment in PHP<!DOCTYPE html><html><body>
<?php//This is a PHP comment line
/*This isa PHP commentblock*/?>
</body></html> http
://ajpate
lit.w
ordpres
s.com
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Rules for Variables in PHP• As with algebra, PHP variables can be used to hold values (x=5) or
expressions (z=x+y).• Variable can have short names (like x and y) or more descriptive
names (age, carname, totalvolume).
Rules for PHP variables:• A variable starts with the $ sign, followed by the name of the variable
• A variable name must begin with a letter or the underscore character
• A variable name can only contain alpha-numeric characters and underscores
(A-z, 0-9, and _ )
• A variable name should not contain spaces
• Variable names are case sensitive ($y and $Y are two different variables)http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Variables in PHP
<?php$x=5;$y=6;$z=$x+$y;echo $z;?>
x=5y=6z=x+y
In algebra we use letters (like x) to hold values (like 5).From the expression z=x+y above, we can calculate the value of z to be 11.In PHP these letters are called VARIABLES. http
://ajpate
lit.w
ordpres
s.com
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP is a Loosely Typed Language
$txt="Hello world!";$x=5;
• PHP automatically converts the variable to the correct data type, depending on its value.
• In a strongly typed programming language, we will have to declare (define) the type and name of the variable before using it.
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Variable Scopes
PHP has four different variable scopes:
• Local scope
• Global scope
• Static scope
• Parameter scope
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Local Scopes<?php$x=5; // global scope
function myTest(){echo $x; // local scope}
myTest();echo $x;?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Global Scopes<?php$x=5; // global scope$y=10; // global scope
function myTest(){global $x,$y;$y=$x+$y;}
myTest();echo $y; // output is 15?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
<?php
function myTest(){static $x=0;echo $x;$x++;}
myTest();myTest();myTest();
?>
Static Scopes
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Parameter Scopes
<?php
function myTest($x){echo $x;}
myTest(5);
?>
• A parameter is a local variable whose value is passed to the function by the calling code.
• Parameters are declared in a parameter list as part of the function declaration:
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
String Variables in PHP
<?php$txt="Hello world!";echo $txt;?>
• String variables are used for values that contain characters.• After we have created a string variable we can manipulate it. A
string can be used directly in a function or it can be stored in a variable.
• In the example below, we create a string variable called txt, then we assign the text "Hello world!" to it. Then we write the
value of the txt variable to the output:
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Concatenation String Operators• There is only one string operator in PHP.• The concatenation operator [ . ] is used to join two string values
together.
<?php$txt1="Hello world!";$txt2="What a nice day!";echo $txt1 . " " . $txt2;?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP strlen() functionSometimes it is useful to know the length of a string value.The strlen() function returns the length of a string, in characters.
<?phpecho strlen("Hello world!");?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP strpos() function• The strpos() function is used to search for a character or a
specific text within a string.
• If a match is found, it will return the character position of the first match. If no match is found, it will return FALSE.
<?phpecho strpos("Hello world!","world");?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Date() function
<?phpprint(“Today’s Date is ”.date(“l F d, y”));
?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Arithmetic Operators
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Assignment Operators• The basic assignment operator in PHP is "=". It means that the left
operand gets set to the value of the expression on the right. That is, the value of "$x = 5" is 5.
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Incr/Decr Operators
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Comparison Operators
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Logical Operators
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Array Operators
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP If StatementsSYNTAX:
if (condition) { code to be executed if condition is true; }
<?php$t=date("H");if ($t<"20") { echo "Have a good day!"; }?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
SYNTAX:
if (condition) { code to be executed if condition is true; } else { code to be executed if condition is false; }
PHP If…Else Statements
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP If…Else Statements (cont…)
<?php$t=date("H");if ($t<"20") { echo "Have a good day!"; }else { echo "Have a good night!"; }?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP If…Else If…Else StatementsSYNTAX:
if (condition) { code to be executed if condition is true; }else if (condition) { code to be executed if condition is true; }else { code to be executed if condition is false; } http
://ajpate
lit.w
ordpres
s.com
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
<?php$t=date("H");if ($t<"10") { echo "Have a good morning!"; }else if ($t<"20") { echo "Have a good day!"; }else { echo "Have a good night!"; }?>
PHP If…Else If…Else Statements (cont…)
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Switch Case StatementsSYNTAX:
switch (n){case label1: code to be executed if n=label1; break;case label2: code to be executed if n=label2; break;default: code to be executed if n is different from both label1 and label2;} http
://ajpate
lit.w
ordpres
s.com
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
<?php$favcolor="red";switch ($favcolor){case "red": echo "Your favorite color is red!“; break;case "blue": echo "Your favorite color is blue!“; break;case "green": echo "Your favorite color is green!“; break;default: echo "Your favorite color is None of them!";}?>
PHP Switch Case Statements
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Arrays• An array is a special variable, which can hold more than one
value at a time.• If you have a list of items (a list of car names, for example),
storing the cars in single variables could look like this:
$cars1="Volvo";$cars2="BMW";$cars3="Toyota";
• An array can hold many values under a single name, and you can access the values by referring to an index number.
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP ArraysCreating a php Arrays
• In PHP, the array() function is used to create an array:
In PHP, there are three types of arrays:
Indexed arrays - Arrays with numeric indexAssociative arrays - Arrays with named keysMultidimensional arrays - Arrays containing one or more arrays
Types of php Arrays
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Indexed Arrays• There are two ways to create indexed arrays:• The index can be assigned automatically (index always starts at
0):$cars=array("Volvo","BMW","Toyota");
$cars[0]="Volvo";$cars[1]="BMW";$cars[2]="Toyota";
• The index can be assigned manually:
<?php$cars=array("Volvo","BMW","Toyota");echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";?> http
://ajpate
lit.w
ordpres
s.com
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Associative Arrays• Associative arrays are arrays that use named keys that you
assign to them.• There are two ways to create an associative array:
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
$age['Peter']="35";$age['Ben']="37";$age['Joe']="43";
<?php$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");echo "Peter is " . $age['Peter'] . " years old.";?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Database ConnectivityWhat is MySQL?
• MySQL is a database system used on the web• MySQL is a database system that runs on a server• MySQL is ideal for both small and large applications• MySQL is very fast, reliable, and easy to use• MySQL supports standard SQL• MySQL compiles on a number of platforms• MySQL is free to download and use• MySQL is developed, distributed, and supported by
Oracle Corporation• MySQL is named after co-founder Monty Widenius's
daughter: My
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Open a Connection to the MySQL ServerBefore we can access data in a database, we must open a connection to the MySQL server.
In PHP, this is done with the mysqli_connect() function.
Syntax
PHP Database Connectivity
mysqli_connect(host,username,password,dbname);
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Database Connectivity
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Database ConnectivityOpen a Connection to the MySQL Server
<?php// Create connection$con=mysqli_connect(“localhost”,”root”,”",”temp”);// Check connectionif (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Database ConnectivityClose a Connection to the MySQL Server
<?php$con=mysqli_connect(“localhost",”root”,””,”temp”);
// Check connectionif (mysqli_connect_errno($con)) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
mysqli_close($con);?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Database ConnectivityCreate a Database
<?php$con=mysqli_connect(“localhost”,“root”,“abc123”);// Check connectionif (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }// Create database$sql="CREATE DATABASE temp";if (mysqli_query($con,$sql)) { echo "Database my_db created successfully"; }else { echo "Error creating database: " . mysqli_error(); }?>
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP DatabaseCreate a Table
<?php$con=mysqli_connect(“localhost","peter","",“temp");// Check connectionif (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
// Create table$sql="CREATE TABLE persons(Firstname CHAR(30),Lastname CHAR(30),Age INT)";
// Execute queryif (mysqli_query($con,$sql)) { echo "Table persons created successfully"; }else { echo "Error creating table: " . mysqli_error(); }?>
PHP Database
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP DatabasePrimary Keys and Auto Increment Fields
$sql = "CREATE TABLE Persons (PID INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(PID),Firstname CHAR(15),Lastname CHAR(15),Age INT(3))";
Each table in a database should have a primary key field.
The following example sets the PID field as the primary key field. The primary key field is often an ID number, and is often used with the AUTO_INCREMENT setting. AUTO_INCREMENT automatically increases the value of the field by 1 each time a new record is added. To ensure that the primary key field cannot be null, we must add the NOT NULL setting to the field:
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
PHP Database
<?php$con=mysqli_connect(“localhost",”root”,””,”temp");// Check connectionif (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }
mysqli_query($con,"INSERT INTO Persons (FirstName, LastName, Age)VALUES ('Peter', 'Griffin',35)");
mysqli_query($con,"INSERT INTO Persons (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire',33)");
mysqli_close($con);?>
Inserting Data into the Table
http://a
jpatelit
.word
press.c
om
http://ajpatelit.wordpress.com
http://ajpatelit.wordpress.com
Yet I will Not Consider this Presentation 100%.
New Topic will be added at the end of the Slide after the Mid Sem Examination.
By Mr. Amit Patel
http://a
jpatelit
.word
press.c
om