PHP:10個不常見卻非常有用的PHP函式
$a = 0;
function iterate() {
global $a;
if( $a < 10 )
recur();
echo $a . “, “;
}
function recur() {
global $a;
$a++;
// how did I get here?
echo “\n\n\n”;
debug_print_backtrace();
if( $a < 10 )
iterate();
}
iterate();
# OUTPUT:
#0 recur() called at [C:\htdocs\php_stuff\index.php:8]
#1 iterate() called at [C:\htdocs\php_stuff\index.php:25]
#0 recur() called at [C:\htdocs\php_stuff\index.php:8]
#1 iterate() called at [C:\htdocs\php_stuff\index.php:21]
#2 recur() called at [C:\htdocs\php_stuff\index.php:8]
#3 iterate() called at [C:\htdocs\php_stuff\index.php:25]
#0 recur() called at [C:\htdocs\php_stuff\index.php:8]
#1 iterate() called at [C:\htdocs\php_stuff\index.php:21]
#2 recur() called at [C:\htdocs\php_stuff\index.php:8]
#3 iterate() called at [C:\htdocs\php_stuff\index.php:21]
#4 recur() called at [C:\htdocs\php_stuff\index.php:8]
#5 iterate() called at [C:\htdocs\php_stuff\index.php:25]
$items = array(
“100 apples”, “5 apples”, “110 apples”, “55 apples”
);
// normal sorting:
sort($items);
print_r($items);
# Outputs:
# Array
# (
# [0] => 100 apples
# [1] => 110 apples
# [2] => 5 apples
# [3] => 55 apples
# )
natsort($items);
print_r($items);
# Outputs:
# Array
# (
# [2] => 5 apples
# [3] => 55 apples
# [0] => 100 apples
# [1] => 110 apples
# )
$dictionary = array(
“php”, “javascript”, “css”
);
$word = “japhp”;
$best_match = $dictionary[0];
$match_value = levenshtein($dictionary[0], $word);
foreach($dictionary as $w) {
$value = levenshtein($word, $w);
if( $value < $match_value ) {
$best_match = $w;
$match_value = $value;
}
}
echo “Did you mean the ‘$best_match’ category?”;
foreach (glob(“*.php”) as $file)
echo “$file\n”;