php函数语法查询

时间:2011-10-07 22:58:01

标签: php function variables

我有一个函数传递一个带有两个默认值的参数......

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

我需要使用10个不同的名称定义为作为参数传递的变量来传递函数10次。语法假设输出类似于此:

乔喜欢去圣保罗,并在度假时住在奶奶家。

4 个答案:

答案 0 :(得分:4)

这样的事情怎么样?

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

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

Demo

答案 1 :(得分:1)

你的意思是这样吗?

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

答案 2 :(得分:0)

如果我正确地阅读了您的问题,那么您需要的只是另一个参数

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

答案 3 :(得分:0)

在代码中查看我的评论。您没有使$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

<强> 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.