首页 > 代码库 > PHP基础

PHP基础

<?php
//定界符定义字符串
/*$str=<<<test
this is a “demo”
hahaha,testestest
test;
//PHP中输出函数echo() print_r() var_dump() print() die() printf() sprintf()
//可以用来调试、输出、遍历函数方法
function fun($b,$c){
$a=$b+$c;
echo $a;
}
sprintf(fun(1,2));
//die(‘world‘);
$a=1;
echo(‘hello‘);
print($a);
echo ‘<br/>‘;
printf($a);
echo ‘<br/>‘;
print "this is a demo";
print("this is a demo");
echo ‘<br/>‘;
if(!print("hello")){
echo "输出失败";
}
//die("一般用于错误判断");
echo"这条语句输出吗?";
echo"yi";
//exit;
echo"231";
$num=100;
printf("%s---%d---%b---%x---%o---%c---%.2f",$num,$num,$num,$num,$num,$num,$num);
echo ‘<br/>‘;
printf("%‘#-5s",$num);
$num=100;
$str=sprintf("%s---%d---%b---%x---%o---%c---%.3f",$num,$num,$num,$num,$num,$num,$num);
echo $str;
$arr=array("zhang",6,5,"haha");
var_dump($arr);
//字符串处理函数
/*$a="s hEllo p";
var_dump($a);
var_dump(ltrim($a));
echo ‘<br/>‘;
var_dump(rtrim($a));
echo ‘<br/>‘;
var_dump(trim($a));
echo ‘<br/>‘;
echo strlen($a);
echo strrev($a);
echo ‘<br/>‘;
echo strtolower($a);
echo strtoupper($a);
echo ‘<br/>‘;
echo ucfirst($a);
echo ucwords($a);
//Trim()函数
$str=" abc ";
echo strlen($str);
echo ‘<br/>‘;
echo strlen(ltrim($str));
echo ‘<br/>‘;
echo strlen(rtrim($str));
echo ‘<br/>‘;
echo strlen(trim($str));
//strrev()函数==>反转字符串保存到新变量,原有字符串不变
$str="hello";
$newstr=strrev($str);
echo $str;
echo $newstr;
//strtolower()&strtoupper()函数==>通常在密码比对时使用,数据统一大小写
$str="HellO";
echo strtolower($str);
echo strtoupper($str);
//ucfirst()&ucwords()函数
$str="this is a demo";
echo ucfirst($str);
echo ucwords($str);
//explode()函数==>用来做分割字符串
$str="this is a demo";
$newstr=explode(" ",$str);
$newstr1=explode(" ",$str,3);
var_dump($newstr);
echo "<br/>";
var_dump($newstr1);
//implode()函数==>把几个小的字符串连接成一个大的字符串
$arr=array("this","is","a","demo");
$str=implode(" ",$arr);
$str1=implode("~",$arr);
echo $str;
echo "<br/>";
echo $str1;
//substr()函数==>通过指定下标指定长度截取字符串
$str="abcdefg";
$newstr=substr($str,2,4);
echo $newstr;
//strstr()函数也可称作strchr()函数==>返回字符串中的第一个某字符串直至结束的字符串
$str="abcdefgcdefgcdef";
echo strstr($str,"cd");
echo "<br/>";
echo strchr($str,"f");
//strrchr()函数==>返回字符串中最后出现的某字符到结束字符的字符串
$str="abcdefgcdefgcdef";
echo strrchr($str,"c");
//strpos()函数==>通过某字符的指定索引查找该字符在字符串中的索引位置
$str="abcdefgcdefgcdef";
echo strpos($str,"g");
echo strpos($str,"c",8);
//strrpos()函数==>查找某字符在字符串中最后出现的索引位置
$str="abcdefgcdefgcdef";
echo strrpos($str,"d");
//str_pad()函数==>通过填补方法、指定字符和规定长度去填补某个字符串
$url="www.raoqi.com";
$loc=str_pad($url,15,"#",STR_PAD_BOTH);//STR_PAD_LEFT、STR_PAD_RITHT、STR_PAD_BOTH)分别表示在左、右、两边填补
echo $loc;
//字符串的高级函数
//strcmp() strcasecmp()==>按字节进行字符串比较
$str1="ahello";
$str2="bhello";
if(strcmp($str1,$str2)==0){
echo $str1."等于".$str2;
}else{
echo $str1."不等于".$str2;
}
echo "<br/>";
$str1="hello";
$str2="HELLO";
if(strcasecmp($str1,$str2)==0){//同样是进行字符串比较,但是strcasecmp()函数不区分字符的大小写
echo $str1."等于".$str2;
}else{
echo $str1."不等于".$str2;
}
//strnatcmp()==>按自然排序法对字符串比较
$str1="12";
$str2="111";
echo strnatcmp($str1,$str2);
//soundex() similar_text()==>字符串的模糊比较
$str1="sun";
$str2="son";
echo soundex($str1);
echo soundex($str2); //俩个输出的发音码是S500
if(soundex($str1)==soundex($str2)){
echo "相等";
}
$str1="helloabc";
$str2="heloxxbc";
echo similar_text($str1,$str2,$s);//输出匹配的个数
echo "<br/>";
echo $s."%"; //输出匹配的百分比
//str_replace()==>用指定的字符去替换字符串中指定的字符
$str="this is a test";
$str1=str_replace("test","demo",$str);
echo $str1;
//str_replace()==>用指定的字符去替换字符串中指定值的字符,数组之间的替换是对应索引的关系

$str="选1、选2、选3、选4";
$arr=array("1","2","3","4");
$arr1=array("a","b","c","d");
echo $str."<br/>";
$str=str_replace($arr,$arr1,$str);
echo $str;
//strtr()==>将字符串中指定的字符翻译成所需要翻译的字符,区分和上面替换函数的区别
$str="this is a test";
$str1=strtr($str,"is","IS");
echo $str1;*/
//其他字符串操作方法
//parse_str()==>对URL字符串的解析
//rawurlencode() urlencode() urldecode()==>URL编码处理函数
//http_build_query()==>构造查询字符串

PHP基础