drq61040 2011-10-07 22:58
浏览 19
已采纳

php函数语法查询

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.

  • 写回答

4条回答 默认 最新

  • doubi4340 2011-10-07 23:55
    关注

    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]) . "
    ";
    }
    
    ?>
    

    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.
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?