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 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动
  • ¥15 x趋于0时tanx-sinx极限可以拆开算吗
  • ¥500 把面具戴到人脸上,请大家贡献智慧,别用大模型回答,大模型的答案没啥用
  • ¥15 任意一个散点图自己下载其js脚本文件并做成独立的案例页面,不要作在线的,要离线状态。