note 45798 added to language.functions
| From: | jtgalkowski at alum dot mit dot edu | Date: | Sun, 19 Sep 2004 16:47:19 +0000 |
| Subject: | note 45798 added to language.functions | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-76980@lists.php.net to get a copy of this message | ||
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
----
Manual Page -- http://www.php.net/manual/en/language.functions.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+45798
Delete -- http://master.php.net/manage/user-notes.php?action=delete+45798&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+45798&report=yes
Search -- http://master.php.net/manage/user-notes.php