+ All Categories
Home > Documents > PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and...

PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and...

Date post: 17-Jun-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
32
Fundamentals of Web Development Randy Connolly and Ricardo Hoar © 2017 Pearson http://www.funwebdev.com PHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only
Transcript
Page 1: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Fundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar© 2017 Pearson

http://www.funwebdev.com

PHP Arrays and Superglobals

Chapter 12 Sections 1 & 2 only

Page 2: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Arrays

An array is a data structure that

• Collects a number of related elements together in a single variable.

• Allows the set to be iterated

• Allows access of any element

Since PHP implements an array as a dynamic structure, handling is different than most languages:

• Add to the array

• Remove from the array

Background

Page 3: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Arrays

In PHP an array is actually an ordered map, which associates each value in the array with a key.

Key Value

To access values in an array you refer to their key using the square bracket notation.

echo "Value at index 1 is ". $days[1];

Page 4: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Arrays

Array keys are the means by which you refer to single elements in the array.

In most programming languages array keys are limited to integers, start at 0, and go up by 1.

In PHP, array keys may be either integers or strings and need not be sequential.

• Don’t mix key types i.e. “1” vs 1

• If you don’t explicitly define them they are 0,1,…

Keys

Page 5: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Arrays

Array values, unlike keys, are not restricted to integers and strings.

They can be any object, type, or primitive supported in PHP.

You can even have objects of your own types, so long as the keys in the array are integers and strings.

Values

Page 6: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Arrays

The following declares an empty array named days:

$days = array();

You can also initialize it with a comma-delimited list of values inside the ( ) braces using either of two following syntaxes:

$days = array("Mon","Tue","Wed","Thu","Fri");

or

$days = ["Mon","Tue","Wed","Thu","Fri"];

Defining an array

Page 7: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

ArraysYou can also declare each subsequent element in the array individually:

$days = array();

$days[0] = "Mon"; //set 0th key’s value to “Mon”

$days[1] = "Tue";

// also alternate approach

$days = array();

$days[] = "Mon"; //set the next sequential value to “Mon”

$days[] = "Tue";

Defining an array

Page 8: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Keys and Values

In PHP, you are also able to explicitly define the keys in addition to the values by using => as an assignment operator:

Page 9: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Super ExplicitArray declaration with string keys, integer values

In PHP, you can use keys other than the classic 0, 1, 2, . . . , n to define the indexes of an array. This type of array is called an Associative Array.

Page 10: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Multidimensional Arrays

$month = array(

array("Mon","Tue","Wed","Thu","Fri"),

array("Tue","Wed","Thu","Fri", "Sat"),

array("Wed","Thu","Fri", "Sat", "Sun"),

array("Thu", "Fri", "Sat", "Sun", "Mon")

);

echo $month[0][3]; // outputs Thu

echo $month[2][0]; // outputs Wed

Creation

Remember to count starting with 0

Page 11: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Multidimensional Arrays$cart = array();

$cart[] = array("id" => 37, "title" => "Burial at Ornans", "quantity" => 1);

$cart[] = array("id" => 345, "title" => "The Death of Marat", "quantity" => 1);

$cart[] = array("id" => 63, "title" => "Starry Night", "quantity" => 1);

Another example

Page 12: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Iterating through an array

Page 13: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Iterating through an arrayForeach loop is pretty nice

The challenge of using the classic loop structures is that when you have nonsequential integer keys (i.e., an associative array), you can’t write a simple loop that uses the $i++ construct. To address the dynamic nature of such arrays, you have to use iterators to move through such an array.

Page 14: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Adding to an arrayTo an array

An element can be added to an array simply by using a key/index that hasn’t been used:

$days[5] = "Sat";

A new element can be added to the end of any array:

$days[ ] = "Sun";

Page 15: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Adding to an arrayAnd quickly printing

PHP is more than happy to let you “skip” an index:

$days = array("Mon","Tue","Wed","Thu","Fri");

$days[7] = "Sat";

The print_r() function is often used for debugging to print out an array's key/value pairs:

print_r($days);

Array ([0] => Mon [1] => Tue [2] => Wed [3] => Thu [4] => Fri [7] => Sat)’

If we try referencing $days[6], it will return a NULL value

Page 16: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Deleting from an array

You can explicitly delete array elements using the unset() function

The array_values() function re-indexes the array numerically

Page 17: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Checking for a valueSince array keys need not be sequential, and need not be integers, you may run into a scenario where you want to check if a value has been set for a particular key.

To check if a value exists for a key, you can therefore use the isset() function, which returns true if a value has been set, and false otherwise

Page 18: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Arrays

PHP uses special predefined associative arrays called superglobal variables that allow the programmer to easily access HTTP headers, query string parameters, and other commonly needed information

Superglobal Arrays

Page 19: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Arrays

• $GLOBALS Array for storing data that needs superglobal scope

• $_COOKIES Array of cookie data passed to page via HTTP request

• $_ENV Array of server environment data

• $_FILES Array of file items uploaded to the server

• $_GET Array of query string data passed to the server via the URL

• $_POST Array of query string data passed to the server via the HTTP header

• $_REQUEST Array containing the contents of $_GET, $_POST, and $_COOKIES

• $_SESSION Array that contains session data

• $_SERVER Array containing information about the request and the server

Superglobal Arrays

Page 20: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

$_GET and $_POST Superglobal Arrays Relating sent query string elements in PHP

Page 21: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

$_GET and $_POST Superglobal Arrays Relating sent query string elements in PHP (POST)

Page 22: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

$_GET and $_POST Superglobal Arrays Note URL encoding and Decoding

Page 23: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

$_GET and $_POST Superglobal Arrays Form display and processing on same page

Page 24: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

$_GET and $_POST Superglobal Arrays

Use the isset() function in PHP to see if there is any value set for a particular expected key

if ($_SERVER["REQUEST_METHOD"] == "POST") {

if ( isset($_POST["uname"]) && isset($_POST["pass"]) ) {

// handle the posted data.

Determining If Any Data Sent

Page 25: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

$_GET and $_POST Superglobal Arrays

Monday <input type="checkbox" name="day[]" value="Monday">

Tuesday <input type="checkbox" name="day[]" value="Tuesday">

Accessing Form Array Data

<?phpecho "You submitted " . count($_GET['day']) . "values";foreach ($_GET['day'] as $d) {

echo $d . " <br>";}

?>

Page 26: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Query StringsConsider a page that contains a list of links.Each of the destination pages looks similar…

Page 27: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Query Strings

A query string appended by the PHP script appended to a URL is a way to pass data from one page to another without a user-submitted form.

Page 28: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Query Strings

Recall that when the method="get" attribute is used in a form, the user data is sent as a query string:

http://someserver.com/process-form-data.php?uname=Fred&submit=Go%21

And can be retrieved using the $_GET superglobal array:

$name = $_GET['uname'];

The same is true for any data sent in a query string…

Page 29: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

$_GET and $_POST Superglobal Arrays

$book_isbn = $_GET['isbn'];

Page 30: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

$_GET and $_POST Superglobal Arrays Sanitizing Query Strings

Just because you are expecting a proper query string, it doesn’t mean that you are going to get one. Your program must be able to handle:

• a query string parameter that doesn’t exist.

• a query string parameter that doesn’t contain a value.

• a query string parameter value that isn’t the correct type or is out of acceptable range.

• a value that is required for a database lookup, but the provided value doesn’t exist in the database table.

Page 31: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

Important Note

• The previous discussion does not present "safe" code.

• This is merely an introduction.

• The proper way to handle data safely will becovered in upcoming chapters.

Page 32: PHP Arrays and Superglobalspeople.uncw.edu/mferner/CIT410/Slides/FWD/Ch12.pdfPHP Arrays and Superglobals Chapter 12 Sections 1 & 2 only. ... In PHP, you are also able to explicitly

Fundamentals of Web Development - 2nd Ed.Randy Connolly and Ricardo Hoar

END OF 12.2


Recommended