$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
$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.
$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?
while (condition)code to be executed;
</pre><html><body>
<?php $i=1;while($i<=5)
{ echo "The number is " . $i . "
"; $i++; }
?>
</body></html></pre>
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 (initialization; condition; increment){ code to be executed;}
<html><body><?phpfor ($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>
<html><body><?phpfunction writeMyName() { echo "David Yang"; }writeMyName();?></body></html>
<html><body><?phpfunction 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.
<html><body><?phpfunction 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><?phpfunction 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><?phpfunction add($x,$y) { $total = $x + $y; return $total; }echo "1 + 16 = " . add(1,16);?></body></html>
*1 + 16 = 17
<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.
联系客服