Results 1 to 4 of 4

Thread: PHP Program

  1. #1
    Join Date
    Sep 2013
    Location
    Schaumburg, IL, U.S.A
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default PHP Program

    What will be the output of following script?

    PHP Code:
    $array = array (1235813213455);
    $sum 0;
    for (
    $i 0$i 5$i++) {
    $sum += $array[$array[$i]];
    }
    echo 
    $sum;
    ?> 
    Last edited by keyboard; 10-07-2013 at 11:45 AM. Reason: Format: Php Tags [php][/php]

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,957
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    Run it and see. My guess is an error, unless PHP type converts the extra array that's made here:

    $sum += $array[$array[$i]];
    back into a number.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Mar 2011
    Location
    N 11 19' 0.0012 E 142 15' 0
    Posts
    1,519
    Thanks
    41
    Thanked 89 Times in 88 Posts
    Blog Entries
    3

    Default

    Program Output:
    78

    Out of curiosity, is there any reason you can't run this script on your own? (read below)

    Edit -
    Right - derp
    Last edited by keyboard; 10-07-2013 at 12:24 PM.

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    28,957
    Thanks
    43
    Thanked 3,195 Times in 3,157 Posts
    Blog Entries
    12

    Default

    Oh, it could be a trick question though. Look at the code in the original post, it has no opening <? or <?php in it. If run like that, it will output:

    $array = array (1, 2, 3, 5, 8, 13, 21, 34, 55);
    $sum = 0;
    for ($i = 0; $i < 5; $i++) {
    $sum += $array[$array[$i]];
    }
    echo $sum;
    ?>

    As this code has now been run and the answer is definitive, I'm closing this thread.

    And looking at it again, I see that there was never any need for type conversion. it's array[array[$i]], not array(array[$i]).

    In the future, please don't post questions you can easily answer by simply running the code.

    If you have nowhere to run the code, you shouldn't really be participating in this section of the forum.

    If you need help setting up a local PHP environment for testing, open a new thread on that, and we will be happy to help you.
    Last edited by jscheuer1; 10-07-2013 at 12:19 PM. Reason: saw "trick", and lack of type conversion
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. Help With This Program Please :)
    By Tseng in forum JavaScript
    Replies: 13
    Last Post: 11-15-2008, 04:14 PM
  2. Trying to run the program
    By krisrimat in forum MySQL and other databases
    Replies: 0
    Last Post: 07-14-2008, 09:11 PM
  3. need help on a program
    By noobzor in forum JavaScript
    Replies: 0
    Last Post: 10-31-2007, 02:52 AM
  4. Replies: 1
    Last Post: 09-12-2007, 06:29 AM
  5. I need help with a program
    By Davebold370 in forum Other
    Replies: 6
    Last Post: 11-21-2005, 12:20 AM

Bookmarks

Posting Permissions

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