SlideShare una empresa de Scribd logo
1 de 8
Descargar para leer sin conexión
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

Más contenido relacionado

Más de tutorialsruby

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 
Winter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20JavascriptWinter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20Javascripttutorialsruby
 

Más de tutorialsruby (20)

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
CSS
CSSCSS
CSS
 
CSS
CSSCSS
CSS
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
Winter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20JavascriptWinter%200405%20-%20Advanced%20Javascript
Winter%200405%20-%20Advanced%20Javascript
 

Último

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Último (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

php%20tutorial_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