calling Array prints 'Array'?

Discussion in 'PHP' started by mz906, Jun 27, 2008.

  1. #1
    OK, i'm sure this is a really easy one...

    what does it mean when i call my array and it just prints 'Array' ?
     
    mz906, Jun 27, 2008 IP
  2. King Goilio

    King Goilio Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    33
    #2
    to print the array you use
    echo print_r($arrayname);
    to printing apart of the array
    echo $arrayname[0]
     
    King Goilio, Jun 27, 2008 IP
  3. sarav_dude

    sarav_dude Peon

    Messages:
    10
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    why echo and print_r ? just use print_r :)
     
    sarav_dude, Jun 27, 2008 IP
  4. King Goilio

    King Goilio Member

    Messages:
    200
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    33
    #4
    that's just the way i do it because i found it on the net but if just print_r works thanks for telling me.
     
    King Goilio, Jun 27, 2008 IP
  5. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    It means that you are treating it as a string.
     
    itnashvilleCOM, Jun 28, 2008 IP
  6. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #6
    Yes, say you have an array:

    
    <?php
    $fruit = array(apple,banana,pear);
    ?>
    
    PHP:
    There are multiple ways I can output that:
    
    <?php
    //1.
    echo $fruit; //Returns "Array"
    
    //2.
    print_r($fruit); // Returns "Array ( [0] => apple [1] => banana [2] => pear )"
    
    //3.
    echo $fruit[0]; //Returns "apple";
    echo $fruit[1]; //Returns "banana";
    echo $fruit[2]; //Returns "pear";
    
    //4.
    foreach($fruit as $fruity){
    echo "$fruity,";
    } //Returns "apple,banana,pear,"
    ?>
    
    PHP:
     
    blueparukia, Jun 28, 2008 IP
  7. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #7
    Try not to use variables within double quotes if you can avoid it, much slower than just doing echo $fruity , ',';.

    (Not going to mention while loops here, that belongs in a different place.)

    Dan
     
    Danltn, Jun 29, 2008 IP
  8. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #8
    I have never bothered with those really small things to save a few milliseconds, unless optimization is requested by the client for whatever reason. The sites I have made that are big enough to notice it, have servers that are more than capable of handling it.

    And I prefer it be easier/lessconfusing to edit for other coders
     
    blueparukia, Jun 29, 2008 IP
  9. xfreex

    xfreex Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    printf will be trend with php 5 :)

    example :
    $name = "xfreex";
    printf("%s",$name");

    %s is string name
    %d is double
    %i is integer
    %b is boolean
     
    xfreex, Jun 29, 2008 IP
  10. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #10
    $name = "xfreex";
    printf("%s",$name");

    Why? That's just wasted resources...
     
    Danltn, Jun 29, 2008 IP