wyattbiker Posted July 18, 2011 Share Posted July 18, 2011 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 More sharing options...
AyKay47 Posted July 18, 2011 Share Posted July 18, 2011 it is a ternary operator that doesn't seem to be necessary here, basically it checks if the variable $callback equates to TRUE, if it does, it will return $callback($i), if not, it will return $i. Link to comment https://forums.phpfreaks.com/topic/242277-crazy-php-function-variable-syntax/#findComment-1244296 Share on other sites More sharing options...
btherl Posted July 19, 2011 Share Posted July 19, 2011 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. Link to comment https://forums.phpfreaks.com/topic/242277-crazy-php-function-variable-syntax/#findComment-1244402 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.