note 45798 deleted from language.functions by tomsommer
| From: | tomsommer@php.net | Date: | Sun, 19 Sep 2004 18:26:33 +0000 |
| Subject: | note 45798 deleted from language.functions by tomsommer | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76984@lists.php.net to get a copy of this message | ||
Note Submitter: jtgalkowski at alum dot mit dot edu
----
PHP 4 and above do support functional parameters. These can be used in a study of both that and of
typed comparisons in PHP, as below:
function sameString( $x, $y ) {
return( 0 == strcmp( $x, $y ) ) ;
}
function sameInteger( $x, $y ) {
return( $x == $y ) ;
}
$MachineEpsilon = 1.0e-7 ;
$ComparisonEpsilon = 8 * $MachineEpsilon ;
function sameFlonum( $x, $y ) {
global $ComparisonEpsilon ;
return( $ComparisonEpsilon > abs( $x - $y ) ) ;
}
function memberUsing( $samenessPredicate, $item, $arrayOfItems ) {
$found = 0 ;
if ( is_callable( $samenessPredicate ) ) {
foreach( $arrayOfItems as $thing ) {
if ( @call_user_func( $samenessPredicate, $item, $thing ) ) {
$found = 1 ;
break ;
}
}
}
return( $found ) ;
}
$CandidatesArray = array( 23, -6, 3.1415926, 'Mac Hack', 'UUO link', 56, 4.001 )
;
echo( '<br><br>' ) ;
echo( '-6 ' . memberUsing( 'sameInteger', -6, $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '-7 ' . memberUsing( 'sameInteger', -7, $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '3.1415926 ' . memberUsing( 'sameFlonum', 3.1415926, $CandidatesArray ) )
;
echo( '<br>' ) ;
echo( '3.14 ' . memberUsing( 'sameFlonum', 3.14, $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '\'Mac Hack\' ' . memberUsing( 'sameString', 'Mac
Hack', $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '\'Mac Hack\' ' . memberUsing( 'sameInteger', 'Mac
Hack', $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '\'Mad Hatter\' ' . memberUsing( 'sameInteger', 'Mad
Hatter', $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '\'Mac Hack\' ' . memberUsing( 'sameFlonum', 'Mac
Hack', $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '56 ' . memberUsing( 'sameString', 56, $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '56 ' . memberUsing( 'sameInteger', 56, $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '56 ' . memberUsing( 'sameFlonum', 56, $CandidatesArray ) ) ;
echo( '<br>' ) ;
echo( '56 ' . memberUsing( 'diddlyDumm', 56, $CandidatesArray ) ) ;
Using PHP 4.3.8 on Windows XP post-SP2 yields:
-6 1
-7 0
3.1415926 1
3.14 0
'Mac Hack' 1
'Mac Hack' 1
'Mad Hatter' 0
'Mac Hack' 1
56 1
56 1
56 1
56 0