www.webdeveloper.com
Results 1 to 7 of 7

Thread: Can not use array

  1. #1
    Join Date
    Oct 2009
    Posts
    33

    Exclamation Can not use array

    I'm learning PHP by reading book but I've problem with array.
    I try similar code with book like this

    <?php
    $name[]="Gillbate";
    $name[]="alonso";
    $name[]="Hernandez";
    foreach($name, as $currentvalue){
    echo $currentvalue};
    ?>

    and this

    <?php
    $name[]="Gillbate";
    $name[]="alonso";
    $name[]="Hernandez";
    sort($name);
    print_r($name);
    ?>

    but they did not work.
    What is my problem?

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,145
    Syntax-wise, there is not error in the 2nd part. In the first part you have a comma in the foreach() arguments which should not be there, and closing "}" needs to come after the ";".

    PHP Code:
    $name[] = "Gillbate";
    $name[] = "alonso";
    $name[] = "Hernandez";
    foreach(
    $name as $currentvalue) {
       echo 
    $currentvalue;

    Cosmetically, the output will all be "scrunched" together since there is no HTML formatting applied to it.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Oct 2009
    Posts
    33
    Quote Originally Posted by NogDog View Post
    Syntax-wise, there is not error in the 2nd part. In the first part you have a comma in the foreach() arguments which should not be there, and closing "}" needs to come after the ";".

    PHP Code:
    $name[] = "Gillbate";
    $name[] = "alonso";
    $name[] = "Hernandez";
    foreach(
    $name as $currentvalue) {
       echo 
    $currentvalue;

    Cosmetically, the output will all be "scrunched" together since there is no HTML formatting applied to it.
    I did like that but both are still give me empty page.

  4. #4
    Join Date
    Oct 2009
    Posts
    25
    try this:
    PHP Code:
    <?php
    function print_pre($array) {
     echo 
    '<pre>';
     
    print_r($array);
     echo 
    '</pre>';
    }

    $name = array('Gillbate''alonso''Hernandez');
    print_pre($name);
    ?>

  5. #5
    Join Date
    Oct 2009
    Posts
    33
    Quote Originally Posted by allanice001 View Post
    try this:
    PHP Code:
    <?php
    function print_pre($array) {
     echo 
    '<pre>';
     
    print_r($array);
     echo 
    '</pre>';
    }

    $name = array('Gillbate''alonso''Hernandez');
    print_pre($name);
    ?>
    This is that out put

    '; print_r($array); echo ''; } $name = array('Gillbate', 'alonso', 'Hernandez'); print_pre($name); ?>

    Is it ok?
    Then, how to sort them?

  6. #6
    Join Date
    Oct 2009
    Posts
    33
    Quote Originally Posted by Gillbate View Post
    This is that out put

    '; print_r($array); echo ''; } $name = array('Gillbate', 'alonso', 'Hernandez'); print_pre($name); ?>

    Is it ok?
    Then, how to sort them?
    Ahhh I found my problem!
    The code above give this

    Array
    (
    [0] => Gillbate
    [1] => alonso
    [2] => Hernandez
    )

    My problem is open that file directly instead though http://localhost
    Thank you everyone!

  7. #7
    Join Date
    Oct 2009
    Posts
    25
    output should be
    Code:
    Array
    (
        [0] => Gillbate
        [1] => alonso
        [2] => Hernandez
    )
    i think the error is somwhere else on your page

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles