phpの役に立つ10の関数を抜粋します
24382 ワード
1 1. sys_getloadavg()
2
3 sys_getloadavt() 。 , 1、5 15 。 , die ,sys_getloadavg() 。 , windows 。
4
5 2. pack()
6
7 Pack() md5() 32 16 16 , 。
8
9 3. cal_days_in_month()
10
11 cal_days_in_month() 。
12
13 4. _()
14
15 WordPress , _e()。 , gettext() , 。 PHP 。
16
17 5. get_browser()
18
19 ?get_browser() , , php_browscap.ini , 。
20
21 , 。 , JavaScript , 。 OS , 。
22
23 6. debug_print_backtrace()
24
25 , 。 , :
26
27 $a = 0;
28 function iterate() {
29 global $a;
30 if( $a < 10 )
31 recur();
32 echo $a . “, “;
33 }
34 function recur() {
35 global $a;
36 $a++;
37 // how did I get here?
38 echo “
”;
39 debug_print_backtrace();
40 if( $a < 10 )
41 iterate();
42 }
43 iterate();
44 # OUTPUT:
45 #0 recur() called at [C:\htdocs\php_stuff\index.php:8]
46 #1 iterate() called at [C:\htdocs\php_stuff\index.php:25]
47 #0 recur() called at [C:\htdocs\php_stuff\index.php:8]
48 #1 iterate() called at [C:\htdocs\php_stuff\index.php:21]
49 #2 recur() called at [C:\htdocs\php_stuff\index.php:8]
50 #3 iterate() called at [C:\htdocs\php_stuff\index.php:25]
51 #0 recur() called at [C:\htdocs\php_stuff\index.php:8]
52 #1 iterate() called at [C:\htdocs\php_stuff\index.php:21]
53 #2 recur() called at [C:\htdocs\php_stuff\index.php:8]
54 #3 iterate() called at [C:\htdocs\php_stuff\index.php:21]
55 #4 recur() called at [C:\htdocs\php_stuff\index.php:8]
56 #5 iterate() called at [C:\htdocs\php_stuff\index.php:25]
57 7. metaphone()
58
59 metaphone , metaphone , 。
60
61 8. natsort()
62
63 natsort() , :
64
65 $items = array(
66 “100 apples”, “5 apples”, “110 apples”, “55 apples”
67 );
68 // normal sorting:
69 sort($items);
70 print_r($items);
71 # Outputs:
72 # Array
73 # (
74 # [0] => 100 apples
75 # [1] => 110 apples
76 # [2] => 5 apples
77 # [3] => 55 apples
78 # )
79 natsort($items);
80 print_r($items);
81 # Outputs:
82 # Array
83 # (
84 # [2] => 5 apples
85 # [3] => 55 apples
86 # [0] => 100 apples
87 # [1] => 110 apples
88 # )
89 9. levenshtein()
90
91 Levenshtein() “ ”。 , 、 。
92
93 :
94
95 $dictionary = array(
96 “php”, “javascript”, “css”
97 );
98 $word = “japhp”;
99 $best_match = $dictionary[0];
100 $match_value = levenshtein($dictionary[0], $word);
101 foreach($dictionary as $w) {
102 $value = levenshtein($word, $w);
103 if( $value < $match_value ) {
104 $best_match = $w;
105 $match_value = $value;
106 }
107 }
108 echo “Did you mean the ‘$best_match’ category?”;
109 10. glob()
110
111 glob() opendir(), readdir() closedir() 。
112
113 foreach (glob(“*.php”) as $file)
114 echo “$file
”;
1、is_a($obj,$class)
2、strtotime()
3、array_search($val); , , , , ‘0’, !==false, php , false
いくつかのPHP関数:
is_a:Checks if the object is of this class or has this class as one of its parents (return bool)
1 <?php
2 // define a class
3 class WidgetFactory
4 {
5 var $oink = 'moo';
6 }
7
8 // create a new object
9 $WF = new WidgetFactory();
10
11 if (is_a($WF, 'WidgetFactory')) {
12 echo "yes, \$WF is still a WidgetFactory
";
13 }
14 ?>
get_object_vars:get_object_vars — Gets the properties of the given object (return array)
1 <?php
2
3 class foo {
4 private $a;
5 public $b = 1;
6 public $c;
7 private $d;
8 static $e;
9
10 public function test() {
11 var_dump(get_object_vars($this));
12 }
13 }
14
15 $test = new foo;
16 var_dump(get_object_vars($test));
17
18 $test->test();
20 ?>
21
22 will output:
23
24 array(2) {
25 ["b"]=> int(1)
27 ["c"]=> NULL
29 }
30 array(4) {
31 ["a"]=> NULL
33 ["b"]=>int(1)
35 ["c"]=> NULL
37 ["d"]=> NULL
39 }
property_exists: Checks if the object or class has a property (return bool)
1 <?php
2
3 class myClass {
4 public $mine;
5 private $xpto;
6 static protected $test;
7
8 static function test() {
9 var_dump(property_exists('myClass', 'xpto')); //true
10 }
11 }
12
13 var_dump(property_exists('myClass', 'mine')); //true
14 var_dump(property_exists(new myClass, 'mine')); //true
15 var_dump(property_exists('myClass', 'xpto')); //true, as of PHP 5.3.0
16 var_dump(property_exists('myClass', 'bar')); //false
17 var_dump(property_exists('myClass', 'test')); //true, as of PHP 5.3.0
18 myClass::test();
19
20 ?>
sprintf()関数は、自分で見て、時には簡単に使うことがあります.特にsqlの中に複数のパラメータ変数があるときは、とても簡単です.
1 __DIR__ ,php5.3
2 uniqid : generate unique ID]
3 json_encode: json
4 json_decode : ,
5
6 :()
7 gzencode()
8 gzcompress()
9 :
gzdecode()
gzuncompress()