Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.