php - 如何在 PHP 中创建函数

标签 php function

我正在尝试创建一个名为 saveorder 的函数。到目前为止,这是我的代码:

<?php
//function foo($arg_1, $arg_2, /* ..., */ $arg_n)
function saveorder($orderid,$txnid,$date,$amount)
{
    $a_str = array( 
        "orderid"=>"175", 
        "txnid"=>"RC456456456", 
        "date"=>"54156456465", 
        "amount"=>"109$" 
    ); 

    $file = 'order.dat';

    $contents = implode("|", $a_str);

    $contents .= PHP_EOL . PHP_EOL;
    file_put_contents($file, $contents);
}
echo "function will return=".saveorder($orderid);
?>

我认为我做错了,因为我从未创建过自己的函数。但是,我仍然想创建此函数以将订单保存在 order.dat 文件中。谁能帮我创建这个功能?我非常努力,但我无法创造它。

最佳答案

你没有返回任何东西。

除此之外功能没问题:

function saveorder($orderid,$txnid,$date,$amount){
    $a_str = array( 
        "orderid"=>$orderid, 
        "txnid"=>$txnid, 
        "date"=>$date, 
        "amount"=>$amount 
    ); 

    $file = 'order.dat';
    $contents = implode("|", $a_str);

    $contents .= PHP_EOL;
    file_put_contents($file, $contents, FILE_APPEND);
    return $contents;
}
echo "function will return=".saveorder("175","RC456456456","54156456465","109$");

关于php - 如何在 PHP 中创建函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7103412/

相关文章:

php - 从mysql数据库中选择查询

php - ajax POST请求的Laravel csrf token 不匹配

c++ - 为非本地容器(数据库后端)重载/专门化 STL 算法

c - 显示数组总和的逻辑缺陷

python - 如何动态导入 python 模块函数?

c# - 包含函数和可选参数的字典

php - mySQL如何根据匹配的列值进行查询?

php - 使用命名空间找不到类

php - json 返回损坏的数据

javascript - 关于滚动交换菜单上的类 - IE 问题