Google Plus Facebook Twitter RSS YouTube

How to output a value in PHP Numeric Array

PHP numeric array accepts values through its syntax from programmers and stores each value by assigning a place as per numeric integer indexing system as displayed in below graphical designing art work which illustrates the simulation to numeric array.

Example Numeric Array

How to Output

For example, consider a numeric array and name it $class to store list of some of the great people. The list of great ones are stored in $class through a numeric array as given in below simple php program.

PHP Code

<?php

  $class = array("Johnson",
                 "Mohammad Ali",
                 "Peterson",
                 "Samuel",
                 "Velvet",
                 "Sunitha",
                 "Ganesh",
                 "Teresa");
  print_r($class);

?>

If programmers use print_r() function and it outputs the entire array in web browser as displayed below.

Output in Web Browser
Array
(
    [0] => Johnson
    [1] => Mohammad Ali
    [2] => Peterson
    [3] => Samuel
    [4] => Velvet
    [5] => Sunitha
    [6] => Ganesh
    [7] => Teresa
)

Totally 8 values are input to the $class array and it stores all eight great people names in eight different locations and those locations are given as per numeric integer indexing system. Hence, the eight key names are 0, 1, 2, 3, 4, 5, 6 and 7.

Our object here is not to output the array but output each value.

PHP Code

<?php

  $class = array("Johnson",
                 "Mohammad Ali",
                 "Peterson",
                 "Samuel",
                 "Velvet",
                 "Sunitha",
                 "Ganesh",
                 "Teresa");
  echo $class;

?>

If users use output returning functions like echo or print, both functions output only Array name and do not print array as returned by the print_r() function.

Output in Web Browser
Array

Because echo and print are string data returning functions not array data returning functions. Hence, they only printed Array name in browser. However, the data inside the array’s every key is also a string. So, the data in every key of array can be output directly using either echo or print function.

echo or print functions can only output the values when programmers specify the location of the value in array. Then only both echo and print outputting function can return the value.

PHP Code

<?php

  $class = array("Johnson",
                 "Mohammad Ali",
                 "Peterson",
                 "Samuel",
                 "Velvet",
                 "Sunitha",
                 "Ganesh",
                 "Teresa");
  echo $class[4];

?>

The location of a value in numeric array can be specified by square brackets. Inside the square brackets, users have to specify the location number which helps outputting functions to return the value exactly as displayed below.

Output in Web Browser
Velvet

Similarly, echo function returns an error message if there is no value in specified location of a numeric array.

You further learnt it by below example.

PHP Code

echo "<strong>Who can be called the mother of kindness?</strong>"."<br>";
echo $class[7];

Add above code to our actual program, it prints a question and also prints answer by getting answer which is stored as value at specified location in numeric array.

Output in Web Browser
Who can be called the mother of kindness?
Teresa

As per this php programming guidelines, php programmers output the values from a numeric array and use this concept in php programs development.

Follow Us
Google Plus Facebook Twitter RSS YouTube
Subscribe via Email
Save or Share This PHP Tutorial with Your Friends
Save or Share PHP Tutorial