SlideShare a Scribd company logo
1 of 65
What is PHP?
 PHP stands for PHP: Hypertext Preprocessor
 PHP is a server-side scripting languagePHP scripts are
executed on the server.
 PHP supports many databases (MySQL, Informix, Oracle,
Sybase, Solid, PostgreSQL, Generic ODBC, etc.)
 PHP is an open source software (OSS)
1
Why PHP?
 PHP runs on different platforms (Windows, Linux, Unix, etc.)
 PHP is compatible with almost all servers used today (Apache, IIS,
etc.)
 PHP is FREE to download from the official PHP resource:
www.php.net
 PHP is easy to learn
2
Where to Start?
 Install an Apache server on a Windows or Linux machine
 Install PHP on a Windows or Linux machine
 Install MySQL on a Windows or Linux machine
3
 Download PHP for free here: http://www.php.net/downloads.php
 Download MySQL for free here:
http://www.mysql.com/downloads/index.html
 Download Apache for free here: http://httpd.apache.org/download.cgi
4
Basic PHP Syntax
 A PHP scripting block always
 starts with <?php and
 ends with ?>
 Example
<?php
…….….
………..
?>
5
What is a PHP File?
 A PHP file normally contains
 HTML tags, just like an HTML file
 some PHP scripting code
6
Combining HTML and PHP
<html>
<head>
<title>A PHP script including HTML</title>
</head>
<body>
<b>
"hello world- HTML";
<?php
echo "hello world-PHP";
?>
</b>
</body>
</html>7
Comments in PHP
 In PHP, we use
// or # to make a single-line comment
/* and */ to make a large comment block.
8
Example
<html>
<body>
<?php
//This is a single-line comment
/*
This is
a comment
block
*/
?>
</body>
</html>
9
Variables in PHP
 Variables are used for storing a values, like text strings, numbers
or arrays.
 When a variable is set it can be used over and over again in your
script
 All variables in PHP start with a $ sign symbol.
 The correct way of setting a variable in PHP:
$var_name = value;
Eg: $name=“Sunil”;
10
Example
<?php
$txt = "Hello World!";
$number = 16;
echo $txt;
echo $number;
?>
11
Variable Naming Rules
A variable name must start with a
letter or an underscore "_"
A variable name can only contain
alpha-numeric characters and
underscores (a-Z, 0-9, and _ )
A variable name should not contain
spaces. If a variable name is more
than one word, it should be separated
with underscore ($my_string), or with
capitalization ($myString)
12
PHP String
 String variables are used for values that contains
character strings.
 Example 1:
<?php
$txt="Hello World";
echo $txt;
?>
The output of the code above will be:
Hello World
13
PHP String
 Example 2:
<?php
$txt1="Hello World";
$txt2="1234";
echo $txt1 . " " . $txt2;
?>
The output of the code above will be:
Hello World 1234
14
PHP String
 Example 2:
<?php
echo strlen("Hello world!");
?>
The output of the code above will be:
12
15
PHP Operators
 Arithmetic Operators
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus (division remainder)
++ Increment
-- Decrement
16
PHP Operators
Comparison Operators
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns
false
<= is less than or equal to5<=8 returns true
17
Conditional Statements
If...Else Statement
 If you want to execute some code if a condition is true
and another code if a condition is false, use the if....else
statement.
 Syntax
if (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
18
Conditional Statements
Example 1:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else
echo "Have a nice day!";
?>
</body>
</html>
19
Conditional StatementsExample 2:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
echo "See you on Monday!";
}
?>
</body>
</html>
20
Conditional Statements
 If you want to execute some code if one of several conditions are
true use the elseif statement
 Syntax
if (condition)
code to be executed if condition is true;
elseif (condition)
code to be executed if condition is true;
else
code to be executed if condition is false;
21
Conditional StatementsExample 3:
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
echo "Have a nice day!";
?>
</body>
</html>
22
PHP Switch Statement
 If you want to select one of many blocks of code to be executed,
use the Switch statement.
 The switch statement is used to avoid long blocks of if..elseif..else
code.
23
PHP Switch StatementSyntax
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different from both label1 and label2;
}24
PHP Switch StatementExample:
<html>
<body>
<?php
$x=2;
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
25
Exercise :
 Assign values to two variables. Use
comparison operators to test whether
the first value is
 The same as the second
 Less than the second
 Greater than the second
 Less than or equal to the second
 Print the result of each test to the
browser.
 Change the values assigned to your test
variables and run the script again.
26
PHP Arrays
What is an array?
 When working with PHP, sooner or later, you might want to
create many similar variables.
 Instead of having many similar variables, you can store the
data as elements in an array.
 Each element in the array has its own ID so that it can be
easily accessed.
27
PHP Arrays : Example 1
<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbors";
?>
The code above will output:
Quagmire and Joe are Peter's neighbors
28
PHP Arrays : Example 2
<?php
$names = array("Peter","Quagmire","Joe");
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbors";
?>
The code above will output:
Quagmire and Joe are Peter's neighbors
29
Multidimensional Arrays
 In a multidimensional array,
 each element in the main array can also be an array.
 each element in the sub-array can be an array, and so on
30
Multidimensional Arrays
 In a multidimensional array,
 each element in the main array can also be an array.
 each element in the sub-array can be an array, and so on
31
PHP Looping
Looping statements in
PHP are used to execute
the same block of code a
specified number of
times.
32
PHP Looping
Looping statements in PHP are
used to execute the same block of
code a specified number of times.
In PHP we have the following
looping statements:
 while - loops
 do...while - loops
 for - loops
 foreach - loops33
The while Statement
 The while statement will execute a block of code if and
as long as a condition is true.
 Syntax
while (condition)
{
statement 1;
statement 2;
}
34
The while Statement
Example
.
.
.
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
.
.
.35
do...while Statement
The do...while statement will execute a
block of code at least once - it then will
repeat the loop as long as a condition
is true.
Syntax
do
{
code to be executed;
}
while (condition);
36
do...while Statement
 Example
.
.
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
.
.
37
The for Statement
 The for statement is used when you know how many
times you want to execute a statement or a list of
statements.
 Syntax
for (initialization; condition; increment)
{
code to be executed;
}
38
The for Statement
 Example
.
.
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
.
.
39
The foreach Statement
The foreach statement is used to loop
through arrays.
For every loop, the value of the current array
element is assigned to $value (and the array
pointer is moved by one) - so on the next
loop, you'll be looking at the next element.
Syntax
foreach (array as value)
{
code to be executed;
}
40
The foreach Statement
Example
The following example demonstrates a loop
that will print the values of the given array:
<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body>
</html>
41
PHP Functions
 A function is a block of code that can be
executed whenever we need it.
 Creating PHP functions:
 All functions start with the word "function()"
 Name the function - It should be possible to
understand what the function does by its name.
The name can start with a letter or underscore
(not a number)
 Add a "{" - The function code starts after the
opening curly brace
 Insert the function code
 Add a "}" - The function is finished by a closing
curly brace
42
Example 1
A simple function that writes my name when it is called:
<html>
<body>
<?php
function writeMyName()
{
echo “Harvendra Pal Singh";
}
writeMyName();
?>
</body>
</html>
43
Example 2
<html>
<body>
<?php
function writeMyName()
{
echo "Kai Jim Refsnes";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>
44
Example 3
 The following example will write different first names,
<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " .<br />";
}
echo "My name is ";
writeMyName(“Nimal");
echo "My name is ";
writeMyName(“Kamal");
echo "My name is ";
writeMyName(“Amal");
?>
</body>
</html>45
Example 4
 The following function has two parameters
<html>
<body>
<?php
function writeMyName($fname,$lname)
{
echo $fname . " Refsnes" . $lname . "<br />";
}
echo "My name is ";
writeMyName(“Nimal",“Amarasinghe");
echo "My name is ";
writeMyName(“Kamal",“Perera");
echo "My name is ";
writeMyName(“Amal",“Silva");
?>
</body>
</html>
46
PHP Functions - Return values
Functions can also be used to return values.
Example
<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>
47
PHP Forms
 The PHP $_GET and $_POST variables are used to
retrieve information from forms, like user input.
48
Example
 Welcome.htm
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
49
Example
 welcome.php
<html>
<body>
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
50
PHP $_GET
 The $_GET variable is used to collect values from a form
with method="get".
 Information sent from a form with the GET method is
visible to everyone (it will be displayed in the browser's
address bar)
 It has limits on the amount of information to send (max.
100 characters).
51
Example
<form action="welcome.php"
method="get">
Name: <input type="text"
name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
 Welcome.php
Welcome <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!
52
Note:
 When using the $_GET variable all variable names and
values are displayed in the URL.
 So this method should not be used when sending
passwords or other sensitive information!
 The HTTP GET method is not suitable on large variable
values; the value cannot exceed 100 characters.
53
PHP $_POST
 The $_POST variable is used to collect values from a
form with method="post".
 Information sent from a form with the POST method is
invisible to others and has no limits on the amount of
information to send.
54
Example
<form action="welcome.php" method="post">
Enter your name: <input type="text" name="name"
/>
Enter your age: <input type="text" name="age" />
<input type="submit" />
</form>
Welcome.php
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!55
PHP Database
56
Connecting to a MySQL
Database
 Before you can access and work with data in a
database, you must create a connection to the
database.
 In PHP, this is done with the mysql_connect() function.
 Syntax
mysql_connect(servername,username,password
);
 servername Optional. Specifies the server to connect to.
Default value is "localhost:3306"
 username Optional. Specifies the username to log in with.
Default value is the name of the user that owns
the server process
 password Optional. Specifies the password to log in with.
Default is ""
57
Example
 In the following example we store the
connection in a variable ($con). The "die"
part will be executed if the connection
fails:
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// some code
mysql_close($con);
?>
58
Create MySQLDatabase and
Tables
 To get PHP to execute a MySQL statement we must use
the mysql_query() function.
 This function is used to send a query or command to a
MySQL connection.
59
Example
 In the following example we create a database called "my_db":
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
if (mysql_query("CREATE DATABASE my_db",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}
mysql_close($con);
?>
60
Create a Table Example
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// Create table in my_db database
mysql_select_db("my_db", $con);
$sql = "CREATE TABLE person
(
FirstName varchar(15),
LastName varchar(15),
Age int
)";
mysql_query($sql,$con);
mysql_close($con);
61
Insert new records into a
database table
 Example
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
mysql_query("INSERT INTO person (FirstName, LastName, Age)
VALUES ('Peter', 'Griffin', '35')");
mysql_query("INSERT INTO person (FirstName, LastName, Age)
VALUES ('Glenn', 'Quagmire', '33')");
mysql_close($con);
?>
62
Insert Data From a Form Into
a Database
 Example : insert.html
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
63
Insert Data From a Form Into
a Database Example : insert.php
<?php
$con = mysql_connect("localhost","peter","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$sql="INSERT INTO person (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
64
Thank you.....
Arena Animation Siliguri,
Niladri Karmakar
65
Siliguri

More Related Content

What's hot

What's hot (20)

Java Script ppt
Java Script pptJava Script ppt
Java Script ppt
 
Php basics
Php basicsPhp basics
Php basics
 
Chapter 02 php basic syntax
Chapter 02   php basic syntaxChapter 02   php basic syntax
Chapter 02 php basic syntax
 
Express JS
Express JSExpress JS
Express JS
 
PHP Tutorials
PHP TutorialsPHP Tutorials
PHP Tutorials
 
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js +  Expres...
Node.js Express Tutorial | Node.js Tutorial For Beginners | Node.js + Expres...
 
Lesson 4 constant
Lesson 4  constantLesson 4  constant
Lesson 4 constant
 
jQuery Ajax
jQuery AjaxjQuery Ajax
jQuery Ajax
 
Php
PhpPhp
Php
 
Lesson 6 php if...else...elseif statements
Lesson 6   php if...else...elseif statementsLesson 6   php if...else...elseif statements
Lesson 6 php if...else...elseif statements
 
Php forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligetiPhp forms and validations by naveen kumar veligeti
Php forms and validations by naveen kumar veligeti
 
Php Unit 1
Php Unit 1Php Unit 1
Php Unit 1
 
PHP
PHPPHP
PHP
 
Files in php
Files in phpFiles in php
Files in php
 
PHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and requirePHP - DataType,Variable,Constant,Operators,Array,Include and require
PHP - DataType,Variable,Constant,Operators,Array,Include and require
 
CSS3 Media Queries
CSS3 Media QueriesCSS3 Media Queries
CSS3 Media Queries
 
Introduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHPIntroduction to PHP - Basics of PHP
Introduction to PHP - Basics of PHP
 
JavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScriptJavaScript guide 2020 Learn JavaScript
JavaScript guide 2020 Learn JavaScript
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
PHP Presentation
PHP PresentationPHP Presentation
PHP Presentation
 

Similar to PHP - Web Development

Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaDeepak Rajput
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIALzatax
 
Php(report)
Php(report)Php(report)
Php(report)Yhannah
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQLArti Parab Academics
 
What Is Php
What Is PhpWhat Is Php
What Is PhpAVC
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting languageElmer Concepcion Jr.
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kzsami2244
 
PHP Course (Basic to Advance)
PHP Course (Basic to Advance)PHP Course (Basic to Advance)
PHP Course (Basic to Advance)Coder Tech
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Gheyath M. Othman
 
Php by shivitomer
Php by shivitomerPhp by shivitomer
Php by shivitomerShivi Tomer
 

Similar to PHP - Web Development (20)

Free PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in IndiaFree PHP Book Online | PHP Development in India
Free PHP Book Online | PHP Development in India
 
PHP MATERIAL
PHP MATERIALPHP MATERIAL
PHP MATERIAL
 
Php(report)
Php(report)Php(report)
Php(report)
 
FYBSC IT Web Programming Unit IV PHP and MySQL
FYBSC IT Web Programming Unit IV  PHP and MySQLFYBSC IT Web Programming Unit IV  PHP and MySQL
FYBSC IT Web Programming Unit IV PHP and MySQL
 
What Is Php
What Is PhpWhat Is Php
What Is Php
 
Php a dynamic web scripting language
Php   a dynamic web scripting languagePhp   a dynamic web scripting language
Php a dynamic web scripting language
 
Php intro by sami kz
Php intro by sami kzPhp intro by sami kz
Php intro by sami kz
 
Php
PhpPhp
Php
 
PHP Course (Basic to Advance)
PHP Course (Basic to Advance)PHP Course (Basic to Advance)
PHP Course (Basic to Advance)
 
Basic of PHP
Basic of PHPBasic of PHP
Basic of PHP
 
Day1
Day1Day1
Day1
 
PHP Reviewer
PHP ReviewerPHP Reviewer
PHP Reviewer
 
Intro to php
Intro to phpIntro to php
Intro to php
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Php by shivitomer
Php by shivitomerPhp by shivitomer
Php by shivitomer
 
PHP
PHPPHP
PHP
 
Php
PhpPhp
Php
 
Php i basic chapter 3
Php i basic chapter 3Php i basic chapter 3
Php i basic chapter 3
 

Recently uploaded

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbuapidays
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 

Recently uploaded (20)

Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 

PHP - Web Development

  • 1. What is PHP?  PHP stands for PHP: Hypertext Preprocessor  PHP is a server-side scripting languagePHP scripts are executed on the server.  PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.)  PHP is an open source software (OSS) 1
  • 2. Why PHP?  PHP runs on different platforms (Windows, Linux, Unix, etc.)  PHP is compatible with almost all servers used today (Apache, IIS, etc.)  PHP is FREE to download from the official PHP resource: www.php.net  PHP is easy to learn 2
  • 3. Where to Start?  Install an Apache server on a Windows or Linux machine  Install PHP on a Windows or Linux machine  Install MySQL on a Windows or Linux machine 3
  • 4.  Download PHP for free here: http://www.php.net/downloads.php  Download MySQL for free here: http://www.mysql.com/downloads/index.html  Download Apache for free here: http://httpd.apache.org/download.cgi 4
  • 5. Basic PHP Syntax  A PHP scripting block always  starts with <?php and  ends with ?>  Example <?php …….…. ……….. ?> 5
  • 6. What is a PHP File?  A PHP file normally contains  HTML tags, just like an HTML file  some PHP scripting code 6
  • 7. Combining HTML and PHP <html> <head> <title>A PHP script including HTML</title> </head> <body> <b> "hello world- HTML"; <?php echo "hello world-PHP"; ?> </b> </body> </html>7
  • 8. Comments in PHP  In PHP, we use // or # to make a single-line comment /* and */ to make a large comment block. 8
  • 9. Example <html> <body> <?php //This is a single-line comment /* This is a comment block */ ?> </body> </html> 9
  • 10. Variables in PHP  Variables are used for storing a values, like text strings, numbers or arrays.  When a variable is set it can be used over and over again in your script  All variables in PHP start with a $ sign symbol.  The correct way of setting a variable in PHP: $var_name = value; Eg: $name=“Sunil”; 10
  • 11. Example <?php $txt = "Hello World!"; $number = 16; echo $txt; echo $number; ?> 11
  • 12. Variable Naming Rules A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ) A variable name should not contain spaces. If a variable name is more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString) 12
  • 13. PHP String  String variables are used for values that contains character strings.  Example 1: <?php $txt="Hello World"; echo $txt; ?> The output of the code above will be: Hello World 13
  • 14. PHP String  Example 2: <?php $txt1="Hello World"; $txt2="1234"; echo $txt1 . " " . $txt2; ?> The output of the code above will be: Hello World 1234 14
  • 15. PHP String  Example 2: <?php echo strlen("Hello world!"); ?> The output of the code above will be: 12 15
  • 16. PHP Operators  Arithmetic Operators + Addition - Subtraction * Multiplication / Division % Modulus (division remainder) ++ Increment -- Decrement 16
  • 17. PHP Operators Comparison Operators == is equal to 5==8 returns false != is not equal 5!=8 returns true > is greater than 5>8 returns false < is less than 5<8 returns true >= is greater than or equal to 5>=8 returns false <= is less than or equal to5<=8 returns true 17
  • 18. Conditional Statements If...Else Statement  If you want to execute some code if a condition is true and another code if a condition is false, use the if....else statement.  Syntax if (condition) code to be executed if condition is true; else code to be executed if condition is false; 18
  • 19. Conditional Statements Example 1: <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?> </body> </html> 19
  • 20. Conditional StatementsExample 2: <html> <body> <?php $d=date("D"); if ($d=="Fri") { echo "Hello!<br />"; echo "Have a nice weekend!"; echo "See you on Monday!"; } ?> </body> </html> 20
  • 21. Conditional Statements  If you want to execute some code if one of several conditions are true use the elseif statement  Syntax if (condition) code to be executed if condition is true; elseif (condition) code to be executed if condition is true; else code to be executed if condition is false; 21
  • 22. Conditional StatementsExample 3: <html> <body> <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; elseif ($d=="Sun") echo "Have a nice Sunday!"; else echo "Have a nice day!"; ?> </body> </html> 22
  • 23. PHP Switch Statement  If you want to select one of many blocks of code to be executed, use the Switch statement.  The switch statement is used to avoid long blocks of if..elseif..else code. 23
  • 24. PHP Switch StatementSyntax switch (expression) { case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }24
  • 25. PHP Switch StatementExample: <html> <body> <?php $x=2; switch ($x) { case 1: echo "Number 1"; break; case 2: echo "Number 2"; break; case 3: echo "Number 3"; break; default: echo "No number between 1 and 3"; } ?> </body> </html> 25
  • 26. Exercise :  Assign values to two variables. Use comparison operators to test whether the first value is  The same as the second  Less than the second  Greater than the second  Less than or equal to the second  Print the result of each test to the browser.  Change the values assigned to your test variables and run the script again. 26
  • 27. PHP Arrays What is an array?  When working with PHP, sooner or later, you might want to create many similar variables.  Instead of having many similar variables, you can store the data as elements in an array.  Each element in the array has its own ID so that it can be easily accessed. 27
  • 28. PHP Arrays : Example 1 <?php $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe"; echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors"; ?> The code above will output: Quagmire and Joe are Peter's neighbors 28
  • 29. PHP Arrays : Example 2 <?php $names = array("Peter","Quagmire","Joe"); echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors"; ?> The code above will output: Quagmire and Joe are Peter's neighbors 29
  • 30. Multidimensional Arrays  In a multidimensional array,  each element in the main array can also be an array.  each element in the sub-array can be an array, and so on 30
  • 31. Multidimensional Arrays  In a multidimensional array,  each element in the main array can also be an array.  each element in the sub-array can be an array, and so on 31
  • 32. PHP Looping Looping statements in PHP are used to execute the same block of code a specified number of times. 32
  • 33. PHP Looping Looping statements in PHP are used to execute the same block of code a specified number of times. In PHP we have the following looping statements:  while - loops  do...while - loops  for - loops  foreach - loops33
  • 34. The while Statement  The while statement will execute a block of code if and as long as a condition is true.  Syntax while (condition) { statement 1; statement 2; } 34
  • 35. The while Statement Example . . . $i=1; while($i<=5) { echo "The number is " . $i . "<br />"; $i++; } . . .35
  • 36. do...while Statement The do...while statement will execute a block of code at least once - it then will repeat the loop as long as a condition is true. Syntax do { code to be executed; } while (condition); 36
  • 37. do...while Statement  Example . . $i=0; do { $i++; echo "The number is " . $i . "<br />"; } while ($i<5); . . 37
  • 38. The for Statement  The for statement is used when you know how many times you want to execute a statement or a list of statements.  Syntax for (initialization; condition; increment) { code to be executed; } 38
  • 39. The for Statement  Example . . for ($i=1; $i<=5; $i++) { echo "Hello World!<br />"; } . . 39
  • 40. The foreach Statement The foreach statement is used to loop through arrays. For every loop, the value of the current array element is assigned to $value (and the array pointer is moved by one) - so on the next loop, you'll be looking at the next element. Syntax foreach (array as value) { code to be executed; } 40
  • 41. The foreach Statement Example The following example demonstrates a loop that will print the values of the given array: <html> <body> <?php $arr=array("one", "two", "three"); foreach ($arr as $value) { echo "Value: " . $value . "<br />"; } ?> </body> </html> 41
  • 42. PHP Functions  A function is a block of code that can be executed whenever we need it.  Creating PHP functions:  All functions start with the word "function()"  Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)  Add a "{" - The function code starts after the opening curly brace  Insert the function code  Add a "}" - The function is finished by a closing curly brace 42
  • 43. Example 1 A simple function that writes my name when it is called: <html> <body> <?php function writeMyName() { echo “Harvendra Pal Singh"; } writeMyName(); ?> </body> </html> 43
  • 44. Example 2 <html> <body> <?php function writeMyName() { echo "Kai Jim Refsnes"; } echo "Hello world!<br />"; echo "My name is "; writeMyName(); echo ".<br />That's right, "; writeMyName(); echo " is my name."; ?> </body> </html> 44
  • 45. Example 3  The following example will write different first names, <html> <body> <?php function writeMyName($fname) { echo $fname . " .<br />"; } echo "My name is "; writeMyName(“Nimal"); echo "My name is "; writeMyName(“Kamal"); echo "My name is "; writeMyName(“Amal"); ?> </body> </html>45
  • 46. Example 4  The following function has two parameters <html> <body> <?php function writeMyName($fname,$lname) { echo $fname . " Refsnes" . $lname . "<br />"; } echo "My name is "; writeMyName(“Nimal",“Amarasinghe"); echo "My name is "; writeMyName(“Kamal",“Perera"); echo "My name is "; writeMyName(“Amal",“Silva"); ?> </body> </html> 46
  • 47. PHP Functions - Return values Functions can also be used to return values. Example <html> <body> <?php function add($x,$y) { $total = $x + $y; return $total; } echo "1 + 16 = " . add(1,16); ?> </body> </html> 47
  • 48. PHP Forms  The PHP $_GET and $_POST variables are used to retrieve information from forms, like user input. 48
  • 49. Example  Welcome.htm <html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> 49
  • 50. Example  welcome.php <html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old. </body> </html> 50
  • 51. PHP $_GET  The $_GET variable is used to collect values from a form with method="get".  Information sent from a form with the GET method is visible to everyone (it will be displayed in the browser's address bar)  It has limits on the amount of information to send (max. 100 characters). 51
  • 52. Example <form action="welcome.php" method="get"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form>  Welcome.php Welcome <?php echo $_GET["name"]; ?>.<br /> You are <?php echo $_GET["age"]; ?> years old! 52
  • 53. Note:  When using the $_GET variable all variable names and values are displayed in the URL.  So this method should not be used when sending passwords or other sensitive information!  The HTTP GET method is not suitable on large variable values; the value cannot exceed 100 characters. 53
  • 54. PHP $_POST  The $_POST variable is used to collect values from a form with method="post".  Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send. 54
  • 55. Example <form action="welcome.php" method="post"> Enter your name: <input type="text" name="name" /> Enter your age: <input type="text" name="age" /> <input type="submit" /> </form> Welcome.php Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old!55
  • 57. Connecting to a MySQL Database  Before you can access and work with data in a database, you must create a connection to the database.  In PHP, this is done with the mysql_connect() function.  Syntax mysql_connect(servername,username,password );  servername Optional. Specifies the server to connect to. Default value is "localhost:3306"  username Optional. Specifies the username to log in with. Default value is the name of the user that owns the server process  password Optional. Specifies the password to log in with. Default is "" 57
  • 58. Example  In the following example we store the connection in a variable ($con). The "die" part will be executed if the connection fails: <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // some code mysql_close($con); ?> 58
  • 59. Create MySQLDatabase and Tables  To get PHP to execute a MySQL statement we must use the mysql_query() function.  This function is used to send a query or command to a MySQL connection. 59
  • 60. Example  In the following example we create a database called "my_db": <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } if (mysql_query("CREATE DATABASE my_db",$con)) { echo "Database created"; } else { echo "Error creating database: " . mysql_error(); } mysql_close($con); ?> 60
  • 61. Create a Table Example $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } // Create table in my_db database mysql_select_db("my_db", $con); $sql = "CREATE TABLE person ( FirstName varchar(15), LastName varchar(15), Age int )"; mysql_query($sql,$con); mysql_close($con); 61
  • 62. Insert new records into a database table  Example <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Peter', 'Griffin', '35')"); mysql_query("INSERT INTO person (FirstName, LastName, Age) VALUES ('Glenn', 'Quagmire', '33')"); mysql_close($con); ?> 62
  • 63. Insert Data From a Form Into a Database  Example : insert.html <html> <body> <form action="insert.php" method="post"> Firstname: <input type="text" name="firstname" /> Lastname: <input type="text" name="lastname" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html> 63
  • 64. Insert Data From a Form Into a Database Example : insert.php <?php $con = mysql_connect("localhost","peter","abc123"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("my_db", $con); $sql="INSERT INTO person (FirstName, LastName, Age) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added"; mysql_close($con) ?> 64
  • 65. Thank you..... Arena Animation Siliguri, Niladri Karmakar 65 Siliguri