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 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图