Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5417090
  • 博文数量: 763
  • 博客积分: 12108
  • 博客等级: 上将
  • 技术积分: 15717
  • 用 户 组: 普通用户
  • 注册时间: 2007-09-28 21:21
个人简介

业精于勤,荒于嬉

文章分类

全部博文(763)

文章存档

2018年(6)

2017年(15)

2016年(2)

2015年(31)

2014年(14)

2013年(87)

2012年(75)

2011年(94)

2010年(190)

2009年(38)

2008年(183)

2007年(28)

分类: 系统运维

2013-01-11 19:53:43

1> var_dump

点击(此处)折叠或打开

  1. var_dump
  2. (PHP 3>= 3.0.5, PHP 4 )
  3. var_dump -- 打印变量的相关信息
  4. 描述
  5. void var_dump ( mixed expression [, mixed expression [, ...]])


  6.  此函数显示关于一个或多个表达式的结构信息,包括表达式的类型与值。数组将递归展开值,通过缩进显示其结构。
  7. 提示: 为了防止程序直接将结果输出到浏览器,您可以使用输出控制函数(output-control functions)来捕获函数的输出,并把它们保存到一个 string 型的变量中。

  8.  可以比较一下 var_dump() 与 print_r()
  9. 例子 1. var_dump() 示例
  10. <pre>
  11. <?php
  12. $a = array (1, 2, array ("a", "b", "c"));
  13. var_dump ($a);

  14. /* 输出:
  15. array(3) {
  16.   [0]=>
  17.   int(1)
  18.   [1]=>
  19.   int(2)
  20.   [2]=>
  21.   array(3) {
  22.     [0]=>
  23.     string(1) "a"
  24.     [1]=>
  25.     string(1) "b"
  26.     [2]=>
  27.     string(1) "c"
  28.   }
  29. }

  30. */

  31. $b = 3.1;
  32. $c = TRUE;
  33. var_dump($b,$c);

  34. /* 输出:
  35. float(3.1)
  36. bool(true)

  37. */
  38. ?>
  39. </pre>

2> var_export

点击(此处)折叠或打开

  1. var_export
  2. (PHP 4 >= 4.2.0)
  3. var_export -- 输出或返回一个变量的字符串表示
  4. 描述
  5. mixed var_export ( mixed expression [, bool return])


  6.  此函数返回关于传递给该函数的变量的结构信息,它和 var_dump() 类似,不同的是其返回的表示是合法的 PHP 代码。

  7.  您可以通过将函数的第二个参数设置为 TRUE,从而返回变量的表示。

  8.  比较 var_export() 和 var_dump().
  9. <pre>
  10. <?php
  11. $a = array (1, 2, array ("a", "b", "c"));
  12. var_export ($a);

  13. /* 输出:
  14. array (
  15.   0 => 1,
  16.   1 => 2,
  17.   2 =>
  18.   array (
  19.     0 => 'a',
  20.     1 => 'b',
  21.     2 => 'c',
  22.   ),
  23. )
  24. */

  25. $b = 3.1;
  26. $v = var_export($b, TRUE);
  27. echo $v;

  28. /* 输出:
  29. 3.1
  30. */
  31. ?>
  32. </pre>


3> isset 检测变量是否设置

点击(此处)折叠或打开

  1. isset
  2. (PHP 3, PHP 4 )
  3. isset -- 检测变量是否设置
  4. 描述
  5. bool isset ( mixed var [, mixed var [, ...]])


  6.  如果 var 存在则返回 TRUE,否则返回 FALSE。

  7.  如果已经使用 unset() 释放了一个变量之后,它将不再是 isset()。若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE。同时要注意的是一个 NULL 字节("\0")并不等同于 PHP 的 NULL 常数。
  8. 警告: isset() 只能用于变量,因为传递任何其它参数都将造成解析错误。若想检测常量是否已设置,可使用 defined() 函数。
  9. <?php

  10. $var = '';

  11. // 结果为 TRUE,所以后边的文本将被打印出来。
  12. if (isset($var)) {
  13.     print "This var is set set so I will print.";
  14. }

  15. // 在后边的例子中,我们将使用 var_dump 输出 isset() 的返回值。

  16. $a = "test";
  17. $b = "anothertest";

  18. var_dump( isset($a) ); // TRUE
  19. var_dump( isset ($a, $b) ); // TRUE

  20. unset ($a);

  21. var_dump( isset ($a) ); // FALSE
  22. var_dump( isset ($a, $b) ); // FALSE

  23. $foo = NULL;
  24. var_dump( isset ($foo) ); // FALSE

  25. ?>

  26.  这对于数组中的元素也同样有效:
  27. <?php

  28. $a = array ('test' => 1, 'hello' => NULL);

  29. var_dump( isset ($a['test']) ); // TRUE
  30. var_dump( isset ($a['foo']) ); // FALSE
  31. var_dump( isset ($a['hello']) ); // FALSE

  32. // 键 'hello' 的值等于 NULL,所以被认为是未置值的。
  33. // 如果想检测 NULL 键值,可以试试下边的方法。
  34. var_dump( array_key_exists('hello', $a) ); // TRUE

  35. ?>

4> unset 释放给定的变量

点击(此处)折叠或打开

  1. unset
  2. (PHP 3, PHP 4 )
  3. unset -- 释放给定的变量
  4. 描述
  5. void unset ( mixed var [, mixed var [, ...]])

  6. unset() 销毁指定的变量。注意在 PHP 3 中,unset() 将返回 TRUE(实际上是整型值 1),而在 PHP 4 中,unset() 不再是一个真正的函数:它现在是一个语句。这样就没有了返回值,试图获取 unset() 的返回值将导致解析错误。
  7. 例子 1. unset() 示例
  8. <?php
  9. // 销毁单个变量
  10. unset ($foo);

  11. // 销毁单个数组元素
  12. unset ($bar['quux']);

  13. // 销毁一个以上的变量
  14. unset ($foo1, $foo2, $foo3);
  15. ?>
  16. unset() 在函数中的行为会依赖于想要销毁的变量的类型而有所不同。

  17.  如果在函数中 unset() 一个全局变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。
  18. <?php
  19. function destroy_foo() {
  20.     global $foo;
  21.     unset($foo);
  22. }

  23. $foo = 'bar';
  24. destroy_foo();
  25. echo $foo;
  26. ?>
  27. 上边的例子将输出:
  28. bar

  29.  如果在函数中 unset() 一个通过引用传递的变量,则只是局部变量被销毁,而在调用环境中的变量将保持调用 unset() 之前一样的值。
  30. <?php
  31. function foo(&$bar) {
  32.     unset($bar);
  33.     $bar = "blah";
  34. }

  35. $bar = 'something';
  36. echo "$bar\n";

  37. foo($bar);
  38. echo "$bar\n";
  39. ?>
  40. 上边的例子将输出:
  41. something
  42. something

  43.  如果在函数中 unset() 一个静态变量,则 unset() 将销毁此变量及其所有的引用。
  44. <?php
  45. function foo() {
  46.     static $a;
  47.     $a++;
  48.     echo "$a\n";
  49.     unset($a);
  50. }

  51. foo();
  52. foo();
  53. foo();
  54. ?>
  55. 上边的例子将输出:
  56. 1
  57. 2
  58. 3

  59.  如果您想在函数中 unset() 一个全局变量,可使用 $GLOBALS 数组来实现:
  60. <?php
  61. function foo() {
  62.     unset($GLOBALS['bar']);
  63. }

  64. $bar = "something";
  65. foo();
  66. ?>
5> echo 输出字符串

点击(此处)折叠或打开

  1. echo
  2. (PHP 3, PHP 4 )
  3. echo -- Output one or more strings
  4. Description
  5. void echo ( string arg1 [, string argn...])


  6.  Outputs all parameters.
  7. echo() is not actually a function (it is a language construct) so you are not required to use parentheses with it. In fact, if you want to pass more than one parameter to echo, you must not enclose the parameters within parentheses.
  8. 例子 1. echo() examples
  9. <?php
  10. echo "Hello World";

  11. echo "This spans
  12. multiple lines. The newlines will be
  13. output as well";

  14. echo "This spans\nmultiple lines. The newlines will be\noutput as well.";

  15. echo "Escaping characters is done \"Like this\".";

  16. // You can use variables inside of an echo statement
  17. $foo = "foobar";
  18. $bar = "barbaz";

  19. echo "foo is $foo"; // foo is foobar

  20. // You can also use arrays
  21. $bar = array("value" => "foo");

  22. echo "this is {$bar['value']} !"; // this is foo !

  23. // Using single quotes will print the variable name, not the value
  24. echo 'foo is $foo'; // foo is $foo

  25. // If you are not using any other characters, you can just echo variables
  26. echo $foo; // foobar
  27. echo $foo,$bar; // foobarbarbaz

  28. // Some people prefer passing multiple parameters to echo over concatenation.
  29. echo 'This ', 'string ', 'was ', 'made ', 'with multiple parameters.', chr(10);
  30. echo 'This ' . 'string ' . 'was ' . 'made ' . 'with concatenation.' . "\n";

  31. echo <<<END
  32. This uses the "here document" syntax to output
  33. multiple lines with $variable interpolation. Note
  34. that the here document terminator must appear on a
  35. line with just a semicolon. no extra
  36. END;
  37. // Because echo is not a function, following code is invalid.
  38. ($some_var) ? echo 'true' : echo 'false';

  39. // However, the following examples will work:
  40. ($some_var) ? print('true'): print('false'); // print is a function
  41. echo $some_var ? 'true': 'false'; // changing the statement around
  42. ?>

6> empty -- 检查一个变量是否为空

如果 var 是非空或非零的值,则 empty() 返回 FALSE。换句话说,""0"0"NULLFALSEarray()var $var; 以及没有任何属性的对象都将被认为是空的,如果 var 为空,则返回 TRUE

除了当变量没有置值时不产生警告之外,empty()(boolean) var 的反义词。参见获取更多信息。

点击(此处)折叠或打开

  1. <?php
  2. $var = 0;

  3. // 结果为 true,因为 $var 为空
  4. if (empty($var)) {
  5.     echo '$var is either 0 or not set at all';
  6. }

  7. // 结果为 false,因为 $var 已设置
  8. if (!isset($var)) {
  9.     echo '$var is not set at all';
  10. }
  11. ?>


7> is_array -- 检测变量是否是数组

is_bool --  检测变量是否是布尔型

is_double --  的别名

is_real --  的别名

is_float -- 检测变量是否是浮点型

is_int -- 检测变量是否是整数

is_integer --  的别名

is_long --  的别名

is_null --  检测变量是否为 NULL

is_numeric --  检测变量是否为数字或数字字符串

is_object -- 检测变量是否是一个对象

is_resource --  检测变量是否为资源类型

is_string -- 检测变量是否是字符串


is_scalar --  检测变量是否是一个标量

如果给出的变量参数 var 是一个标量,is_scalar() 返回 TRUE,否则返回 FALSE

标量变量是指那些包含了 、、 或 的变量,而 、 和 则不是标量。


strval -- 获取变量的字符串值




















阅读(1089) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~