Classic loop example
Classic loop example
for($counter = 1; $counter <= 6; $counter++) //Loop 6 times
{
print("
counter is $countern");
}
?>
Advanced use of for
Advanced uses for
/*
** Print necessary description text
*/
Print("
How many days until Monday?n");
Print("
n");
for($currentDate = date("U"); //Define $currentDate time format
Date("l", $currentDate) != "Monday"; //Determine whether the current system time is Monday
$ Currentdate+= (60*60*24)) // Add 1 day
{
/*
** Print time name
*/
print("- " . date("l", $currentDate) . "n");
}
Print("
n");
?>
Simple call of function:
Simple function
Function printBold($inputText) //Define function printBold()
{
Print
}
Print("This line is not emphasized!
n"); //Print the string directly
printBold("This line is aggravated!!!"); //Call function printBold() function
Print("
n");
Print("This line is not emphasized!
n"); //Print the string directly
?>
Function with return value
Function with return value
Function makeBold($inputText) //Define function makeBold() function
{
$boldedText = "";
$boldedText .= $inputText;
$boldedText .= "";
return($boldedText); .}
Print("This line is not emphasized!!!
n"); //Print the string directly
Print(makeBold("This line is emphasized!!!") . "
n");//Call function makeBold() function
Print("This line is not emphasized!!!
n"); //Print the string directly
?>
Function with default parameters
Function with default parameters
Function printColored($Text, $Color="black") //Define function function
{
Get the content and color of the string
}
printColored("This is a black word!"); //Call function
Print("
n");
printColored("This is a blue word!", "blue"); //Call function
Print("
n");
?>
Use recursive algorithm to determine whether it is an integer
Judge integers
Function checkInteger($Number)
{
If($Number > 1)
{
/* An integer minus 1 is still an integer */
return(checkInteger($Number-1));
}
elseif($Number < 0)
{
/* For a negative number, */
to /* can be analyzed for its absolute value*/
return(checkInteger((-1)*$Number-1));//Take the absolute value and analyze negative numbers as integers
}
else
{
If(($Number > 0) AND ($Number < 1))
Return("Of course not");
else
/*0 and 1 are integer*/
/*According to the relevant mathematical definition*/
Return("Yes");
}
}
Print("Is 0 an integer?" .
checkInteger (0) . "
n");
Print("Is 7 an integer? " .
checkInteger(7) . "
n");
print("What about 3.5?" . checkInteger(3.5) . "
n");
print("What about -5?" . checkInteger(-5) . "
n");
print("And -9.2?" . checkInteger(-9.2) . "
n");
?>
Initialize array
Initializing array
$monthName = array(1=>"January", "February", "March",//Initialize an array
"April", "May", "June", "July", "August",
"September", "October", "November", "December");
Print(""May" in English is $monthName[5] .
n");//Print the 6th element in the array
?>
Get the elements in the array
Get elements in the array
$monthName = array(
/*Define $monthName[1] to $monthName[12]*/
1=>"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
/*Define $monthName["Jan"] to $monthName["Dec"]*/
"Jan"=>"January", "Feb"=>"February",
"Mar"=>"March", "Apr"=>"April",
"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 5 is " . $monthName[5]. "
n");
print("Month Aug is " . $monthName["Aug"] . "
n");
print("Month June is " . $monthName["June"] . "
n");
?>