PHP : Two simple memory allocation in an Array


Hello everyone, today I would like to show you two different ways to display memory allocation of array elements. First, way is to use "var_dump" and second one is to use "print_r" method to print your memory map of array elements with their indexes. Now, lets get started!!

Using "var_dump"

<?php
$months=array('jan','feb','mar','apr');
var_dump($months);
?>

If you use var_dump method it will print index no. of an array plus total char in a string of an array element. Your output should be like this

Output -

array(4){[0]=>string(3) "jan"

    [1]=>string(3) "feb"

    [2]=>string(3) "mar"

    [3]=>string(3) "apr" }

Using "print_r"

<?php
$months=array('jan','feb','mar','apr');
print_r($months);
?>

On the other hand if you use print_r method it will print index no. of an array with its associating array element. It's looks much easy and simple as compared with var_dump menthod and personally I like print_r much but you can use both var_dump and print_r depending upon your choice.Your output should be like this

Output -

Array

( [0]=>jan

  [1]=>feb

  [2]=>mar

  [3]=>apr

)

If you like this post please do share, give your views, feedback and suggestions and mention us in comments. Thank You!!

SHARE

Raunak Hajela

Hi. I’m CEO/Founder of Kodestat. I’m Student, Geek, Web Designer, Developer, Blog Enthusiast and Player. Inspired to make things looks better. I like playing with codes plus I am marvel fan :-P You can check my design portfolio here. For consulation fell free to drop an email at raunakhajela@gmail.com.

  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment