1.作用域
1.PHP不支持块作用域:
<?php
$name = 'lk';
if(true)
{
$age = 200;
echo $name;
}
echo $age;
2.PHP支持函数作用域:
函数内部不能访问函数外部,函数外部不能访问函数内部
/**
* 函数外部访问函数内部
*/
function fun1() : array
{
$name = 'yk';
return [$name,20];
}
list($name,$age) = fun1();
echo $name,$age;
/**
* 函数内部访问函数外部
*/
$namex = 'yk';
$agex = '20';
$sex = 'ana';
function fun2(string $sex)
{
global $name;
echo PHP_EOL . $name .$GLOBALS['age'] . $sex;
}
fun2($sex);
$fun3 = function (string $name) use ($age):string{
return $name ;
};
echo $fun3($namex);
2.字符串函数
1.string与array之间转换
$color = ['red','green'];
echo implode('-',$color);
$string = 'asdf-sa-asd';
echo explode('-',$string);
echo str_split($string,1);
2.字符串截取与查找
$color = ['red','green'];
$string = 'ad,asd,asd ';
echo strstr($string,',');
echo strstr($string,',',true);
echo substr($string,-3,1);
echo strpos($string,',',3);
3.字符串替换:
$class = '\admin\controllers\User';
$path = str_replace('\\','/',$class);
echo $path;
$path = str_replace('\\',DIRECTORY_SEPARATOR,$class);
echo $path;
echo str_replace(['a','s'],'**','asdcf');
//ltrim() trim() rtrim()(删除空格,删除字符);默认删除空格
echo ltrim($string) . ' ' . rtrim($string) . ' ' . trim($string);
echo PHP_EOL . ltrim($string,' a');
//删除字符串中的html,php标签, 防止脚本注入
echo strip_tags('<h2>php.cn</h2><?php echo "Hello" ?>');