Reputation: 13
I have a function that passes an argument with two default values...
function places($location="Minneapolis", $lodging="Mom's house")
{
echo "enjoys going to {$location} and staying at {$lodging} while on vacation.";
}
places("St. Paul","Grandma's house");
I need to pass the function 10 times using 10 different names of people defined as a variable passed as an argument. What would the syntax be assuming the output would resemble this:
Joe enjoys going to St. Paul and staying at Grandma's house while on vacation.
Upvotes: 1
Views: 154
Reputation: 49188
See my comments in the code. You didn't make the $i
iterator a valid PHP variable, so FYI: All PHP variables must be prefixed with a $
.
<?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";
}
?>
OUTPUTS
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.
Upvotes: 0
Reputation: 248
How about something like this?
$names = explode(',', 'James ,Betsy ,Andrew ,Marvin ,Alicia ,etc... ');
foreach($names as $name)
{
echo $name, places(), '<br>';
}
Upvotes: 4
Reputation: 6402
If I am reading your question correctly then all you need is another parameter
function places($location="Minneapolis", $lodging="Mom's house", $name="Bob")
{
echo "{$name} enjoys going to {$location} and staying at {$lodging} while on vacation.";
}
places("St. Paul","Grandma's house","Joe");
Upvotes: 0
Reputation: 2912
Do you mean something like this?
function places($location="Minneapolis", $lodging="Mom's house")
{
echo "enjoys going to {$location} and staying at {$lodging} while on vacation.\n";
}
$loc = array(
array('location'=>'St. Paul1', 'lodging' => 'Grandma\'s house1'),
array('location'=>'St. Paul2', 'lodging' => 'Grandma\'s house2'),
array('location'=>'St. Paul3', 'lodging' => 'Grandma\'s house3'),
array('location'=>'St. Paul4', 'lodging' => 'Grandma\'s house4'),
array('location'=>'St. Paul5', 'lodging' => 'Grandma\'s house5'),
// etc
);
foreach($loc as $i)
{
places($i['location'], $i['lodging']);
}
Upvotes: 1