Classic loop example
<HTML>
<Head>
<Title> classic loop example </title>
</Head>
<Body>
<?
For ($ counter = 1; $ counter <= 6; $ counter ++) // 6 cycles
{
Print ("<B> counter is $ counter </B> <br> \ n"); // print 6 times
}
?>
</Body>
</Html>
Advanced Application of
<HTML>
<Head>
<Title> advanced application of for </title>
</Head>
<Body>
<?
/*
** Print necessary instructions
*/
Print ("<B> How many days are there from Monday? </B> \ n ");
Print ("<ol> \ n ");
For ($ currentdate = date ("U"); // defines the $ currentdate time format
Date ("L", $ currentdate )! = "Monday"; // determines whether the current system time is Monday.
$ Currentdate + = (60*60*24) // Add 1 day to the current time
{
/*
** Print the time name
*/
Print ("<li>". date ("L", $ currentdate). "\ n ");
}
Print ("</OL> \ n ");
?>
</Body>
</Html>
Simple function call:
<HTML>
<Head>
<Title> simple functions </title>
</Head>
<Body>
<Font size = 5>
<?
Function printbold ($ inputtext) // defines the function printbold ()
{
Print ("<B>". $ inputtext. "</B>"); // Print $ inputtext
}
Print ("this line is not aggravated! <Br> \ n "); // print the string directly.
Printbold ("this line is aggravated !!! "); // Call the function printbold () function
Print ("<br> \ n ");
Print ("this line is not aggravated! <Br> \ n "); // print the string directly.
?>
</Font>
</Body>
</Html>
Functions with return values
<HTML>
<Head>
<Title> Functions with returned values </title>
</Head>
<Body>
<Font size = 5>
<?
Function makebold ($ inputtext) // defines the function makebold () function
{
$ Boldedtext = "<B> ";
$ Boldedtext. = $ inputtext;
$ Boldedtext. = "</B> ";
Return ($ boldedtext); // return the variable $ boldedtext
}
Print ("this line is not aggravated !!! <Br> \ n "); // print the string directly.
Print (makebold ("this line is aggravated !!! ")." <Br> \ n "); // call the function makebold () function
Print ("this line is not aggravated !!! <Br> \ n "); // print the string directly.
?>
</Size>
</Body>
</Html>
Functions with default parameters
<HTML>
<Head>
<Title> Functions with default parameters </title>
</Head>
<Body>
<Font size = 5>
<?
Function printcolored ($ text, $ color = "black") // defines the function.
{
Print ("<font color = \" $ color \ "> $ text </font>"); // obtain the content and color of the string.
}
Printcolored ("this is a black color word! "); // Call the Function
Print ("<br> \ n ");
Printcolored ("This is a blue color word! "," Blue "); // call the Function
Print ("<br> \ n ");
?>
</Size>
</Body>
</Html>
Rules usedAlgorithmJudge whether it is an integer
<HTML>
<Head>
<Title> judge integer </title>
</Head>
<Body>
<?
Function checkinteger ($ number)
{
If ($ number> 1)
{
/* The integer minus 1 is still an integer */
Return (checkinteger ($ number-1 ));
}
Elseif ($ number <0)
{
/* For a negative number ,*/
/* You can analyze its absolute value */
Return (checkinteger (-1) * $ number-1); // obtain the absolute value and analyze the negative number by an integer.
}
Else
{
If ($ number> 0) and ($ number <1 ))
{
Return ("of course not ");
}
Else
{
/* 0 and 1 are integers */
/* According to the mathematical definition */
Return ("yes ");
}
}
}
Print ("<B> 0 is an integer? </B> ".
Checkinteger (0). "<br> \ n ");
Print ("<B> 7 is an integer? </B> ".
Checkinteger (7). "<br> \ n ");
Print ("<B> what about 3.5? </B> ". checkinteger (3.5)." <br> \ n ");
Print ("<B> what about-5? </B> ". checkinteger (-5)." <br> \ n ");
Print ("<B> and-9.2? </B> ". checkinteger (-9.2)." <br> \ n ");
?>
</Body>
</Html>
Initialize an array
<HTML>
<Head>
<Title> Initialize an array </title>
</Head>
<Font size = 5>
<?
$ Monthname = array (1 => "January", "February", "March", // Initialize an array
"Maid", "may", "June", "July", "August ",
"September", "October", "November", "December ");
Print ("May" in English is <B> $ monthname [5] </B>. <Br> \ n "); // print the 6th elements in the array
?>
</Font>
</Body>
</Html>
Obtain the elements in the array.
<HTML>
<Head>
<Title> retrieve elements in the array </title>
</Head>
<?
$ Monthname = array (
/* Define $ monthname [1] to $ monthname [12] */
1 => "January", "February", "March ",
"Maid", "may", "June ",
"July", "August", "September ",
"October", "November", "December ",
/* Define $ monthname ["Jan"] to $ monthname ["dec"] */
"Jan" => "January", "FEB" => "February ",
"Mar" => "March", "APR" => "yml ",
"May" => "may", "Jun" => "June ",
"Jul" => "July", "Aug" => "August ",
"Sep" => "September", "Oct" => "October ",
"Nov" => "November", "dec" => "December ",
/* Define $ monthname ["Jan"] to $ monthname ["dec"] */
"January" => "January", "February" => "February ",
"March" => "March", "April" => "April ",
"May" => "may", "June" => "June ",
"July" => "July", "August" => "August ",
"September" => "September", "October" => "October ",
"November" => "November", "December" => "December"
);
/* Print related elements */
Print ("month <B> 5 </B> is <B>". $ monthname [5]. "</B> <br> \ n ");
Print ("month <B> Aug </B> is <B>". $ monthname ["Aug"]. "</B> <br> \ n ");
Print ("month <B> June </B> is <B>". $ monthname ["June"]. "</B> <br> \ n ");
?>
</Body>
</Html>
Create a multi-dimensional array
<HTML>
<Head>
<Title> Create a multi-dimensional array </title>
</Head>
<?
$ Cities = array (// two-dimensional array ()
"North China" => array (
"Beijing ",
"Tianjin ",
"Shijiazhuang"
),
"Northwest China" => array (
"Xi'an ",
"Lhasa"
)
);
Print ("North China:". $ cities ["North China"] [0]); // Print $ cities ["North China"] [0]
?>
</Body>
</Html>