zoukankan      html  css  js  c++  java
  • php函数总结2

    1.匿名参数

    func_get_args      //获取参数列表的数组

    func_get_arg(num)   //数组索引

    func_num_args()    //获取匿名函数的个数

    <?php
    function foo()
    {
        $numargs = func_num_args();    //总共有几个匿名函数
        echo "Number of arguments: $numargs<br />
    ";
        if ($numargs >= 2) {
            echo "Second argument is: " . func_get_arg(1) . "<br />
    ";
        }
        $arg_list = func_get_args();  //返回一个包含函数参数列表的数组
        for ($i = 0; $i < $numargs; $i++) {
            echo "Argument $i is: " . $arg_list[$i] . "<br />
    ";
        }
    }
    
    foo(1, 2, 3);
    ?>

     2.serialize()与unserialize()函数

    返回一个具有变量类型和结构的字符串表达式

    <?php
    //声明一个类
    class dog {
        var $name;
        var $age;
        var $owner;
    
        function dog($in_name="unnamed",$in_age="0",$in_owner="unknown") {
            $this->name = $in_name;
            $this->age = $in_age;
            $this->owner = $in_owner;
        }
    
        function getage() {
            return ($this->age * 365);
        }
        
        function getowner() {
            return ($this->owner);
        }
        
        function getname() {
            return ($this->name);
        }
    }
    //实例化这个类
    $ourfirstdog = new dog("Rover",12,"Lisa and Graham");
    //用serialize函数将这个实例转化为一个序列化的字符串
    $dogdisc = serialize($ourfirstdog);
    print $dogdisc; //$ourfirstdog 已经序列化为字符串 O:3:"dog":
    //3:{s:4:"name";s:5:"Rover";s:3:"age";i:12;s:5:"owner";s:15:"Lisa and Graham";}
    //3:代表三个元素
    //s:4:"name" -->第一个元素 s:4代表4个字母的name
    //s:5:"Rover" --> 第二个元素 s:5:"Rover" 代表5个字符的Rover
    //i:12 后面i:12;也应该猜得到是整数(integer)12。 print '<BR>'; /* ----------------------------------------------------------------------------------------- 在这里你可以将字符串 $dogdisc 存储到任何地方如 session,cookie,数据库,php文件 ----------------------------------------------------------------------------------------- */ //我们在此注销这个类 unset($ourfirstdog); /* 还原操作 */ /* ----------------------------------------------------------------------------------------- 在这里将字符串 $dogdisc 从你存储的地方读出来如 session,cookie,数据库,php文件 ----------------------------------------------------------------------------------------- */ //我们在这里用 unserialize() 还原已经序列化的对象 $pet = unserialize($dogdisc); //此时的 $pet 已经是前面的 $ourfirstdog 对象了 //获得年龄和名字属性 $old = $pet->getage(); $name = $pet->getname(); //这个类此时无需实例化可以继续使用,而且属性和值都是保持在序列化之前的状态 print "Our first dog is called $name and is $old days old<br>"; print '<BR>'; ?>


     3.magic_quotes_gpc()函数

    返回一个具有变量类型和结构的字符串表达式

    magic_quotes_gpc函数在php中的作用是判断解析用户提交的数据,如包括有:post、get、cookie过来的数据增加转义字符“”,以确保这些数据不会引起程序,特别是数据库语句因为特殊字符引起的污染而出现致命的错误

    当magic_quotes_gpc=On的时候,函数get_magic_quotes_gpc()就会返回1
    
    当magic_quotes_gpc=Off的时候,函数get_magic_quotes_gpc()就会返回0

    因此可以看出这个get_magic_quotes_gpc()函数的作用就是得到环境变量magic_quotes_gpc的值。既然在PHP6中删除了magic_quotes_gpc这个选项,那么在PHP6中这个函数我想也已经不复存在了。

     1 function SQLString($c, $t){
     2  $c=(!get_magic_quotes_gpc())?addslashes($c):$c;
     3  switch($t){
     4   case 'text':
     5    $c=($c!='')?"'".$c."'":'NULL';
     6    break;
     7   case 'search':
     8    $c="'%%".$c."%%'";
     9    break;
    10   case 'int':
    11    $c=($c!='')?intval($c):'0';
    12    break;
    13  }
    14  return $c;
    15 }
     1 function check_input($value)
     2 
     3 {
     4 
     5 // 去除斜杠
     6 
     7 if (get_magic_quotes_gpc())
     8 
     9 {
    10 
    11 $value = stripslashes($value);
    12 
    13 }
    14 
    15 // 如果不是数字则加引号
    16 
    17 if (!is_numeric($value))
    18 
    19 {
    20 
    21 $value = “‘” . mysql_real_escape_string($value) . “‘”;
    22 
    23 }
    24 
    25 return $value;
    26 
    27 }
    28 
    29 $con = mysql_connect(“localhost”, “hello”, “321″);
    30 
    31 if (!$con)
    32 
    33 {
    34 
    35 die(‘Could not connect: ‘ . mysql_error());
    36 
    37 }
    38 
    39 // 进行安全的 SQL
    40 
    41 $user = check_input($_POST['user']);
    42 
    43 $pwd = check_input($_POST['pwd']);
    44 
    45 $sql = “SELECT * FROM users WHERE
    46 
    47 user=$user AND password=$pwd”;
    48 
    49 mysql_query($sql);
    50 
    51 mysql_close($con);
    52 
    53 ?>

     4.dirname() 函数返回路径中的目录部分。

      path 参数是一个包含有指向一个文件的全路径的字符串。该函数返回去掉文件名后的目录名。

    1 <?php
    2 echo dirname("c:/testweb/home.php");
    3 echo dirname("/testweb/home.php");
    4 ?>

    输出:

    c:/testweb
    /testweb

    5.autoload()自动加载函数

     1 /** 
     2  * 文件non_autoload.php 
     3  */ 
     4    
     5 require_once(‘your/proj/inc/A.php’);  
     6 require_once(‘your/proj/inc/B.php’);  
     7 require_once(‘your/proj/inc/C.php’);  
     8    
     9 if (条件A) {  
    10     $a = new A();  
    11     $b = new B();  
    12     $c = new C();  
    13     // … 业务逻辑  
    14 } else if (条件B) {  
    15     $a = newA();  
    16     $b = new B();  
    17     // … 业务逻辑  
    18 }
    /** 
     * 文件autoload_demo.php 
     */ 
    function  __autoload($className) {  
        $filePath = “your/proj/inc/{$className}.php”;  
        if (is_readable($filePath)) {  
            require($filePath);  
        }  
    }  
       
    if (条件A) {  
        $a = new A();  
        $b = new B();  
        $c = new C();  
        // … 业务逻辑  
    } else if (条件B) {  
        $a = newA();  
        $b = new B();  
        // … 业务逻辑  
    }

    两个代码对比下应该不难发现autoload的用法

    6.trigger_error

    trigger_error() 用于在用户指定的条件下触发一个错误消息。它与内建的错误处理器一同使用,也可以与由 set_error_handler() 函数创建的用户自定义函数使用。

    如果指定了一个不合法的错误类型,该函数返回 false,否则返回 true。

    语法

    trigger_error(error_message,error_types)
    参数描述
    error_message 必需。规定错误消息。长度限制为 1024 个字符。
    error_types 可选。规定错误消息的错误类型。 可能的值:
    • E_USER_ERROR
    • E_USER_WARNING
    • E_USER_NOTICE

    例子

    <?php
    $test=2;
    if ($test>1)
    {
    trigger_error("A custom error has been triggered");
    }
    ?>

    输出:

    Notice: A custom error has been triggered
    in C:webfolder	est.php on line 6

     parse_url 

  • 相关阅读:
    part17 一些知识总结
    part16 php面向对象
    part15 php函数
    part14 php foreach循环
    part13 数组排序
    part12 php数组
    part11 php条件语句
    part10 php运算符
    part09 php字符串变量
    part08 php常量
  • 原文地址:https://www.cnblogs.com/subtract/p/4323862.html
Copyright © 2011-2022 走看看