Page 1 of 1
anything wrong with this code?
Posted: Thu Oct 23, 2003 3:04 am
by qingren
Code: Select all
<?php
function by_length ($a ,$b ){
$l_a=strlen($a);
$l_b=strlen($b);
if ($l_a==$l_b) return 0;
return ($l_a<$l_b)?-1:1;
}
$countries=array("e"=>"United States",
"d"=>"United Kingdom",
"c"=>"Canada",
"b"=>"Costa Rica",
"a"=>"Germany");
usort ($countries, by_length);
while (list ($key,$val)=each($countries)){
echo "Element $key equals $val<br>\n";
}
Newbie here
thanks in advance.
?>
Posted: Thu Oct 23, 2003 3:13 am
by JayBird
What do you mean?
Do you get an error?
Mark
?
Posted: Thu Oct 23, 2003 3:15 am
by itsmani1
wel i donot think so identify if any
Posted: Thu Oct 23, 2003 3:18 am
by qingren
Sorry.
At first i get the error msg below.
Notice: Use of undefined constant by_length - assumed 'by_length' in E:\Program Files\Apache Group\Apache2\htdocs\myroot\mypractice\usort.php on line 16
But when i change the code from
usort ($countries, by_length);
to
usort ($countries, "by_length");
it is running fine.....
is it right to code so?

Posted: Thu Oct 23, 2003 3:36 am
by markl999
is it right to code so?
Yes. usort ($countries, by_length); will look for a constant named by_length, for example, define('by_length', 'hellooooo');
But you want by_length to refer to the the function named 'by_length' so you must do usort ($countries, 'by_length');
Posted: Thu Oct 23, 2003 3:42 am
by qingren
Thank you very much..
