在上篇文章給大家介紹了php可變變量,今天通過本篇文章給大家介紹php可變函數,具體詳情如下。
PHP 支持可變函數的概念。這意味著如果一個變量名後有圓括號,PHP 將尋找與變量的值同名的函數,並且嘗試執行它。可變函數可以用來實現包括回調函數,函數表在內的一些用途。
可變函數不能用於例如 echo,print,unset(),isset(),empty(),include,require 以及類似的語言結構。需要使用自己的包裝函數來將這些結構用作可變函數。
Example #1 可變函數示例
<?php
function foo() {
echo "In foo()<br />\n";
}
function bar($arg = '') {
echo "In bar(); argument was '$arg'.<br />\n";
}
// 使用 echo 的包裝函數
function echoit($string)
{
echo $string;
}
$func = 'foo';
$func(); // This calls foo()
$func = 'bar';
$func('test'); // This calls bar()
$func = 'echoit';
$func('test'); // This calls echoit()
?>
也可以用可變函數的語法來調用一個對象的方法。
<?php
class Foo
{
function Variable()
{
$name = 'Bar';
$this->$name(); // This calls the Bar() method
}
function Bar()
{
echo "This is Bar";
}
}
$foo = new Foo();
$funcname = "Variable";
$foo->$funcname(); // This calls $foo->Variable()
?>
當調用靜態方法時,函數調用要比靜態屬性優先:
Example #3 Variable 方法和靜態屬性示例
<?php
class Foo
{
static $variable = 'static property';
static function Variable()
{
echo 'Method Variable called';
}
}
echo Foo::$variable; // This prints 'static property'. It does need a $variable in this scope.
$variable = "Variable";
Foo::$variable(); // This calls $foo->Variable() reading $variable in this scope.
?>
可以使用可變函數的方法列表如下:
<?php array_diff_assoc() array_diff_key() array_diff_uassoc() array() array_intersect_ukey() array_map() array_merge() array_merge_recursive() array_multisort() array_push() array_replace() array_replace_recursive() array_unshift() call_user_func() call_user_method() compact() dba_open() dba_popen() echo() forward_static_call() fprintf() fscanf() httprequestpool_construct() ibase_execute() ibase_set_event_handler() ibase_wait_event() isset() list() maxdb_stmt_bind_param() maxdb_stmt_bind_result() mb_convert_variables() newt_checkbox_tree_add_item() newt_grid_h_close_stacked() newt_grid_h_stacked() newt_grid_v_close_stacked() newt_grid_v_stacked() newt_win_choice() newt_win_entries() newt_win_menu() newt_win_message() newt_win_ternary() pack() printf() register_shutdown_function() register_tick_function() session_register() setlocale() sprintf() sscanf() unset() var_dump() w32api_deftype() w32api_init_dtype() w32api_invoke_function() wddx_add_vars() wddx_serialize_vars()
下面給大家介紹下 PHP Math函數
php math簡介
數學 (Math) 函數能處理 integer 和 float 范圍內的值。
php math 函數
PHP:指示支持該函數的最早的 PHP 版本。
PHP Math 常量