How would I fix these PHP scripts?

Rind

New member
For a project I am supposed to identify what is wrong with these scripts and fix them:


The first script:


<?php // Script 7.1 - soups1.php
/* This script creates and prints out an array. */
// Address error management, if you want.

// Create the array:
$soups = array (
'Monday' => 'Clam Chowder',
'Tuesday' => 'White Chicken Chili',
'Wednesday' => 'Vegetarian'
);

// Try to print the array:
print "<p>$soups</p>";

// Print the contents of the array:
print_r ($soups);

?>


The second Script:

<?php // Script 7.2 - soups2.php
/* This script creates and prints out an array. */
// Address error management, if you want.

// Create the array:
$soups = array (
'Monday' => 'Clam Chowder',
'Tuesday' => 'White Chicken Chili',
'Wednesday' => 'Vegetarian'
);

// Count and print the current number of elements:
$count1 = count ($soups);
print "<p>The soups array originally had $count1 elements.</p>";

// Add three items to the array:
$soups['Thursday'] = 'Chicken Noodle';
$soups['Friday'] = 'Tomato';
$soups['Saturday'] = 'Cream of Broccoli';

// Count and print the number of elements again:
$count2 = count ($soups);
print "<p>After adding 3 more soups, the array now has $count2 elements.</p>";

// Print the contents of the array:
print_r ($soups);

?>

Third Script:

<?php // Script 7.3 - soups3.php
/* This script creates and prints out an array. */

// Address error management, if you want.

// Create the array:
$soups = array (
'Monday' => 'Clam Chowder',
'Tuesday' => 'White Chicken Chili',
'Wednesday' => 'Vegetarian',
'Thursday' => 'Chicken Noodle',
'Friday' => 'Tomato',
'Saturday' => 'Cream of Broccoli'
);

// Print each key and value:
foreach ($soups as $day => $soup) {
print "<p>$day: $soup</p>\n";
}

?>
 
For the first one, the obvious mistake is that you don't print the array with a simple print, it doesn't work - but print_r works, though all it does is dump the array values in an unformatted manner.

The other two do exactly what they're supposed to. I tried them out, no problems.
 
The easiest way is to run it through the php and see the output. Here are the outputs for each script:

Output of First script:
<p>Array</p>Array
(
[Monday] => Clam Chowder
[Tuesday] => White Chicken Chili
[Wednesday] => Vegetarian
)

Output of Second Script
<p>The soups array originally had 3 elements.</p><p>After adding 3 more soups, the array now has 6 elements.</p>Array
(
[Monday] => Clam Chowder
[Tuesday] => White Chicken Chili
[Wednesday] => Vegetarian
[Thursday] => Chicken Noodle
[Friday] => Tomato
[Saturday] => Cream of Broccoli
)

Output of Third Script:
<p>Monday: Clam Chowder</p>
<p>Tuesday: White Chicken Chili</p>
<p>Wednesday: Vegetarian</p>
<p>Thursday: Chicken Noodle</p>
<p>Friday: Tomato</p>
<p>Saturday: Cream of Broccoli</p>

For the first script, it is easy to see where it is wrong, you'll just get the word "Array" in your browser. For the second script, I'm not sure what he wants to be the "correct" result, but maybe it's because you usually don't want to print an array like that, instead encapsulating it with for-loop like the third script. I don't see anything wrong with the third script....
 
Back
Top