看到代碼我的意見。您沒有將$i
迭代器設置爲有效的PHP變量,所以FYI:所有PHP變量的前綴必須爲$
。
<?php
// You declare your functions typically in the global scope, not
// within a for or any other loop.
// NOTE: $name is a required function parameter in this function.
function places($name, $location="Minneapolis", $lodging="Mom's house") {
return "$name enjoys going to $location and staying at $lodging while on vacation.";
}
// Note, I've got $people setup to have arrays that can be passed
// containing a "name, city, hotel" syntax. This is equivalent to
// $people[loop index][0] ~ $people[loop index][name]
// $people[loop index][1] ~ $people[loop index][city]
// $people[loop index][2] ~ $people[loop index][hotel]
$people = array(
array("James", "Brooklyn", "Granada Inn"),
array("Betsy", "Memphis", "Tennessee Hotel"),
array("Andrew", "San Francisco", "101 Hotel"),
array("Marvin", "San Diego", "Oceanview Beach Resort"),
array("Sara", "Orlando", "Disney World"),
array("Alicia", "Hilton Head", "Vincent Inn")
);
// Cache the count of the $names array members
$c_people = count($people);
// Loop and echo.
for ($i = 0; $i < $c_people; $i++) {
echo places($people[$i][0], $people[$i][1], $people[$i][2]) . "\n";
}
?>
http://codepad.org/QHh83cKz
產出
James enjoys going to Brooklyn and staying at Granada Inn while on vacation.
Betsy enjoys going to Memphis and staying at Tennessee Hotel while on vacation.
Andrew enjoys going to San Francisco and staying at 101 Hotel while on vacation.
Marvin enjoys going to San Diego and staying at Oceanview Beach Resort while on vacation.
Sara enjoys going to Orlando and staying at Disney World while on vacation.
Alicia enjoys going to Hilton Head and staying at Vincent Inn while on vacation.
首先,我建議你不要在函數中使用'echo';相反,返回一個字符串或創建的字符串數組。 –