0

Hi, i have a question about php functions when using arrays. I have the following task to do:

1.Create a Php array $months which contains the months of the year, “January”, “February”, “March”, … “December”.
2.Create a Php function called echoArray() which accepts an array as a parameter and uses a for loop to output the following:
Month 1 is January
Month 2 is February
Month 3 is March

Month 12 is December

In the following piece of code i have other php code for another task and it does work. So the following code also shows what i have put in for the above task:

<?php     
            $theFirst=$_POST['firstName'];
            $theLast=$_POST['lastName'];
            $theAge=$_POST['age'];
            $theAddress=$_POST['addressLine1'];
            $theTown=$_POST['town'];

            $months=array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

        function echoArray($theArray)
         {

         $size=count($theArray);

         for ($i=0; $i<=$size; $i++)
        echo "month number " . $i . " is :" .$months[$i] . "<br />";
         }

         echo "Welcome " .$theFirst . " " . $theLast . ". " . " I see you are " . $theAge . " years old and live at " . $theAddress . " , " . $theTown;


        ?>

I'm not sure if I've done the code right for the array and function as when i load the page onto the internet the code to display the months doesn't appear. However the code to welcome the user does. Can anyone explain what's wrong with the code and what i have to do make it work right. Thank you in advance.

2
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by phorce
0

Hello,

First of all, I wouldn't put the function there. Make sure that the function is above all of the main code, so like this:

<?php
   function echoArray($theArray)
   {
       $size=count($theArray);
       for ($i=0; $i<=$size; $i++)
       echo "month number " . $i . " is :" .$theArray[$i] . "<br />";
    }

    // rest of PHP code
    // rest of PHP code
?>

(There was a problem with your code inside this function, you were trying to output $months but it does not exist, instead, you need to output $theArray).

The next step is to actually display the months (or in this case, execute the function).

 <?php
       function echoArray($theArray)
       {
           $size=count($theArray);
           for ($i=0; $i<=$size; $i++)
           echo "month number " . $i . " is :" .$theArray[$i] . "<br />";
        }
        $months=array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
        echo echoArray($months);
    ?>

This will now display the months. I don't know how your application works, but, you should be able to adapt these changes inside your code.

I hope this helps you, feel free to ask for further help :) - Phil

Edited by phorce

0

No problem :)

Please mark this thread as solved and give rep if you think someone helped!

Goodluck with your application

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.