function time_parts($time) {
return explode(':', $time);
}
list($hour, $minute, $second) = time_parts('12:34:56');
You pass in a time string as you might see on a digital clock and
call explode( ) to break it apart as array
elements. When time_parts( ) returns, use
list( ) to take each element and store it in a
scalar variable. Although this is a little inefficient, the other
possible solutions are worse because they can lead to confusing code.
function time_parts($time, &$hour, &$minute, &$second) {
list($hour, $minute, $second) = explode(':', $time);
}
time_parts('12:34:56', $hour, $minute, $second);
Without knowledge of the function prototype, there's
no way to look at this and know $hour,
$minute, and $second are, in
essence, the return values of time_parts( ).
function time_parts($time) {
global $hour, $minute, $second;
list($hour, $minute, $second) = explode(':', $time);
}
time_parts('12:34:56');
Again, here it's clear because the function is
directly above the call, but if the function is in a different file
or written by another person, it'd be more
mysterious and thus open to creating a subtle bug.
Our advice is that if you modify a value inside a function, return
that value and assign it to a variable unless you have a very good
reason, such as significant performance issues. It's
cleaner and easier to understand and maintain.