I am having trouble understanding the following code:
<?php
$myText = <<<END_TEXT
But think not that this famous town has
only harpooneers, cannibals, and
bumpkins to show her visitors. Not at
all. Still New Bedford is a queer place.
Had it not been for us whalemen, that
tract of land would this day perhaps
have been in as howling condition as the
coast of Labrador.
END_TEXT;
echo “<h2>The text:</h2>”;
echo “<div style=\”width: 30em;\”>$myText</div>”;
$myText = preg_replace( “/[\,.]/”, “”, $myText );
$words = array_unique( preg_split( “/[ \n\r\t]+/”, $myText ) );
usort( $words, create_function( ‘$a, $b’, ‘return strlen($a) - strlen($b);
’ ) );
echo “<h2>The sorted words:</h2>”;
echo “<div style=\”width: 30em;\”>”;
foreach ( $words as $word ) {
echo “$word “;
}
echo “</div>”;
?>
Basically, what this (above) code is suppose to do is display an unedited version of the paragraph stored in the $myText and beneath that it displays an edited version of the paragraph which rearranges the words in order of size (smallest to biggest).
I don't have any visuals to show you but I have a link to the author's book (which contains visuals ( which you can view online for free),,,, page 155. http://www.cs.karelia.ru/~musen/php/...tt%20Doyle.pdf
I think i understand the majority of the code (above).
But i am kind of confused with how the 'anonymous function' (shown below) works in the above code:
usort( $words, create_function( ‘$a, $b’, ‘return strlen($a) - strlen($b);
here are the things i don't understand:
1) how does this function even work if its not even referenced by any of the other code?
2) and why on earth are $a and $b used and what does passing them in the function actually do, since they are blank variables?
Please help me understand.
Paul.