數(shù)值數(shù)組
$names = array("Peter","Quagmire","Joe");
$names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe";
<?php $names[0] = "Peter"; $names[1] = "Quagmire"; $names[2] = "Joe"; echo $names[1] . " and " . $names[2] . " are ". $names[0] . "'s neighbors"; ?>
Quagmire and Joe are Peter's neighbors 關(guān)聯(lián)數(shù)組
$ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34);
$ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34";
<?php $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is " . $ages['Peter'] . " years old."; ?>
Peter is 32 years old. 多維數(shù)組
$families = array ( "Griffin"=>array ( "Peter", "Lois", "Megan" ), "Quagmire"=>array ( "Glenn" ), "Brown"=>array ( "Cleveland", "Loretta", "Junior" ) );
Array ( [Griffin] => Array ( [0] => Peter [1] => Lois [2] => Megan ) [Quagmire] => Array ( [0] => Glenn ) [Brown] => Array ( [0] => Cleveland [1] => Loretta [2] => Junior ) )
echo "Is " . $families['Griffin'][2] . " a part of the Griffin family?";
Is Megan a part of the Griffin family? 循環(huán)
while
for
foreach
語法while (condition) code to be executed; 例子
</pre> <html> <body> <?php $i=1; while($i<=5) {
echo "The number is " . $i . "
?> </body> </html> </pre> do...while 語句
do
{
code to be executed;
}
while (condition);
<html>
<body>
<?php
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
?>
</body>
</html>
for 語句
for (initialization; condition; increment)
{
code to be executed;
}
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
?>
</body>
</html>
foreach (array as value)
{
code to be executed;
}
<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body>
</html>
PHP 函數(shù)
創(chuàng)建 PHP 函數(shù)
創(chuàng)建 PHP 函數(shù)
<html>
<body>
<?php
function writeMyName()
{
echo "David Yang";
}
writeMyName();
?>
</body>
</html>
<html>
<body>
<?php
function writeMyName()
{
echo "David Yang";
}
echo "Hello world!<br />";
echo "My name is ";
writeMyName();
echo ".<br />That's right, ";
writeMyName();
echo " is my name.";
?>
</body>
</html>
以上代碼的輸出: Hello world! My name is David Yang. That's right, David Yang is my name. PHP 函數(shù)
<html>
<body>
<?php
function writeMyName($fname)
{
echo $fname . " Yang.<br />";
}
echo "My name is ";
writeMyName("David");
echo "My name is ";
writeMyName("Mike");
echo "My name is ";
writeMyName("John");
?>
</body>
</html>
My name is David Yang. My name is Mike Yang. My name is John Yang.
<html>
<body>
<?php
function writeMyName($fname,$punctuation)
{
echo $fname . " Yang" . $punctuation . "<br />";
}
echo "My name is ";
writeMyName("David",".");
echo "My name is ";
writeMyName("Mike","!");
echo "My name is ";
writeMyName("John","...");
?>
</body>
</html>
My name is David Yang. My name is Mike Yang! My name is John Yang...
<html>
<body>
<?php
function add($x,$y)
{
$total = $x + $y;
return $total;
}
echo "1 + 16 = " . add(1,16);
?>
</body>
</html>
*1 + 16 = 17 PHP 表單處理
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
*"welcome.php" 文件類似這樣: <pre> <html> <body> Welcome <?php echo $_POST["name"]; ?>.<br /> You are <?php echo $_POST["age"]; ?> years old. </body> </html>
Welcome John. You are 28 years old.
表單驗證
|
|
|