使用 <?php 而不是 <? 作为PHP语法标记,因为如果使用短格式,诸如 <?xml 会被误判。
Heredoc
Another way to delimit strings is by using heredoc syntax ("<<<").
<?php $str = <<<EOD Example of string spanning multiple lines using heredoc syntax. EOD; echo <<<EOT My name is "$name". I am printing some $foo->foo. Now, I am printing some {$foo->bar[1]}. This should print a capital 'A': \x41 EOT; ?>
Variable scope
function Sum() { global $a, $b; $b = $a + $b; } --- or --- function Sum() { $GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"]; }
Variable variables
<?php $a = "hello"; $$a = "world"; echo "$a ${$a}"; // output: hello world ?>
PHP Superglobals
因为从 4.2.0 之后的 PHP, register_globals 缺省为 flase,使得不能用 $GLOBALS["xxx"] 来访问用户提交的变量。而用如下所述的变量。
但也可以用函数 import_request_variables ,将用户提交变量转换为全局变量。
// This will import GET and POST vars // with an "rvar_" prefix import_request_variables("gP", "rvar_");
NewImplement | obsolete |
---|---|
$GLOBALS | |
$_SERVER | $HTTP_SERVER_VARS |
$_GET | $HTTP_GET_VARS |
$_POST | $HTTP_POST_VARS |
$_COOKIE | $HTTP_COOKIE_VARS |
$_FILES | $HTTP_POST_FILES |
$_ENV | $HTTP_ENV_VARS |
$_REQUEST | |
$_SESSION | $HTTP_SESSION_VARS |
$_REQUEST USED FOR QUICK ACCESS: Variables provided to the script via any user input mechanism, and which therefore cannot be trusted. The presence and order of variable inclusion in this array is defined according to the variables_order configuration directive. (Variables_order configure directive: set the order of the EGPCS (Environment, GET, POST, Cookie, Server) ) |
Variables from outside PHP
<?php // Available since PHP 4.1.0 print $_POST['username']; print $_REQUEST['username']; import_request_variables('p', 'p_'); print $p_username; // Available since PHP 3. print $HTTP_POST_VARS['username']; // Available if the PHP directive register_globals = on. As of // PHP 4.2.0 the default value of register_globals = off. // Using/relying on this method is not preferred. print $username; ?>
Defining Constants
<?php define("CONSTANT", "Hello world."); echo CONSTANT; // outputs "Hello world." echo Constant; // outputs "Constant" and issues a notice. ?>
Error Control Operators
The at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored.
Syntax: foreach
foreach(array_expression as $value) statement foreach(array_expression as $key => $value) statement
The first form loops over the array given by array_expression. On each loop, the value of the current element is assigned to $value and the internal array pointer is advanced by one (so on the next loop, you'll be looking at the next element).
The second form does the same thing, except that the current element's key will be assigned to the variable $key on each loop.
You may have noticed that the following are functionally identical: reset ($arr); while (list(, $value) = each ($arr)) { echo "Value: $value<br>\n"; } foreach ($arr as $value) { echo "Value: $value<br>\n"; } The following are also functionally identical: reset ($arr); while (list($key, $value) = each ($arr)) { echo "Key: $key; Value: $value<br>\n"; } foreach ($arr as $key => $value) { echo "Key: $key; Value: $value<br>\n"; } |
Syntax: require() and include()
require() and include() are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, don't hesitate to use require() if you want a missing file to halt processing of the page.
There are another two function, require_once() and include_once(), which prevent a file being included twice. But this can be done through a C header like declaration. |
Syntax: Function variables - passed by value and by reference
// variable $string is passed by reference. function add_some_extra(&$string) { $string .= 'and something extra.'; }
Syntax: Function variables - Default argument values
function makecoffee ($type = "cappuccino") { return "Making a cup of $type.\n"; } echo makecoffee (); echo makecoffee ("espresso");
Syntax: Function variables - Variable variables
<?php function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs\n"; } foo (1, 2, 3); // Prints 'Number of arguments: 3' ?> <?php function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs<br>\n"; if ($numargs >= 2) { echo "Second argument is: " . func_get_arg (1) . "<br>\n"; } } foo (1, 2, 3); ?> <?php function foo() { $numargs = func_num_args(); echo "Number of arguments: $numargs<br>\n"; if ($numargs >= 2) { echo "Second argument is: " . func_get_arg (1) . "<br>\n"; } $arg_list = func_get_args(); for ($i = 0; $i < $numargs; $i++) { echo "Argument $i is: " . $arg_list[$i] . "<br>\n"; } } foo (1, 2, 3); ?>
Syntax: Function return - return a reference
function &returns_reference() { return $someref; } $newref =& returns_reference();
Syntax: Variable functions
<?php function foo() { echo "In foo()<br>\n"; } function bar($arg = '') { echo "In bar(); argument was '$arg'.<br>\n"; } $func = 'foo'; $func(); $func = 'bar'; $func('test'); ?>
Syntax: Class - Serializing objects
可以将对象存储和恢复。
classa.inc: class A { var $one = 1; function show_one() { echo $this->one; } } page1.php: include("classa.inc"); $a = new A; $s = serialize($a); // store $s somewhere where page2.php can find it. $fp = fopen("store", "w"); fputs($fp, $s); fclose($fp); page2.php: // this is needed for the unserialize to work properly. include("classa.inc"); $s = implode("", @file("store")); $a = unserialize($s); // now use the function show_one() of the $a object. $a->show_one();
Syntax: Class - :: and parent
class A { function example() { echo "I am the original function A::example().<br>\n"; } } class B extends A { function example() { echo "I am the redefined function B::example().<br>\n"; A::example(); } } // or class B extends A { function example() { echo "I am B::example() and provide additional functionality.<br>\n"; parent::example(); } }
Copyright © 2006 WorldHello 开放文档之源 计划 |