1

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.

1
  • First, I would recommend you not use an echo within your function; instead, return a string or an array of the strings created. Commented Oct 7, 2011 at 23:08

4 Answers 4

4

How about something like this?

$names = explode(',', 'James ,Betsy ,Andrew ,Marvin ,Alicia ,etc... ');

foreach($names as $name)
{
   echo $name, places(), '<br>';
}

Demo

Sign up to request clarification or add additional context in comments.

6 Comments

@hakre - Well, sort've. I would assume each name should be it's own array member, and the function arguments are not demonstrated being used from an array or any other means, for that matter.
thanks for the edits guys--it wasn't working code, just expressing a general concept here. But certainly you have improved it.
You'll typically either want to provide working code, code marked as pseudo-code, or note any special considerations in your answer. Otherwise, it can be confusing to whoever asked the question. :)
Hi Mountain Girl! Ok, your info was extremely helpful. I'm really close now however just need a bit more help on the sytanx, if you please. Here is what I have and wondering from here what I need to do to pull those name variables in and echo it out <?php $names = array(); $names = ("James", "Betsy", "Andrew", "Marvin", "Sara", "Alicia"); for (i=0; i< 10; i++) 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"); ?>
The code I originally gave you was not working code: I was just trying to show a certain logic. You need to explode the names in the array using a , delimiter (or whatever other delimiter you prefer). Use a foreach instead of a for loop. Basically...follow the changes made to my code up above.
|
1

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']);
}

Comments

0

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";
}

?>

http://codepad.org/QHh83cKz

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.

2 Comments

Hey Jared! Yes, this is great. Thanks so much. I know very little PHP and taking a couple of online classes now. I know there are always several ways to code things, and this is just what I envisioned this to operate like. Thanks again!
Make sure and mark it as the answer (by clicking the outlined checkmark next to this answer) and upvote. :)
0

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.