Skip navigation.
Home

PHP Arrays Ex. #1: Simple Array

Arrays allow you to assign multiple values to one variable. For this PHP exercise, write an array variable of weather conditions with the following values: rain, sunshine, clouds, hail, sleet, snow, wind. Using the array variable for all the weather conditions, echo the following statement to the browser:

We've seen all kinds of weather this month. At the beginning of the month, we had snow and wind. Then came sunshine with a few clouds and some rain. At least we didn't get any hail or sleet.

Don't forget to include a title for your page, both in the header and on the page itself.

 

sorting array in one variable

hello.. this is my first question so far for any other site like this. can you give me an idea how to sort value? what im doing is that when statement is true, it will add total.. so its on a loop everytime the system finds the statement is true it will come up with total. what i want is to sort the total out of that. thank you.. please help me..

A better way

In a professional situation you would ideally separate the html from the php like this:

<body>
<?php 
$arr = array("rain", "sunshine", "clouds", "hail", "sleet", "snow", "wind");
?>

<p>We've seen all kinds of weather this month. At the beginning of the month we had <?php echo $arr[5]; ?> and <?php echo $arr[6];?>. Then came <?php echo $arr[1]; ?> with a few <?php echo $arr[2]; ?> and some <?php echo $arr[0]; ?>. At least we didn't get any <?php echo $arr[3]; ?> or <?php echo $arr[4]; ?>.</p>
</body>

Hmm

Thanks for the insight. I find it interesting, however, that your example doesn't include any HTML tags at all. In a professional situation, especially striving for standards compliance, at least some <p> tags would be necessary.

In any case, either example works fine for the purpose of the exercise, which is to use the array to get at its values.