简体   繁体   中英

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. 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:

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)   
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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