SlideShare ist ein Scribd-Unternehmen logo
1 von 8
Downloaden Sie, um offline zu lesen
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”




                                               1
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




                                             2
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;




                                                         3
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
               )
          );




                                                                      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>';
    }




                                                                                       5
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();




                                                       6
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>




                                                     7
Session Control (Example

⇨   End session
<?php

 session_start();

 echo 'The content of $_SESSION['sess_var'] is '
    .$_SESSION['sess_var'].'<br />';

 session_destroy();
?>




                                                     8

Weitere ähnliche Inhalte

Was ist angesagt?

Things I Believe Now That I'm Old
Things I Believe Now That I'm OldThings I Believe Now That I'm Old
Things I Believe Now That I'm OldRoss Tuck
 
Client-side Storage 
Client-side Storage Client-side Storage 
Client-side Storage Tobias Wolf
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8PrinceGuru MS
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionIan Barber
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data ObjectsWez Furlong
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP GeneratorsMark Baker
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersIan Barber
 
Pemrograman Web 8 - MySQL
Pemrograman Web 8 - MySQLPemrograman Web 8 - MySQL
Pemrograman Web 8 - MySQLNur Fadli Utomo
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretssmueller_sandsmedia
 
TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0Henrique Moody
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHPmarkstory
 
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsRoss Tuck
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix itRafael Dohms
 

Was ist angesagt? (19)

Things I Believe Now That I'm Old
Things I Believe Now That I'm OldThings I Believe Now That I'm Old
Things I Believe Now That I'm Old
 
Client-side Storage 
Client-side Storage Client-side Storage 
Client-side Storage 
 
Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8Corephpcomponentpresentation 1211425966721657-8
Corephpcomponentpresentation 1211425966721657-8
 
Debugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 VersionDebugging: Rules And Tools - PHPTek 11 Version
Debugging: Rules And Tools - PHPTek 11 Version
 
PHP Data Objects
PHP Data ObjectsPHP Data Objects
PHP Data Objects
 
Electrify your code with PHP Generators
Electrify your code with PHP GeneratorsElectrify your code with PHP Generators
Electrify your code with PHP Generators
 
Teaching Your Machine To Find Fraudsters
Teaching Your Machine To Find FraudstersTeaching Your Machine To Find Fraudsters
Teaching Your Machine To Find Fraudsters
 
Smelling your code
Smelling your codeSmelling your code
Smelling your code
 
Pemrograman Web 8 - MySQL
Pemrograman Web 8 - MySQLPemrograman Web 8 - MySQL
Pemrograman Web 8 - MySQL
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0TestFest - Respect\Validation 1.0
TestFest - Respect\Validation 1.0
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Table through php
Table through phpTable through php
Table through php
 
Future of HTTP in CakePHP
Future of HTTP in CakePHPFuture of HTTP in CakePHP
Future of HTTP in CakePHP
 
Models and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and HobgoblinsModels and Service Layers, Hemoglobin and Hobgoblins
Models and Service Layers, Hemoglobin and Hobgoblins
 
Agile database access with CakePHP 3
Agile database access with CakePHP 3Agile database access with CakePHP 3
Agile database access with CakePHP 3
 
php plus mysql
php plus mysqlphp plus mysql
php plus mysql
 
Unfiltered Unveiled
Unfiltered UnveiledUnfiltered Unveiled
Unfiltered Unveiled
 
You code sucks, let's fix it
You code sucks, let's fix itYou code sucks, let's fix it
You code sucks, let's fix it
 

Ähnlich wie Php tutorial handout

php tutorial.ppt
php tutorial.pptphp tutorial.ppt
php tutorial.pptPohuiPank
 
php tutorial.ppt
php tutorial.pptphp tutorial.ppt
php tutorial.pptElieNGOMSEU
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐいHisateru Tanaka
 
PHP tips and tricks
PHP tips and tricks PHP tips and tricks
PHP tips and tricks Damien Seguy
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Jeff Carouth
 
symfony on action - WebTech 207
symfony on action - WebTech 207symfony on action - WebTech 207
symfony on action - WebTech 207patter
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overviewjsmith92
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128PrinceGuru MS
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPBradley Holt
 
R57shell
R57shellR57shell
R57shellady36
 
Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)Fabien Potencier
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitsmueller_sandsmedia
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownpartsBastian Feder
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2markstory
 

Ähnlich wie Php tutorial handout (20)

php tutorial.ppt
php tutorial.pptphp tutorial.ppt
php tutorial.ppt
 
php tutorial.ppt
php tutorial.pptphp tutorial.ppt
php tutorial.ppt
 
関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい関西PHP勉強会 php5.4つまみぐい
関西PHP勉強会 php5.4つまみぐい
 
PHP tips and tricks
PHP tips and tricks PHP tips and tricks
PHP tips and tricks
 
CakePHP workshop
CakePHP workshopCakePHP workshop
CakePHP workshop
 
PHP PPT FILE
PHP PPT FILEPHP PPT FILE
PHP PPT FILE
 
Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4Can't Miss Features of PHP 5.3 and 5.4
Can't Miss Features of PHP 5.3 and 5.4
 
PHPSpec BDD for PHP
PHPSpec BDD for PHPPHPSpec BDD for PHP
PHPSpec BDD for PHP
 
symfony on action - WebTech 207
symfony on action - WebTech 207symfony on action - WebTech 207
symfony on action - WebTech 207
 
Blog Hacks 2011
Blog Hacks 2011Blog Hacks 2011
Blog Hacks 2011
 
PHP 5.3 Overview
PHP 5.3 OverviewPHP 5.3 Overview
PHP 5.3 Overview
 
Php tips-and-tricks4128
Php tips-and-tricks4128Php tips-and-tricks4128
Php tips-and-tricks4128
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
R57shell
R57shellR57shell
R57shell
 
PHP 5.4
PHP 5.4PHP 5.4
PHP 5.4
 
Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)Beyond symfony 1.2 (Symfony Camp 2008)
Beyond symfony 1.2 (Symfony Camp 2008)
 
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnitinternational PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
international PHP2011_Bastian Feder_The most unknown Parts of PHPUnit
 
PHP pod mikroskopom
PHP pod mikroskopomPHP pod mikroskopom
PHP pod mikroskopom
 
Php unit the-mostunknownparts
Php unit the-mostunknownpartsPhp unit the-mostunknownparts
Php unit the-mostunknownparts
 
Ch ch-changes cake php2
Ch ch-changes cake php2Ch ch-changes cake php2
Ch ch-changes cake php2
 

Php tutorial handout

  • 1. 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” 1
  • 2. 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 2
  • 3. 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; 3
  • 4. 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 ) ); 4
  • 5. 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>'; } 5
  • 6. 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(); 6
  • 7. 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> 7
  • 8. Session Control (Example ⇨ End session <?php session_start(); echo 'The content of $_SESSION['sess_var'] is ' .$_SESSION['sess_var'].'<br />'; session_destroy(); ?> 8