php 的变量必须用$开头
$myAge
switch有两种语法:
$myAge = 25;
switch($myAge){
case 20:
print "your are age is 20";
break;
case 24:
print "your are age is 24";
break;
case 25:
print "your are age is 25";
break;
default:
print "your are age is not match";
break;
}
$myAge = 25;
switch($myAge):
case 20:
print "your are age is 20";
break;
case 24:
print "your are age is 24";
break;
case 25:
print "your are age is 25";
break;
default:
print "your are age is not match";
break;
endswitch;
?>
$array = array("cheng", "fang", "peng");
$array[2];
$array{2}
unset(array[2]);
$languages = array("HTML/CSS",
"JavaScript", "PHP", "Python", "Ruby");
unset($languages[3]);
foreach($languages as $lang) {
print "$lang
";
}
/>
// 输出前五个偶数
for ($i = 2; $i < 11; $i = $i + 2) {
echo $i;
}
?>
foreach循环是迭代对象里的每一个元素。
$langs = array("JavaScript",
"HTML/CSS", "PHP",
"Python", "Ruby");
foreach ($langs as $lang) {
echo "$lang ";
}
unset($lang);
?>
$headCount = 0;
$flipCount = 0;
while ($headCount < 3) {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
$headCount ++;
echo "H";
}
else {
$headCount = 0;
echo "T";
}
}
echo "It took {$flipCount} flips!
";
?>
$isLoop = true;
$index = 0;
while($isLoop):
if($index > 10){
$isLoop = false;
}
echo $index;
$index++;
endwhile;
?>
先执行do中的代码,然后再执行while循环
$flipCount = 0;
do {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
echo "H";
}
else {
echo "T";
}
} while ($flip);
$verb = "were";
$last = "flips";
if ($flipCount == 1) {
$verb = "was";
$last = "flip";
}
echo "There {$verb} {$flipCount} {$last}!
";
?>
$name = "chengfangpeng";
print strlen($name);
?>
$name = "chengfangpeng";
print substr($name, 5, 10);
?>
$name = "chengfangpeng";
print strtoupper($name);
?>
$name = "CHENGFANGPENG";
print strtolower($name);
?>
$name = "chengfangpeng";
print strpos($name, "fang");
if(strpos($name, "Hello") === false){
print "no match string";
}
?>
print round(M_PI); //3
print round(M_PI, 3);//3.142
?>
//使用rand,strlen,substr函数随机打印你姓名中的一个字母
$name = "chengfangpeng";
$rIndex = rand(0, strlen($name));
print substr($name, $rIndex, 1);
?>