Technology Programming

PHP Output Array to HTML

    Array Creation

    • PHP scripts can create arrays to hold data elements. The following example PHP code demonstrates creating an array variable and adding a series of elements to it at the same time:

      $people=array("Jim", "Barbara", "Sam");

      The array structure contains three elements, each a text string indicating a person's name. The PHP script can now process the array, including accessing the elements within it, iterating through it and optionally writing out information about it in HTML markup.

    HTML Output

    • PHP scripts can output information to the user's browser in a number of ways. The "print_r" function outputs information about a program variable, formatted in a way that makes it readable to humans. The following example code demonstrates writing the array using this technique:

      print_r($people);

      This method is generally used for testing and development, but for user output, the echo command provides the ability to write HTML structures and other content. The following code demonstrates use of the echo function to write the value of a variable within an HTML element:

      $some_value = "hello";

      echo "<p>".$some_value."</p>";

    Individual Elements

    • If a Web page needs to output the value of a single array element in HTML, the following syntax provides this ability:

      echo "<div>".$people[2]."</div>";

      This code outputs the value of the element at position two within the array. The position index could also exist as a variable as follows:

      $posn = 2;

      echo "<div>".$people[$posn]."</div>";

      This code uses an HTML "div" element, with the opening and closing tags appearing before and after the array element value. You can include any HTML markup you need as part of an echo statement within a PHP script.

    Entire Array

    • To output each item within a PHP array using tailored markup code, PHP scripts use loops. The following example code demonstrates a "for each" loop, iterating through the array and outputting each element within an HTML paragraph element along with some other text:

      echo "People:<br/>";

      foreach ($people as $value) {

      echo "<p>".$value."</p>";

      }

      The "for each" loop carries out the processing specified for each element in the array indicated. The code refers to each element in turn using the "value" variable, which represents the element at the current array position on each iteration, moving along until it has exhausted the array.

SHARE
RELATED POSTS on "Technology"
WordPress - How to Set up a New Theme to WordPress 3.
WordPress - How to Set up a New Theme to WordPress 3.
Solution of Creative Web Design
Solution of Creative Web Design
The three disciplines of User Experience
The three disciplines of User Experience
Web Design Sheffield Options For Professional Enterprises
Web Design Sheffield Options For Professional Enterprises
Do you have what it takes?
Do you have what it takes?
Segway Cost
Segway Cost
Microsoft Access 2010: What's Coming with Office 2010?
Microsoft Access 2010: What's Coming with Office 2010?
Companies of Web Development in Ireland Provide Designs that Work
Companies of Web Development in Ireland Provide Designs that Work
Penguin Update to Put Red-Flags on Negative SEO
Penguin Update to Put Red-Flags on Negative SEO
Innovative web 2 design templates can make your business famous quickly
Innovative web 2 design templates can make your business famous quickly
Building A Search Engine Friendly Website
Building A Search Engine Friendly Website
Exceptional Advice To Build Up Your Internet Marketing
Exceptional Advice To Build Up Your Internet Marketing
The Benefits of Selecting The Right Hosting Company
The Benefits of Selecting The Right Hosting Company
Is There a Methodology for Making Successful Logos
Is There a Methodology for Making Successful Logos
Benefits of Ruby On Rails Development
Benefits of Ruby On Rails Development
The Power of Colour and Shapes in Your Infant's Life.
The Power of Colour and Shapes in Your Infant's Life.
Advantages of Hiring PSD To HTML Service Providers
Advantages of Hiring PSD To HTML Service Providers
How to Make Responsive Web Design Attractive?
How to Make Responsive Web Design Attractive?
Converting PSD to Responsive HTML
Converting PSD to Responsive HTML
Just a few realy really hints and tips when it comes to website design but look for.
Just a few realy really hints and tips when it comes to website design but look for.

Leave Your Reply

*