Translate

Saturday, February 8, 2014

How to print an array using PHP


 In this post, we will learn the different ways to print an array using PHP.

1- To print an array, we can use a PHP function called print_r(),which receive an array as parameter::
´
Example: we are going to create our unidimensional array

    

    $aArray = array('lunes','martes','miercoles','jueves','viernes');
 


Now, we will pass the array to the print_r function:

   
     print_r($aArray );
 

This is the output:

     Array ( [0] => lunes [1] => martes [2] => miercoles [3] => jueves [4] => viernes )




2- the second way is using the php  function var_dump()

Example: we are going to create our unidimensional array

  
$aArray = array('lunes','martes','miercoles','jueves','viernes');
 


Now, we will pass the array to the var_dump function:
  

var_dump($aArray );
 


This is the output:

      array(5) { [0]=> string(5) "lunes" [1]=> string(6) "martes" [2]=> string(9) "miercoles"   [3]=> string(6) "jueves" [4]=> string(7) "viernes" }





3-
3- Finally, we have a combination of HTML and PHP, that will help us to see the array  more  tidy:

This is the code:


<?php

   
  
 $aArray = array('lunes','martes','miercoles','jueves','viernes');
 
  ?><pre><? print_r($aArray );?></pre><?  
?>

 This is the output:


Array
(
    [0] => lunes
    [1] => martes
    [2] => miercoles
    [3] => jueves
    [4] => viernes
)


No comments:

Post a Comment