PHP快速入门(1)
时间:2010-10-18 来源:Aixe
# 注释
<?php
// 介是单行注释
/* 介是多行注释... */
echo "<p>This is something about php </p>";
?>
$str1 = "You will see: ";
$str2 = "How to concat strings.";
echo "<p>".$str1.$str2."</p>";
?>
$flag = true;
if($flag)
{
echo "<p>Under the condition...</p>";
}
else
{
echo "<p>Under other conditions...</p>";
}
$val = 100;
if($val == 0)
{
echo "<p>It is zero.</p>";
}
elseif($val < 0)
{
echo "<p>It is a negative.</p>";
}
else
{
echo "<p>It is a positive.</p>";
}
?>
$case = 10;
$result = "";
switch($case)
{
case 1:
$result = "In case one.";
break;
case 2:
$result = "In case two.";
break;
case 3:
$result = "In case three.";
break;
default:
$result = "In other case.";
break;
}
echo "<p>".$result."</p>";
?>
$val = 1;
$exp = ($val == 0) ? "<p>The value is zero</p>" : "<p>The value is not zero</p>";
echo $exp;
?>
echo "<ul>";
$arr1 = array("John", "Jane", "Julie");
echo "<li>People in array #1: $arr1[0], $arr1[1], $arr1[2]</li>";
$arr2 = array(0 => "Jodan", 1 => "Jack", 2 => "James");
echo "<li>People in array #2: $arr2[0], $arr2[1], $arr2[2]</li>";
$arr3 = array(1 => "Tom", "Sam", "Kim");
echo "<li>People in array #3: $arr3[1], $arr3[2], $arr3[3]</li>";
$arr4[3] = "Avril";
$arr4[2] = "Taylor";
$arr4[1] = "Miley";
echo "<li>People in array #4: $arr4[1], $arr4[2], $arr4[3]</li>";
echo "</ul>";
?>
$fib[0] = 1;
$fib[1] = 1;
for($i = 2; $i < 10; $i++)
{
$fib[$i] = $fib[$i-1] + $fib[$i-2];
}
echo "<p>The Fibonacci: ";
foreach($fib as $index => $value)
{
echo (string)$value." ";
}
echo "</p>";
?>
$n = 6;
$c = 1;
$r = 1;
while($c <= $n)
{
$r *= $c;
$c += 1;
}
echo "<p>$n != $r </p>";
?>
function factorial($val)
{
if($val == 0) return 1;
return $val * factorial($val - 1);
}
$n = 7;
echo "<p>$n! = ".(string)factorial($n)."</p>";
?>
// 介是单行注释
/* 介是多行注释... */
echo "<p>This is something about php </p>";
?>
# 连接字符串 <?php
$str1 = "You will see: ";
$str2 = "How to concat strings.";
echo "<p>".$str1.$str2."</p>";
?>
# 条件判断 代码 <?php
$flag = true;
if($flag)
{
echo "<p>Under the condition...</p>";
}
else
{
echo "<p>Under other conditions...</p>";
}
$val = 100;
if($val == 0)
{
echo "<p>It is zero.</p>";
}
elseif($val < 0)
{
echo "<p>It is a negative.</p>";
}
else
{
echo "<p>It is a positive.</p>";
}
?>
# 分支结构 代码 <?php
$case = 10;
$result = "";
switch($case)
{
case 1:
$result = "In case one.";
break;
case 2:
$result = "In case two.";
break;
case 3:
$result = "In case three.";
break;
default:
$result = "In other case.";
break;
}
echo "<p>".$result."</p>";
?>
# 三元运算 <?php
$val = 1;
$exp = ($val == 0) ? "<p>The value is zero</p>" : "<p>The value is not zero</p>";
echo $exp;
?>
# 数组array 代码 <?php
echo "<ul>";
$arr1 = array("John", "Jane", "Julie");
echo "<li>People in array #1: $arr1[0], $arr1[1], $arr1[2]</li>";
$arr2 = array(0 => "Jodan", 1 => "Jack", 2 => "James");
echo "<li>People in array #2: $arr2[0], $arr2[1], $arr2[2]</li>";
$arr3 = array(1 => "Tom", "Sam", "Kim");
echo "<li>People in array #3: $arr3[1], $arr3[2], $arr3[3]</li>";
$arr4[3] = "Avril";
$arr4[2] = "Taylor";
$arr4[1] = "Miley";
echo "<li>People in array #4: $arr4[1], $arr4[2], $arr4[3]</li>";
echo "</ul>";
?>
# for循环和foreach 代码 <?php
$fib[0] = 1;
$fib[1] = 1;
for($i = 2; $i < 10; $i++)
{
$fib[$i] = $fib[$i-1] + $fib[$i-2];
}
echo "<p>The Fibonacci: ";
foreach($fib as $index => $value)
{
echo (string)$value." ";
}
echo "</p>";
?>
# while循环 <?php
$n = 6;
$c = 1;
$r = 1;
while($c <= $n)
{
$r *= $c;
$c += 1;
}
echo "<p>$n != $r </p>";
?>
# 函数和递归 <?php
function factorial($val)
{
if($val == 0) return 1;
return $val * factorial($val - 1);
}
$n = 7;
echo "<p>$n! = ".(string)factorial($n)."</p>";
?>
相关阅读 更多 +