Jump to content

Crazy PHP Function Variable Syntax.


wyattbiker

Recommended Posts

I found this section of code below that builds a months select/options list.

The variable $callback is set to  'callback_month' function name to return the month of the index.  What does the expression ($callback?$callback($i):$i) do?

Is it necessary?

 

I just used ($callback($i)) and it did the exact same thing. Thanks

 

$return_string=array();
for($i=$from;$i<=$to;$i++) {
$return_string[]='<option value="'.$i.'">'.($callback?$callback($i):$i).'</option>';
}

function callback_month($month) {
return date('M',mktime(0,0,0,$month,1));
}

Link to comment
https://forums.phpfreaks.com/topic/242277-crazy-php-function-variable-syntax/
Share on other sites

It probably means "If we were given a callback, call the callback on $i before inserting it into the html, otherwise insert $i directly".  It's allowing the use of a callback as a kind of filter for data going into the HTML.

 

So if $callback is set to a function name it calls that function on $i.  Otherwise it just uses $i.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.