rss· 投稿· 设为首页· 加入收藏· 繁體版
当前位置: 火魔网 » 程序开发 » PHP

php 编程笔记

include $_SERVER["DOCUMENT_ROOT"] . "/myscript.php";
      In addition, if the INI directive, allow_url_fopen, is enabled in your PHP
configuration (the default), you can also include URLs. This method is not rec-
ommended for performance reasons because PHP must first download the
source code to be included before it runs it. So, use this option only when it’s
really necessary. Here’s an example:
      include "http://www.example.org/example.php";
     The included URL must return a valid PHP script and not a web page
which is HTML (possibly created by PHP). You can also use other protocols
besides HTTP, such as FTP.
     When the included file or URL doesn’t exist, include emits a PHP warn-
ing but does not halt execution. If you want PHP to error out in such a case
(usually, this is a fatal condition, so that’s what you’d probably want), you can
use the require statement, which is otherwise identical to include.
     There are two additional variants of include/require, which are probably
the most useful. include_once/require_once which behave exactly like their
include/require counterparts, except that they “remember” what files have
been included, and if you try and include_once/require_once the same file
again, it is just ignored.
$str = '$var = 5;';
eval($str);
print $var;
          As mentioned in the “Variables” section, you can use the built-in
$GLOBALS[] array to access variables in the global scope of the script.
     Rewrite the previous script the following way:
     function func ()          $GLOBALS["var"] = 2;      $var = 1;
     func();
     print $var;
    Here’s a short description of it—but please, don’t use it!
    The syntax is
    global $var1, $var2, ...;
Adding a global line for the previous example results in the following:
    function func()         global $var;
        $var = 2;     $var = 1;
    func();
    print $var;
     function get_global_variable_value($name)           return $GLOBALS[$name];      $num = 10;
     $value = get_global_variable_value("num");
     print $value;
     This code prints the number 10. However, making changes to $value before
the print statement only affects $value and not the global variable $num. This is
because its value was returned by the get_global_variable_value() by value and
not by reference.
      Returning Values By Reference
PHP also allows you to return variables by reference. This means that you’re
not returning a copy to the variable, but you’re returning the address of your
variable instead, which enables you to change it from the calling scope. To
return a variable by-reference, you need to define the function as such by plac-
ing an & sign in front of the function’s name and in the caller’s code, assigning
the return value by reference to $value:
     function &get_global_variable($name)           return $GLOBALS[$name];      $num = 10;
     $value =& get_global_variable("num");
     print $value . "\n";
     $value = 20;
     print $num;
     The previous code prints as
     10
     20
         By-Reference Parameters Passing by-reference requires the argu-
ment to be a variable. Instead of the variable’s value being passed, the corre-
sponding variable in the function directly refers to the passed variable
whenever used. Thus, if you change it inside the function, it affects the sent
variable in the outer scope as well:
      function square(&$n)           $n = $n*$n;       $number = 4;
      square($number);
      print $number;
      The & sign that proceeds $n in the function parameters tells PHP to pass
it by-reference, and the result of the function call is $number squared; thus, this
code would print 16.
        Default Parameters Default parameters like C++ are supported by
PHP. Default parameters enable you to specify a default value for function
parameters that aren’t passed to the function during the function call. The
default values you specify must be a constant value, such as a scalar, array
with scalar values, or constant.
          2.8 Functions                                                                             53
                        The following is an example for using default parameters:
                        function increment(&$num, $increment = 1)                                $num += $increment;                         $num = 4;
                        increment($num);
                        increment($num, 3);
                        This code results in $num being incremented to 8. First, it is incremented
                 by 1 by the first call to increment, where the default increment size of 1 is used,
                 and second, it is incremented by 3, altogether by 4.
Note: When you a call a function with default arguments, after you omit a
default function argument, you must emit any following arguments. This also
means that following a default argument in the function’s definition, all other
arguments must also be declared as default arguments.
       Static Variables
Like C, PHP supports declaring local function variables as static. These kind
of variables remain in tact in between function calls, but are still only accessi-
ble from within the function they are declared. Static variables can be initial-
ized, and this initialization only takes place the first time the static
declaration is reached.
      Here’s an example for the use of static that runs initialization code the
first time (and only the first time) the function is run:
      function do_something()           static first_time = true;
          if (first_time) {
               // Execute this code only the first time the function is
                   ➥called           }
          // Execute the function's main logic every time the function is
              ➥called       }
顶一下
(0)
踩一下
(0)