数字、浮点、布尔型、字符串和数组、浮点、布尔型是值类型,英文:int、float、bool,这样你知道他们怎么用了.
比如语句:$fa=3.14;
字符串和数组是引用类型,也就是说他们作为地址放在栈里,当重新赋值时,栈里的地址改变方向,原来的指向变没或给回收了,英文:string、array.
比如:$str=”字符串”;$arr=array(“a”=>”数”,”b”=>”组”);// array()是一个数组赋值函数,这样的函数php有一千多个,一般常用的不到200个,我觉得.
字符串操作,实例2:字符串合并、相加,代码如下:
- <?php
- $str = 1;
- echo $str .= "";
- echo "<br>";
- echo $str += "1元";
- echo "<br>";
- ?>
实例3:字符串改变大小写,代码如下:
- <?php
- $str="12345abc";
- echo strtolower($str);
- echo "<br>";
- echo strtoupper($str);
- echo "<br>";
- ?>
实例4:字符串长度、截取子字符串(中英文),代码如下:
- <?php
- $str = "字符串2";
- echo mb_strlen($str, "utf-8");
- echo "<br>";
- echo mb_substr($str, 1, 2, "utf-8");
- echo "<br>";
-
-
-
-
- function my_mb_strlen($str, $code = "utf-8")
- {$num= 0;
- if ($code == "utf-8")
- {
- $str = iconv("utf-8", "gb2312", $str);
- for($i = 0;$i < strlen($str);$i++)
- {
- if (ord($str[$i]) > 0xa0)$i++;
- $num++;
- }
- }
- else
- {
- $num = "编码未实现";
- }
- return $num;
- }
- echo my_mb_strlen($str) . ";" . my_mb_strlen($str, "gb2312") . "<br>";
- ?>
实例5:子字符串查找、替换,代码如下:
- <?php
- $str = "字符串4";
- echo mb_strpos($str, '串4', 0, "utf-8");
- echo "<br>";
- echo mb_strstr($str, '串', 0, "utf-8");
- echo "<br>";
- echo str_replace("4", "不是4", $str) ;
- echo "<br>";
- ?>
实例6:子字符串去空、html转义,代码如下:
- <?php
- $str=" 字符串5 ";
- echo $str=trim($str);
- echo "<br>";
- echo "color="red"";
- echo "<br>";
- $str="<br>123";
- echo htmlentities($str) ;
- echo "<br>";
- ?>
|