SlideShare ist ein Scribd-Unternehmen logo
1 von 29
PHP Arrays
Dr. Charles Severance
www.wa4e.com
http://www.wa4e.com/code/arrays
http://www.wa4e.com/code/arrays.zip
PHP Arrays Rock!
• Better than Python Dictionaries
• Better than Java Hash Maps
• PHP Arrays have all the benefits of Python Dictionaries
but they can also maintain the order of the items in the
array
http://en.wikipedia.org/wiki/Associative_array
Associative Arrays
• Can be key => value or simply indexed by numbers
• Ignore two-dimensional arrays for now...
http://en.wikipedia.org/wiki/Associative_array
Integer Indices
<?php
$stuff = array("Hi", "There");
echo $stuff[1], "n";
?>
There
Key / Value
<?php
$stuff = array("name" => "Chuck",
"course" => "PHPIntro");
echo $stuff["course"], "n";
?>
PHPIntro
Dumping an Array
• The function print_r() shows PHP data - it is good for
debugging
<?php
$stuff = array("name" => "Chuck",
"course" => "PHPIntro");
echo("<pre>n");print_r($something);echo("n</pre>n");
?>
Array(
[name] => Chuck
[course] => PHPIntro
)
Building up an Array
• You can allocate a new item in the array and append a
value at the same time using empty square braces [ ]
on the right hand side of an assignment statement
$va = array();
$va[] = "Hello";
$va[] = "World";
print_r($va);
Array(
[0] => Hello
[1] => World
)
Building up an Array
• You can also add new items in an array using a key as
well
$za = array();
$za["name"] = "Chuck";
$za["course"] = "PHPIntro";
print_r($za);
Array(
[name] => Chuck
[course] => PHPIntro
)
Looping Through an Array
<?php
$stuff = array("name" => "Chuck",
"course" => "PHPIntro");
foreach($stuff as $k => $v ) {
echo "Key=",$k," Val=",$v,"n";
}
?>
Key=name Val=Chuck
Key=course Val=PHPIntro
Arrays of
Arrays
$products = array(
'paper' => array(
'copier' => "Copier & Multipurpose",
'inkjet' => "Inkjet Printer",
'laser' => "Laser Printer",
'photo' => "Photographic Paper"),
'pens' => array(
'ball' => "Ball Point",
'hilite' => "Highlighters",
'marker' => "Markers"),
'misc' => array(
'tape' => "Sticky Tape",
'glue' => "Adhesives",
'clips' => "Paperclips")
);
The elements of an array
can be many things other
than a string or integer.
You can even have
objects or other arrays.
echo $products["pens"]["marker"];
Markers
Array Functions
Array Functions
• count($ar) - How many elements in an array
• is_array($ar) - Returns TRUE if a variable is an array
• isset($ar['key']) - Returns TRUE if key is set in the array
• sort($ar) - Sorts the array values (loses key)
• ksort($ar) - Sorts the array by key
• asort($ar) - Sorts array by value, keeping key association
• shuffle($ar) - Shuffles the array into random order
$za = array();
$za["name"] = "Chuck";
$za["course"] = "PHPIntro";
print "Count: " . count($za) . "n";
if ( is_array($za) ) {
echo '$za Is an array' . "n";
} else {
echo '$za Is not an array' . "n";
}
echo isset($za['name']) ? "name is setn" : "name is not setn";
echo isset($za['addr']) ? "addr is setn" : "addr is not setn";
Count: 2
$za Is an array
name is set
addr is not set
$za = array();
$za["name"] = "Chuck";
$za["course"] = "PHPIntro";
$za["topic"] = "PHP";
print_r($za);
sort($za);
print_r($za);
Array(
[name] => Chuck
[course] => PHPIntro
[topic] => PHP
)
Array(
[0] => Chuck
[1] => PHP
[2] => PHPIntro
)
$za = array();
$za["name"] = "Chuck";
$za["course"] = "PHPIntro";
$za["topic"] = "PHP";
print_r($za);
ksort($za);
print_r($za);
asort($za);
print_r($za);
Array(
[name] => Chuck
[course] => PHPIntro
[topic] => PHP
)
Array(
[course] => PHPIntro
[name] => Chuck
[topic] => PHP
)
Array(
[name] => Chuck
[topic] => PHP
[course] => PHPIntro
)
Arrays and Strings
$inp = "This is a sentence with seven words";
$temp = explode(' ', $inp);
print_r($temp);
Array(
[0] => This
[1] => is
[2] => a
[3] => sentence
[4] => with
[5] => seven
[6] => words
)
HTTP Parameters and Arrays
PHP Global Variables
• Part of the goal of PHP is to make interacting with
HTTP and HTML as easy as possible
• PHP processes the incoming HTTP Request based on
the protocol specifications and drops the data into
various super global variables (usually arrays)
http://www.wa4e.com/code/arrays/get-01.php
Web Server Database Server
Time
Apache
PHP
MySql
Browser
JavaScri
pt
D
O
M
php
code
static
files
RRC/HTTP SQL
Parse
Respons
e
Parse
Reques
t
ind.ph
p
$_GET
Validation
• Making sure all user data is present and the correct
format before proceeding
• Non empty strlen($var) > 0
• A number is_numeric($var)
• An email address strpos($var, '@') > 0
• ....
http://www.wa4e.com/code/arrays/guess.php?guess=7
http://www.wa4e.com/code/arrays/guess.php?guess=200
Summary
• PHP arrays are a very powerful associative array as they
can be indexed by integers like a list, or use keys to look
values up like a hash map or dictionary
• PHP arrays maintain order and there are many options for
sorting
• We can use explode() to split a string into an array of
strings
• HTTP Information is pre-processed and placed in super
global arrays
Acknowledgements / Contributions
These slides are Copyright 2010- Charles R. Severance
(www.dr-chuck.com) as part of www.wa4e.com and made
available under a Creative Commons Attribution 4.0 License.
Please maintain this last slide in all copies of the document
to comply with the attribution requirements of the license. If
you make a change, feel free to add your name and
organization to the list of contributors on this page as you
republish the materials.
Initial Development: Charles Severance, University of
Michigan School of Information
Insert new Contributors and Translators here including
names and dates
Continue new Contributors and Translators here

Weitere ähnliche Inhalte

Ähnlich wie PHP Arrays: Powerful, Order-Maintaining Data Structures

Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perlworr1244
 
Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Ajay Khatri
 
php fundamental
php fundamentalphp fundamental
php fundamentalzalatarunk
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of phppooja bhandari
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshotsrichambra
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Dhivyaa C.R
 
Php basics
Php basicsPhp basics
Php basicshamfu
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Michelangelo van Dam
 
Scripting3
Scripting3Scripting3
Scripting3Nao Dara
 
New SPL Features in PHP 5.3
New SPL Features in PHP 5.3New SPL Features in PHP 5.3
New SPL Features in PHP 5.3Matthew Turland
 
Php Introduction nikul
Php Introduction nikulPhp Introduction nikul
Php Introduction nikulNikul Shah
 
Substitution Cipher
Substitution CipherSubstitution Cipher
Substitution CipherAgung Julisman
 

Ähnlich wie PHP Arrays: Powerful, Order-Maintaining Data Structures (20)

05php
05php05php
05php
 
Intro to Perl and Bioperl
Intro to Perl and BioperlIntro to Perl and Bioperl
Intro to Perl and Bioperl
 
Introduction to Perl
Introduction to PerlIntroduction to Perl
Introduction to Perl
 
Introduction to PHP Lecture 1
Introduction to PHP Lecture 1Introduction to PHP Lecture 1
Introduction to PHP Lecture 1
 
php fundamental
php fundamentalphp fundamental
php fundamental
 
Php introduction with history of php
Php introduction with history of phpPhp introduction with history of php
Php introduction with history of php
 
php
phpphp
php
 
PHP and MySQL with snapshots
 PHP and MySQL with snapshots PHP and MySQL with snapshots
PHP and MySQL with snapshots
 
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
Regular expressions, Session and Cookies by Dr.C.R.Dhivyaa Kongu Engineering ...
 
UNIT IV (4).pptx
UNIT IV (4).pptxUNIT IV (4).pptx
UNIT IV (4).pptx
 
Php basics
Php basicsPhp basics
Php basics
 
Php
PhpPhp
Php
 
Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010Php Crash Course - Macq Electronique 2010
Php Crash Course - Macq Electronique 2010
 
PHP for hacks
PHP for hacksPHP for hacks
PHP for hacks
 
Scripting3
Scripting3Scripting3
Scripting3
 
New SPL Features in PHP 5.3
New SPL Features in PHP 5.3New SPL Features in PHP 5.3
New SPL Features in PHP 5.3
 
Introduction to Perl and BioPerl
Introduction to Perl and BioPerlIntroduction to Perl and BioPerl
Introduction to Perl and BioPerl
 
Php Introduction nikul
Php Introduction nikulPhp Introduction nikul
Php Introduction nikul
 
Substitution Cipher
Substitution CipherSubstitution Cipher
Substitution Cipher
 
Initializing arrays
Initializing arraysInitializing arrays
Initializing arrays
 

KĂźrzlich hochgeladen

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
 
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
 
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
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
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
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
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
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 

KĂźrzlich hochgeladen (20)

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
 
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
 
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
 
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
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
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...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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...
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
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...
 
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
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 

PHP Arrays: Powerful, Order-Maintaining Data Structures

  • 1. PHP Arrays Dr. Charles Severance www.wa4e.com http://www.wa4e.com/code/arrays http://www.wa4e.com/code/arrays.zip
  • 2. PHP Arrays Rock! • Better than Python Dictionaries • Better than Java Hash Maps • PHP Arrays have all the benefits of Python Dictionaries but they can also maintain the order of the items in the array http://en.wikipedia.org/wiki/Associative_array
  • 3. Associative Arrays • Can be key => value or simply indexed by numbers • Ignore two-dimensional arrays for now... http://en.wikipedia.org/wiki/Associative_array
  • 4. Integer Indices <?php $stuff = array("Hi", "There"); echo $stuff[1], "n"; ?> There
  • 5. Key / Value <?php $stuff = array("name" => "Chuck", "course" => "PHPIntro"); echo $stuff["course"], "n"; ?> PHPIntro
  • 6. Dumping an Array • The function print_r() shows PHP data - it is good for debugging <?php $stuff = array("name" => "Chuck", "course" => "PHPIntro"); echo("<pre>n");print_r($something);echo("n</pre>n"); ?> Array( [name] => Chuck [course] => PHPIntro )
  • 7. Building up an Array • You can allocate a new item in the array and append a value at the same time using empty square braces [ ] on the right hand side of an assignment statement $va = array(); $va[] = "Hello"; $va[] = "World"; print_r($va); Array( [0] => Hello [1] => World )
  • 8. Building up an Array • You can also add new items in an array using a key as well $za = array(); $za["name"] = "Chuck"; $za["course"] = "PHPIntro"; print_r($za); Array( [name] => Chuck [course] => PHPIntro )
  • 9. Looping Through an Array <?php $stuff = array("name" => "Chuck", "course" => "PHPIntro"); foreach($stuff as $k => $v ) { echo "Key=",$k," Val=",$v,"n"; } ?> Key=name Val=Chuck Key=course Val=PHPIntro
  • 10. Arrays of Arrays $products = array( 'paper' => array( 'copier' => "Copier & Multipurpose", 'inkjet' => "Inkjet Printer", 'laser' => "Laser Printer", 'photo' => "Photographic Paper"), 'pens' => array( 'ball' => "Ball Point", 'hilite' => "Highlighters", 'marker' => "Markers"), 'misc' => array( 'tape' => "Sticky Tape", 'glue' => "Adhesives", 'clips' => "Paperclips") ); The elements of an array can be many things other than a string or integer. You can even have objects or other arrays. echo $products["pens"]["marker"]; Markers
  • 12.
  • 13. Array Functions • count($ar) - How many elements in an array • is_array($ar) - Returns TRUE if a variable is an array • isset($ar['key']) - Returns TRUE if key is set in the array • sort($ar) - Sorts the array values (loses key) • ksort($ar) - Sorts the array by key • asort($ar) - Sorts array by value, keeping key association • shuffle($ar) - Shuffles the array into random order
  • 14. $za = array(); $za["name"] = "Chuck"; $za["course"] = "PHPIntro"; print "Count: " . count($za) . "n"; if ( is_array($za) ) { echo '$za Is an array' . "n"; } else { echo '$za Is not an array' . "n"; } echo isset($za['name']) ? "name is setn" : "name is not setn"; echo isset($za['addr']) ? "addr is setn" : "addr is not setn"; Count: 2 $za Is an array name is set addr is not set
  • 15. $za = array(); $za["name"] = "Chuck"; $za["course"] = "PHPIntro"; $za["topic"] = "PHP"; print_r($za); sort($za); print_r($za); Array( [name] => Chuck [course] => PHPIntro [topic] => PHP ) Array( [0] => Chuck [1] => PHP [2] => PHPIntro )
  • 16. $za = array(); $za["name"] = "Chuck"; $za["course"] = "PHPIntro"; $za["topic"] = "PHP"; print_r($za); ksort($za); print_r($za); asort($za); print_r($za); Array( [name] => Chuck [course] => PHPIntro [topic] => PHP ) Array( [course] => PHPIntro [name] => Chuck [topic] => PHP ) Array( [name] => Chuck [topic] => PHP [course] => PHPIntro )
  • 17. Arrays and Strings $inp = "This is a sentence with seven words"; $temp = explode(' ', $inp); print_r($temp); Array( [0] => This [1] => is [2] => a [3] => sentence [4] => with [5] => seven [6] => words )
  • 19. PHP Global Variables • Part of the goal of PHP is to make interacting with HTTP and HTML as easy as possible • PHP processes the incoming HTTP Request based on the protocol specifications and drops the data into various super global variables (usually arrays)
  • 21.
  • 22.
  • 23.
  • 24. Web Server Database Server Time Apache PHP MySql Browser JavaScri pt D O M php code static files RRC/HTTP SQL Parse Respons e Parse Reques t ind.ph p $_GET
  • 25. Validation • Making sure all user data is present and the correct format before proceeding • Non empty strlen($var) > 0 • A number is_numeric($var) • An email address strpos($var, '@') > 0 • ....
  • 28. Summary • PHP arrays are a very powerful associative array as they can be indexed by integers like a list, or use keys to look values up like a hash map or dictionary • PHP arrays maintain order and there are many options for sorting • We can use explode() to split a string into an array of strings • HTTP Information is pre-processed and placed in super global arrays
  • 29. Acknowledgements / Contributions These slides are Copyright 2010- Charles R. Severance (www.dr-chuck.com) as part of www.wa4e.com and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates Continue new Contributors and Translators here

Hinweis der Redaktion

  1. Note from Chuck. If you are using these materials, you can remove my name and URL from this replace it with your own, but please retain the CC-BY logo on the first page as well as retain the entire last page when you remix and republish these slides.
  2. Note from Chuck. Please retain and maintain this page as you remix and republish these materials. Please add any of your own improvements or contributions.