当前位置:首页 > Web开发 > 正文

则返回FALSE $str1 = ‘javascript‘ ; $str2 = ‘A‘ ; var_dump ( str

2024-03-31 Web开发

$n = 5; //全局变量 function fun1(){ global $n; echo ‘我在函数体内也可以挪用全局变量n,它的值是:‘ , $n;//5 $n++; } fun1(); echo ‘<hr>‘; echo $n;//6

$n = 6; function fun1(){ echo ‘变量的值是:‘ , $GLOBALS[‘n‘]; $GLOBALS[‘n‘]++; } fun1(); echo $GLOBALS[‘n‘];

不使用循环语句,来计算1~100的和

function recursive($n){ if($n>=1){ return $n + recursive($n-1); } } echo recursive(100);

引用

$foo = ‘Bob‘; $bar = &$foo; //对待成变量的别号 $bar = ‘Rose‘; echo $foo;//Rose $foo = ‘Mooc‘; $bar = &$foo; //对待成变量的别号 unset($foo); //变量销毁 echo $bar;//Mooc

自界说函数

function fun1(&$n){ $n++; echo ‘我是函数体内的局部变量‘ , $n ;//4 } $n = 3; fun1($n); echo $n , ‘<hr>‘;//4

获得扩展名

function getExtension($filename) { $pos = strrpos($filename, ‘.‘); $extension = strtolower(substr($filename, $pos + 1)); return $extension; } $path = ‘mooc.func.pHP‘; var_dump(getExtension($path));

求平均数

function avg(...$args) { return $args; } var_dump(avg(1, 2, 3));

系统函数库

字符串转数组

$str = ‘A|B|C|D‘; $arr = explode(‘|‘, $str); print_r($arr);//[A,B,C,D]

数组转字符串

$arr2 = array(‘Tom‘,‘John‘,‘Rose‘); $str2 = implode(‘,‘,$arr2); echo $str2;//Tom,John,Rose

获取扩展名:
要领一

$filename = ‘ab.cd.gif.JpEg‘; //gepj.fig.dc.ba $num = strrpos($filename, ‘.‘); echo strtolower(substr($filename, $num+1)) , ‘<br/><br/>‘;//jpeg

要领二

$filename = ‘ab.cd.gif.JpEg‘; //gepj.fig.dc.ba $str2 = strrev($filename);//strrev反转字符串 $num = strpos($str2, ‘.‘); echo strtolower(strrev(substr($str2, 0,$num)));//jpeg

trim移除字符串两侧的字符

$str = "\n\n\t\tABC\t\t"; echo trim($str);//ABC

md5()加密

$str = ‘abc‘; echo md5($str);//900150983cd24fb0d6963f7d28e17f72

格局化字符串

$number = 5; $str = ‘shanghai‘; $txt = sprintf(‘there are %d million cars in %s‘,$number,$str); echo $txt;//there are 5 million cars in shanghai $number = 123; $txt = sprintf("带有两位小数的功效是:%1\$.2f,\n不带小数的是:%1\$d",$number); echo $txt;//带有两位小数的功效是:123.00,不带小数的是:123

htmlspecialchars特殊字符转为HTML实体

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/31903.html