简体   繁体   English

用PHP创建具有可变参数名称的库函数

[英]creating a library function with variable param name in PHP

I am sure this is obvious to most PHP programmers but I can't find much on the web. 我相信这对大多数PHP程序员来说都是显而易见的,但是我在网上找不到很多。 I am creating a library function and have a function (more complicated that what's below) and need to pass in a single variable. 我正在创建一个库函数,并且具有一个函数(比下面更复杂),并且需要传递一个变量。 I can't guarantee that parameter will be called the same thing every time though. 我不能保证每次都会将同一参数称为同一事物。 So I guess what I'm asking is how to grab the parameter (with multiple possible names) that is passed in and store it in a variable to be manipulated in the function. 因此,我想我想问的是如何获取传入的参数(具有多个可能的名称)并将其存储在要在函数中进行操作的变量中。 Here is an example: 这是一个例子:

function commentFormatTime(){
   $imported time = ?? //how do I grab what I am passing in?
   $replythen = Carbon::createFromFormat('Y-m-d H:i:s', $importedTime)   
}

I would greatly appreciate any explanation if I misused or missing any terms here. 如果我在这里滥用或遗漏了任何术语,我将不胜感激。

This is a logical easier to understand way: 这是一种逻辑上更容易理解的方式:

function commentFormatTime($incoming_variable){
   $importedTime = $incoming_variable;
   $replythen = Carbon::createFromFormat('Y-m-d H:i:s', $importedTime);
}

Or for a faster way, use the incoming variable instead of making another one: 或者以一种更快的方式,使用传入的变量而不是另外一个变量:

function commentFormatTime($importedTime){
   $replythen = Carbon::createFromFormat('Y-m-d H:i:s', $importedTime);
}

And don't use spaces in variable names. 并且不要在变量名中使用空格。

Why not use $param = null in your function ie: 为什么不在函数中使用$param = null ,即:

function commentFormatTime($param = null){
   $imported time = $param; //how do I grab what I am passing in?
   $replythen = Carbon::createFromFormat('Y-m-d H:i:s', $importedTime)   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM