$str = 'Hello PHP'; echo $str;
$str = 'Hello PHP'; echo strpos($str,'o'); //计算字符在字符串中的位置 echo '<br/>'; echo strpos($str,'PH');
$str = 'Hello PHP'; //截取字符串 $str1 = substr($str,2,3); //从2位置开始截取,截取长度为3的字符串 echo $str1;不传入长度参数的话,会从指定位置一直截取到字符串的末尾
$str = 'Hello PHP'; //分割字符串 $result = str_split($str); //将结果保存到一个数组中 PRint_r($result); //使用print_r输入一个数组 echo '<br/>'; $result1 = str_split($str,2); print_r($result1);
$str = 'Hello PHP java C# C++'; $result = explode(' ',$str); print_r($result);
$str = 'Hello PHP Java C# C++'; //字符串的连接 $num = 100; $str1 = $str.'<br/>Objective-C '.$num; echo $str1; echo '<br/>'; $str2 = "$str<br/>Objective-C $num"; //另一中简便的写法 echo $str2;
2.PHP数组
//数组的声明方式 $arr = array(); $arr[0] = 'Hello'; $arr[1] = 'World'; $arr[2] = 2; $arr[3] = 3.14; print_r($arr);
$arr = array(); for($i=0;$i<100;$i++){ array_push($arr,'Item'.$i); } print_r($arr);
$arr = array(); $arr['H'] = 'Hello'; $arr['W'] = 'World'; print_r($arr); echo '<br/>'; echo $arr['H'];输出: Array ( [H] => Hello [W] => World ) Hello
$arr = array('H'=>'Hello','W'=>'World'); //PHP数组初始化 print_r($arr);
$arr = array(0=>'Make',1=>'Tom','H'=>'Hello','W'=>'World'); print_r($arr); echo '<br/>'; echo $arr[0]; echo '<br/>'; echo $arr['W'];输出: Array ( [0] => Make [1] => Tom [H] => Hello [W] => World ) Make World 3.include与require
function sayHello(){ echo 'Hello PHP'; }index.php下
require 'lib.php'; //include 'lib.php'; sayHello();
function sayHello(){ echo 'Hello PHP'; } echo 'This is a lib file';index.php下
require_once 'lib.php'; require_once 'lib.php'; echo '<br/>'; sayHello(); echo '<br/>end';输出 This is a lib file Hello PHP end