其他分享
首页 > 其他分享> > 常用函数

常用函数

作者:互联网

一、foreach 循环

echo "<br/>";
$arr = array(1=>"23",2=>"234");
foreach ($arr as $k=>$v){
    echo $k;
    echo "<br/>";
    echo $v;
    echo "<br/>";
}
die();

二、递归

function digui($a)
{
    if($a <=1)
    {
        return 1;
    }else{
        return $a * digui($a - 1);
    }
}

echo "<br/>";
echo digui(3);
die();

三、自定义过滤函数

function zc_check($string)
{
        if(!is_array($string)){
            
            if(get_magic_quotes_gpc()){
                return htmlspecialchars(trim($string));
            }else{
                //$string = trim($string)   //去除字符串两边的空白字符
                //$string = htmlspecialchars(htmlspecialchars)  //实体化输出<>等
                //$string = addslashes($string) //转义',"等注入字符
                return addslashes(htmlspecialchars(trim($string)));//相当于执行一遍上面的注释
            }
        }
        foreach ($string as $k => $v) $string[$k] = zc_check($v);
        return $string;
}

if($_REQUEST){
    $_POST = zc_check($_POST);
    $_GET  = zc_check($_GET);
    var_dump($_GET);
    die();
}

四、全局

$a = "Original";
$my_array = array("a" => "Cat", "b" => "Dog", "c" => "Horse");
//$a = "Cat"
//$b = "Dog"
//$c = "Horse"
extract($my_array);    //相当于执行一遍上面的注释
echo "\$a = $a; \$b = $b; \$c = $c";
die();

标签:常用,函数,zc,echo,array,htmlspecialchars,check,string
来源: https://www.cnblogs.com/lnterpreter/p/14121070.html