首页 > 代码库 > classic example2

classic example2

function kg(){
echo "<br/>";
}
//9.将字母转换成大小写
echo ucfirst("how do you do today?"); //字符串首字母大写,只能是第一个字母,第一个不是字母就不行
kg();
echo ucwords("the prince of wales"); //所有单词首字母大写
kg();

结果: How do you do today?

The Prince Of Wales

//10.改变字符串的大小写形式
echo strtoupper("i‘m not yelling!");
kg();
echo strtolower(‘WHAT A HAPPY DAY!‘);
kg();

 结果:I‘M NOT YELLING!
what a happy day!

//11.ltrim()用于删除字符串开始处的空白符,rtrim()用于删除字符串结尾处的空白符,trim()可以同时删除字符串开始和结尾处的空白符
$ssss = " / trimmmmm \ ";
echo ltrim($ssss);
kg();
echo rtrim($ssss);
kg();
echo trim($ssss);
kg();
//仅在源码中可见,网页上不可见

结果:

/ trimmmmm \ 
/ trimmmmm \
/ trimmmmm \

//12.用一个固定字符串作为分隔符来分割片段.
$words = explode(‘ ‘,‘my sentence is not very complicated.‘);
print_r($words);
kg();

结果:Array ( [0] => my [1] => sentence [2] => is [3] => not [4] => very [5] => complicated. ) 

//13.对一个字符串自动换行
$s = "four score and seven years ago our fathers brougth forth on this continent a new nation, conveived in liberty and dedicated to the proposition that all men are created equal.";

echo "<pre>\n".wordwrap($s,50,"\n\n")."\n</pre>"; //默认换行为75,可以使用第二参数修改,第三个参数用来修改换行符

结果:

four score and seven years ago our fathers brougthforth on this continent a new nation, conveived inliberty and dedicated to the proposition that allmen are created equal.

//14.比较浮点型数字
$delta = 0.000001;

$a = 1.000000001;
$b = 1.000000000;

if(abs($a-$b)<$delta) {
echo ‘$a = $b‘;
}
kg();

结果:$a = $b

//15.对浮点数取整
$number = round(-2.4); //最接近取整,四舍五入
$number2 = ceil(2.4); //向上取整
$number3 = floor(2.4); //向下取整
echo $number;
kg();
echo $number2;
kg();
echo $number3;
kg();

结果:

-2
3
2

 

//16.给定数字范围生成随机数
$random = mt_rand(1,100);
echo $random;
kg();

 

结果:28

classic example2