SlideShare a Scribd company logo
CS Learning Centre
PHP Tutorial
Introduction
⇨ Based on PHP and MySQL Web
Development, Third Edition (Available as
CS eBook from Dal Library)
⇨ Other eBooks from Dal Library
⇨ Learning PHP 5
⇨ PHP Cookbook
⇨ For Online resources, google “PHP”
Table of Contents
⇨ Embedding PHP
⇨ Variables
⇨ Operators and Control Structures
⇨ Array
⇨ Function
⇨ Session Control (Using Cookie)
Embedding PHP in HTML
⇨ Insert PHP tag inside HTML file (with .php
extension
⇨ XML Style
<?php PHP statement; ?>
⇨ Short Style (Need to be enabled)
<? PHP statement; ?>
⇨ Script Style
<SCRIPT LANGUAGE='php'> PHP statement;
</SCRIPT>
⇨ ASP Style (Need to be enabled)
<% PHP statement; %>
⇨ Dynamic Content
function('argument');
⇨ Note: argument is in string
Variables
⇨ Do not require to declare variable type
⇨ Variable variables
$varname = 'tireqty';
$$varname = 5;
⇨ Constants
define('TIREPRICE', 100);
⇨ Accessing form variables (field=tireqty)
⇨ Short style (requires register_globals)
$tieryqty
⇨ Medium style
$_POST['tireqty'] or $_GET['tireqty']
⇨ Long style
$HTTP_POST_VARS['tireqty']
Operators and Control
Structures
⇨ Pretty much same as in other programming
languages (C, Java, etc.)
⇨ Break statements are also same (continue,
break), except it provides exit statement to
break out of the script
⇨ Alternative control structure syntex
if( $totalqty == 0):
echo 'You did not order anything on the previous
page!<br />';
exit;
endif;
Array
⇨ Create an array
$products = array ('Tires', 'Oil', 'Engine');
⇨ Automatically generate sequnces of
number, character
$numbers = range (1,10,2); //last parameter optional(Indicate step)
⇨ Accessing element
$products[0]
⇨ Array with different indices
$prices = array( 'Tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4 );
⇨ Assign key and value to variables
list( $product, $price ) = each( $prices );
Array (Cont'd)
⇨ Multidimensional Array
($products[row][column]
$products = array( array( 'Code' => 'TIR',
'Description' => 'Tires',
'Price' => 100
),
array( 'Code' => 'OIL',
'Description' => 'Oil',
'Price' => 10
),
array( 'Code' => 'SPK',
'Description' => 'Spark Plugs',
'Price' =>4
)
);
Function
⇨ New function
function my_function()
{
echo 'My function was called';
}
⇨ Calling function
my_function();
Function (Cont'd)
⇨ Using argument
⇨ Should reset the argument if it is an array
⇨ The next command gets next element of arg
⇨ The current command gets current element
⇨ Ex.
function create_table2( $data, $border = 1, $cellpadding = 4, $cellspacing = 4 )
{
echo "<table border = $border cellpadding = $cellpadding"
." cellspacing = $cellspacing>";
reset($data);
$value = current($data);
while ($value)
{
echo "<tr><td>$value</td></tr>n";
$value = next($data);
}
echo '</table>';
}
Session Control (Using Cookie)
⇨ Manually setting Cookie in PHP
bool setcookie (string name [, string value [, int
expire [, string path
[, string domain [, int secure]]]]])
Ex. setcookie ('mycookie', 'value');
⇨ Using Cookie with Sessions
⇨ Get session cookie parameters
session_get_cookie_params()
⇨ Set session cookie parameters
session_set_cookie_params($lifetime, $path,
$domain [, $secure]);
Session Control (Cont'd)
⇨ Starting Session (Must be declared at the
beginning of the file)
session_start();
⇨ Registering Session variables
$_SESSION['myvar'] = 5;
⇨ Unsetting variables
⇨ Single variable
unset($_SESSION['myvar']);
⇨ All variables
$_SESSION=array();
⇨ Destroying session
session_destroy();
Session Control (Example)
⇨ Begin session
<?php
session_start();
$_SESSION['sess_var'] = "Hello world!";
echo 'The content of $_SESSION['sess_var'] is '
.$_SESSION['sess_var'].'<br />';
?>
<a href="page2.php">Next page</a>
Session Control (Example)
⇨ Get the variable and unset it
<?php
session_start();
echo 'The content of $_SESSION['sess_var'] is '
.$_SESSION['sess_var'].'<br />';
unset($_SESSION['sess_var']);
?>
<a href="page3.php">Next page</a>
Session Control (Example
⇨ End session
<?php
session_start();
echo 'The content of $_SESSION['sess_var'] is '
.$_SESSION['sess_var'].'<br />';
session_destroy();
?>
Ad

Recommended

Php tutorial handout
Php tutorial handout
SBalan Balan
 
Php reference sheet
Php reference sheet
Silvia Rios
 
Php mysql training-in-mumbai
Php mysql training-in-mumbai
vibrantuser
 
Unit 1
Unit 1
tamilmozhiyaltamilmo
 
Php tips-and-tricks4128
Php tips-and-tricks4128
PrinceGuru MS
 
PHP tips and tricks
PHP tips and tricks
Damien Seguy
 
SULTHAN's - PHP MySQL programs
SULTHAN's - PHP MySQL programs
SULTHAN BASHA
 
Php summary
Php summary
Michelle Darling
 
PHP BASIC PRESENTATION
PHP BASIC PRESENTATION
krutitrivedi
 
Php
Php
samirlakhanistb
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
ShitalGhotekar
 
P H P Part I I, By Kian
P H P Part I I, By Kian
phelios
 
Php
Php
supriya pandit
 
Php with my sql
Php with my sql
husnara mohammad
 
php is the most important programming language
php is the most important programming language
padmanabanm47
 
Industrail training in php
Industrail training in php
ResistiveTechnosource Pvt. Ltd.
 
PHP Workshop Notes
PHP Workshop Notes
Pamela Fox
 
Tutorial_4_PHP
Tutorial_4_PHP
tutorialsruby
 
Tutorial_4_PHP
Tutorial_4_PHP
tutorialsruby
 
Tutorial_4_PHP
Tutorial_4_PHP
tutorialsruby
 
Tutorial_4_PHP
Tutorial_4_PHP
tutorialsruby
 
Php mysql training-in-mumbai
Php mysql training-in-mumbai
Unmesh Baile
 
Google Cloud Challenge - PHP - DevFest GDG-Cairo
Google Cloud Challenge - PHP - DevFest GDG-Cairo
Haitham Nabil
 
Learn PHP Lacture2
Learn PHP Lacture2
ADARSH BHATT
 
Introduction to PHP_ Lexical structure_Array_Function_String
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
Php
Php
khushbulakhani1
 
Php
Php
Tohid Kovadiya
 
Intro to php
Intro to php
Sp Singh
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 

More Related Content

Similar to php tutorial.ppt (20)

PHP BASIC PRESENTATION
PHP BASIC PRESENTATION
krutitrivedi
 
Php
Php
samirlakhanistb
 
PHP SESSIONS & COOKIE.pptx
PHP SESSIONS & COOKIE.pptx
ShitalGhotekar
 
P H P Part I I, By Kian
P H P Part I I, By Kian
phelios
 
Php
Php
supriya pandit
 
Php with my sql
Php with my sql
husnara mohammad
 
php is the most important programming language
php is the most important programming language
padmanabanm47
 
Industrail training in php
Industrail training in php
ResistiveTechnosource Pvt. Ltd.
 
PHP Workshop Notes
PHP Workshop Notes
Pamela Fox
 
Tutorial_4_PHP
Tutorial_4_PHP
tutorialsruby
 
Tutorial_4_PHP
Tutorial_4_PHP
tutorialsruby
 
Tutorial_4_PHP
Tutorial_4_PHP
tutorialsruby
 
Tutorial_4_PHP
Tutorial_4_PHP
tutorialsruby
 
Php mysql training-in-mumbai
Php mysql training-in-mumbai
Unmesh Baile
 
Google Cloud Challenge - PHP - DevFest GDG-Cairo
Google Cloud Challenge - PHP - DevFest GDG-Cairo
Haitham Nabil
 
Learn PHP Lacture2
Learn PHP Lacture2
ADARSH BHATT
 
Introduction to PHP_ Lexical structure_Array_Function_String
Introduction to PHP_ Lexical structure_Array_Function_String
DeepakUlape2
 
Php
Php
khushbulakhani1
 
Php
Php
Tohid Kovadiya
 
Intro to php
Intro to php
Sp Singh
 

Recently uploaded (20)

Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
Microsoft-365-Administrator-s-Guide1.pdf
Microsoft-365-Administrator-s-Guide1.pdf
mazharatknl
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Advance Doctor Appointment Booking App With Online Payment
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
How Automation in Claims Handling Streamlined Operations
How Automation in Claims Handling Streamlined Operations
Insurance Tech Services
 
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Test Case Design Techniques – Practical Examples & Best Practices in Software...
Muhammad Fahad Bashir
 
Key Challenges in Troubleshooting Customer On-Premise Applications
Key Challenges in Troubleshooting Customer On-Premise Applications
Tier1 app
 
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
Enable Your Cloud Journey With Microsoft Trusted Partner | IFI Tech
IFI Techsolutions
 
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
OpenChain Webinar - AboutCode - Practical Compliance in One Stack – Licensing...
Shane Coughlan
 
A Guide to Telemedicine Software Development.pdf
A Guide to Telemedicine Software Development.pdf
Olivero Bozzelli
 
Microsoft-365-Administrator-s-Guide1.pdf
Microsoft-365-Administrator-s-Guide1.pdf
mazharatknl
 
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
CodeCleaner: Mitigating Data Contamination for LLM Benchmarking
arabelatso
 
Introduction to Agile Frameworks for Product Managers.pdf
Introduction to Agile Frameworks for Product Managers.pdf
Ali Vahed
 
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Folding Cheat Sheet # 9 - List Unfolding 𝑢𝑛𝑓𝑜𝑙𝑑 as the Computational Dual of ...
Philip Schwarz
 
Advance Doctor Appointment Booking App With Online Payment
Advance Doctor Appointment Booking App With Online Payment
AxisTechnolabs
 
Best Practice for LLM Serving in the Cloud
Best Practice for LLM Serving in the Cloud
Alluxio, Inc.
 
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
Milwaukee Marketo User Group June 2025 - Optimize and Enhance Efficiency - Sm...
BradBedford3
 
HYBRIDIZATION OF ALKANES AND ALKENES ...
HYBRIDIZATION OF ALKANES AND ALKENES ...
karishmaduhijod1
 
Y - Recursion The Hard Way GopherCon EU 2025
Y - Recursion The Hard Way GopherCon EU 2025
Eleanor McHugh
 
Sysinfo OST to PST Converter Infographic
Sysinfo OST to PST Converter Infographic
SysInfo Tools
 
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Automated Migration of ESRI Geodatabases Using XML Control Files and FME
Safe Software
 
How Automation in Claims Handling Streamlined Operations
How Automation in Claims Handling Streamlined Operations
Insurance Tech Services
 
Building Geospatial Data Warehouse for GIS by GIS with FME
Building Geospatial Data Warehouse for GIS by GIS with FME
Safe Software
 
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
Application Modernization with Choreo - The AI-Native Internal Developer Plat...
WSO2
 
Open Source Software Development Methods
Open Source Software Development Methods
VICTOR MAESTRE RAMIREZ
 
Ad

php tutorial.ppt

  • 2. Introduction ⇨ Based on PHP and MySQL Web Development, Third Edition (Available as CS eBook from Dal Library) ⇨ Other eBooks from Dal Library ⇨ Learning PHP 5 ⇨ PHP Cookbook ⇨ For Online resources, google “PHP”
  • 3. Table of Contents ⇨ Embedding PHP ⇨ Variables ⇨ Operators and Control Structures ⇨ Array ⇨ Function ⇨ Session Control (Using Cookie)
  • 4. Embedding PHP in HTML ⇨ Insert PHP tag inside HTML file (with .php extension ⇨ XML Style <?php PHP statement; ?> ⇨ Short Style (Need to be enabled) <? PHP statement; ?> ⇨ Script Style <SCRIPT LANGUAGE='php'> PHP statement; </SCRIPT> ⇨ ASP Style (Need to be enabled) <% PHP statement; %> ⇨ Dynamic Content function('argument'); ⇨ Note: argument is in string
  • 5. Variables ⇨ Do not require to declare variable type ⇨ Variable variables $varname = 'tireqty'; $$varname = 5; ⇨ Constants define('TIREPRICE', 100); ⇨ Accessing form variables (field=tireqty) ⇨ Short style (requires register_globals) $tieryqty ⇨ Medium style $_POST['tireqty'] or $_GET['tireqty'] ⇨ Long style $HTTP_POST_VARS['tireqty']
  • 6. Operators and Control Structures ⇨ Pretty much same as in other programming languages (C, Java, etc.) ⇨ Break statements are also same (continue, break), except it provides exit statement to break out of the script ⇨ Alternative control structure syntex if( $totalqty == 0): echo 'You did not order anything on the previous page!<br />'; exit; endif;
  • 7. Array ⇨ Create an array $products = array ('Tires', 'Oil', 'Engine'); ⇨ Automatically generate sequnces of number, character $numbers = range (1,10,2); //last parameter optional(Indicate step) ⇨ Accessing element $products[0] ⇨ Array with different indices $prices = array( 'Tires'=>100, 'Oil'=>10, 'Spark Plugs'=>4 ); ⇨ Assign key and value to variables list( $product, $price ) = each( $prices );
  • 8. Array (Cont'd) ⇨ Multidimensional Array ($products[row][column] $products = array( array( 'Code' => 'TIR', 'Description' => 'Tires', 'Price' => 100 ), array( 'Code' => 'OIL', 'Description' => 'Oil', 'Price' => 10 ), array( 'Code' => 'SPK', 'Description' => 'Spark Plugs', 'Price' =>4 ) );
  • 9. Function ⇨ New function function my_function() { echo 'My function was called'; } ⇨ Calling function my_function();
  • 10. Function (Cont'd) ⇨ Using argument ⇨ Should reset the argument if it is an array ⇨ The next command gets next element of arg ⇨ The current command gets current element ⇨ Ex. function create_table2( $data, $border = 1, $cellpadding = 4, $cellspacing = 4 ) { echo "<table border = $border cellpadding = $cellpadding" ." cellspacing = $cellspacing>"; reset($data); $value = current($data); while ($value) { echo "<tr><td>$value</td></tr>n"; $value = next($data); } echo '</table>'; }
  • 11. Session Control (Using Cookie) ⇨ Manually setting Cookie in PHP bool setcookie (string name [, string value [, int expire [, string path [, string domain [, int secure]]]]]) Ex. setcookie ('mycookie', 'value'); ⇨ Using Cookie with Sessions ⇨ Get session cookie parameters session_get_cookie_params() ⇨ Set session cookie parameters session_set_cookie_params($lifetime, $path, $domain [, $secure]);
  • 12. Session Control (Cont'd) ⇨ Starting Session (Must be declared at the beginning of the file) session_start(); ⇨ Registering Session variables $_SESSION['myvar'] = 5; ⇨ Unsetting variables ⇨ Single variable unset($_SESSION['myvar']); ⇨ All variables $_SESSION=array(); ⇨ Destroying session session_destroy();
  • 13. Session Control (Example) ⇨ Begin session <?php session_start(); $_SESSION['sess_var'] = "Hello world!"; echo 'The content of $_SESSION['sess_var'] is ' .$_SESSION['sess_var'].'<br />'; ?> <a href="page2.php">Next page</a>
  • 14. Session Control (Example) ⇨ Get the variable and unset it <?php session_start(); echo 'The content of $_SESSION['sess_var'] is ' .$_SESSION['sess_var'].'<br />'; unset($_SESSION['sess_var']); ?> <a href="page3.php">Next page</a>
  • 15. Session Control (Example ⇨ End session <?php session_start(); echo 'The content of $_SESSION['sess_var'] is ' .$_SESSION['sess_var'].'<br />'; session_destroy(); ?>