今天学习的内容如下:对字符串的操作,if else语句,swicth语句,数组的创建以及遍历,关联数组。
echo输出:字符串1.字符串2 中间可以用·来连接字符串。另外我们可以对字符串做一些相对应的操作,比如获取字符串的长度和字符所在的位置。
$txt1 = "123";
$txt2 = "456";
//拼接字符串
$txt = $txt1 . $txt2;//{有了字符串的连接,我们就可以用.来连接字符 不需要用以前的,""}
echo $txt;
//拿到字符串长度
echo "<br>字符串长度:".strlen($txt);
//strlen() 常常用在循环和其他函数中,因为那时确定字符串何时结束是很重要的。(例如,在循环中,我们需要在字符串中的最后一个字符之后结束循环。)
//查找字符所在位置
echo "<br>4字符所在位置:".strpos($txt, "4");
if else则与Android的基本一样的:
if ($txt1 > $txt2) {
print '对';
} else {
print '不对';
}
switch也是:
switch ($txt1) {
case "123" :
echo "---123---"."<br>";
break;
case "456" :
echo "---456---"."<br>";
break;
case "789" :
echo "---789---"."<br>";
break;
default :
break;
}
数组的创建之前就有讲过,如何遍历呢?
//创建数组并遍历
$array=array("Tom","Mary","Lucy");
//遍历
for ($index = 0; $index < sizeof($array); $index++) {
echo $array[$index],"<br>";
}
java中的foreach遍历-拿到位置取得数值。
//创建关联数组
$arrays=array("Tom"=>1,"Mary"=>2,"Lucy"=>3);
echo $array[0]." Is ".$arrays['Tom'];
关联数组,有2个 Tom 1 类似与Map 当我们取到arrays['Tom']则对应的1值。
完整代码如下:
<?php
/*
*字符串变量
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*
*/
$txt1 = "123";
$txt2 = "456";
//拼接字符串
$txt = $txt1 . $txt2;//{有了字符串的连接,我们就可以用.来连接字符 不需要用以前的,""}
echo $txt;
//拿到字符串长度
echo "<br>字符串长度:".strlen($txt);
//strlen() 常常用在循环和其他函数中,因为那时确定字符串何时结束是很重要的。(例如,在循环中,我们需要在字符串中的最后一个字符之后结束循环。)
//查找字符所在位置
echo "<br>4字符所在位置:".strpos($txt, "4");
$isset = isset ($txt1) ? $txt1 : 'nobody';
echo "<br>", $isset;
echo "<br>". "-------------学习if else switch------------"."<br>";
if ($txt1 > $txt2) {
print '对';
} else {
print '不对';
}
echo "<br>时间:", date("Y"), "<br>";
switch ($txt1) {
case "123" :
echo "---123---"."<br>";
break;
case "456" :
echo "---456---"."<br>";
break;
case "789" :
echo "---789---"."<br>";
break;
default :
break;
}
//创建数组并遍历
$array=array("Tom","Mary","Lucy");
//遍历
for ($index = 0; $index < sizeof($array); $index++) {
echo $array[$index],"<br>";
}
//创建关联数组
$arrays=array("Tom"=>1,"Mary"=>2,"Lucy"=>3);
echo $array[0]." Is ".$arrays['Tom'];
?>
点此运行..