How to Have Lists in Block PHP
PHP is a programming language that is well suited to creating dynamic Web pages. A dynamic Web page is one that adapts to different calling parameter values or user actions. All Web pages are written in HTML, which is the Hypertext Markup Language. However, HTML has no programming constructs, so any Web pages are stored on a server as a PHP program and are only generated out as a full HTML page when a user requests it. Lists are a useful element in the creation of a Web page from a PHP program.
-
PHP Blocks
-
A PHP program contains a mixture of PHP and HTML. The purpose of the program is to generate a Web page and so the majority of the contents of the program is actually an HTML document. The parts of the page that will adapt are missing in the stored page. In these locations are sections of PHP code. A section of PHP code within a PHP file is called a PHP block.
List Types
-
PHP contains two types of lists. One is an argument list to a function and the other is a variable containing a sequence of elements, which is called an array. A list of arguments passed to a function follows that function's name. Functions can also pass back a list as the results of its operation; in this case, the variable set to receive the results of the function should either be an array or a list of variables.
-
List Formation
-
A list in PHP is easily formed. It is a series of elements separated by commas and contained in brackets. It is not necessary to declare the list before using it. The elements of the list do not have to be all the same data type. Each element can either be a value or a variable containing a value. Elements can be skipped by putting two commas. Thus (“blue”, , $color) would leave the middle element of the array blank. PHP contains two functions that populate arrays. One is list() and the other is array().
Array Indices
-
Each element in an array can be accessed directly by using and index. The index follows the array name and is contained in square brackets. The first element of the array has an index of 0, not 1. Thus, in the array $arr = list(“yes”, “no”, “maybe”), $arr[1] contains the value “no”. The default index is a numeric sequence. The list() function can only use this default index, but the array() function can assign a string to the index, for example: $arr = array(“Name” => “David”, “Age” => 15, “Home” => “Manchester”). If no labels are given for the index, the array() function defaults to a numeric sequence as used by the list() function.
-
References
- Photo Credit Comstock/Comstock/Getty Images