批改状态:合格
老师批语:
<!-- url相关 -->
2.10 parse_str():将查询字符串转为数组,格式:(查询字符串,$变量名)实例源码
<?php$sum = '方法1';// global方式获取外部变量function text():string{global $sum;return 'global' . $sum;};echo text().'<hr>';$sum2 = '方法2';function text2():string{return '超全局变量方法'. $GLOBALS['sum2'];};echo text2().'<hr>';$sum3 = '方法3';$text3 = function()use($sum3) {return '闭包函数用use介绍变量'.$sum3;};echo $text3().'<hr>'; //不管什么函数调用都是要'()'的$sum4 = '方法4';$text4 = fn() =>'箭头函数可以直接使用外部变量'.$sum4;echo $text4().'<hr>';$sum5 = '方法5';$text5 = function($x) {return '纯函数传参使用外部变量'.$x;};echo $text5($sum5).'<hr>'; // 纯函数传参即可function getConfig(){$a = 10;$b = 10;$c = 10;$d = $a * $b + $c;return [$d,$a];};echo print_r(getConfig()) .'<hr>';echo getConfig()[1].'<hr>';echo getConfig()[0].'<hr>';list($first,$second) = getConfig(); // 解构函数的返回的数组;echo $first.'<hr>';echo $second.'<hr>';$strtext = ' www.baidu.com ';echo $strtext.'<hr>';$strtext = trim($strtext);echo $strtext;echo substr($strtext,2,4).'<hr>'; //获取字串echo strstr($strtext,'b').'<hr>'; //获取第一次出现的字符的前半段或者后半段,true区分前后,用于获取文件名或者扩展名;echo strpos($strtext,'b').'<hr>'; // 获取某个字符首次的索引echo str_replace('baidu','qq',$strtext).'<hr>'; //替换字符串中的一段字符,并输出更新后的字符串;echo strlen($strtext).'<hr>';echo rtrim($strtext,'com').'<hr>';echo ltrim($strtext,'www').'<hr>';$strtext = '<li>'.$strtext.'</li>'.'<?php $sum6=2 ?>'.'<script>let a = 1;</script>';echo $strtext.'<hr>';echo strip_tags($strtext).'<hr>';echo str_repeat($strtext,2).'<hr>';// 复制字符串// $sum7 = 0xfff;// echo hex2bin($sum7);$strtext = 'www.baidu.com';print_r(str_split($strtext)) ; // 将字符串转变为数组echo '<hr>';echo substr_count($strtext,'w').'<hr>'; // 计算字符出现的次数echo strtoupper($strtext).'<hr>'; // 转换英文字符串大写;echo strtolower(strtoupper($strtext)).'<hr>'; // 转换英文字符串为小写;//url// print_r($_SERVER) ;echo $_SERVER['QUERY_STRING'].'<hr>';$_SERVER['QUERY_STRING'] = 'a=1&b=2&c=3';echo print_r($_SERVER['QUERY_STRING']).'<hr>' ;parse_str($_SERVER['QUERY_STRING'],$arr); // 将查询字符串转为数组printf('<pre>%s<pre>',print_r($arr,true)) ;echo '<hr>';$url = 'http://phpedu.io/0810/0810zuoye.php?a=1&b=2&c=3';$arr2 = parse_url($url); // 将url字符串地址转为数组;printf('<pre>%s</pre>',print_r($arr2,true));
实例结果
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号