Several classic codes that must be collected when learning PHP Page 1/2_PHP Tutorial

WBOY
Release: 2016-07-21 15:52:43
Original
797 people have browsed it

Classic loop example



Classic loop example


for($counter = 1; $counter <= 6; $counter++) //Loop 6 times
{
print("counter is $counter< /B>
n"); //Print 6 times
} }
?>




Advanced use of for


Advanced use of for


/*
** Print necessary description
*/
print("How many days until Monday?n ");
print("

    n");
    for($currentDate = date("U"); //Define $currentDate time format
    date("l", $currentDate ) != "Monday"; //Judge whether the current system time is Monday
    $currentDate += (60 * 60 * 24)) //Current time plus 1 day
    {
    /*
          ** Print time name                                                                                              ("
n");
?>



Simple call of function:



Simple function


function printBold($inputText) //Define function printBold()
{
print("" . $inputText . "") ; ////Print $inputText
}
print("This line is not emphasized!
n"); //Print the string directly
printBold("This line is getting heavier!!!"); //Call the function printBold() function
print("
n" );
print("This line is not emphasized!
n"); 🎜>
Function with return value


Function with return value
< ;/HEAD>


function makeBold($inputText) //Define function makeBold() function
{
$boldedText = "";
$boldedText .= $inputText;
$boldedText .= "
";
return($boldedText); //Return variable $ 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 aggravated!!!
n"); //Print the string directly
?>




Functions with default parameters


Functions with default parameters


function printColored($Text, $Color="black") //Define function function
{
print("< FONT COLOR="$Color">$Text
"); //Get the content and color of the string
}
printColored("This is a black word!"); //Call function function
print("

n");
printColored("This is a blue word!", "blue"); //Call function
print("
n");
?>




The algorithm used to determine whether it is an integer

;


function checkInteger($Number)
{
                                                                                                                                                                                                                                                            {
                                           * For a negative number, */
              /* You can analyze its absolute value*/                                                                                                            
                                                                                                                                          return("Of course not");
                                                                                                                   Mathematical definition */
     return("Yes"); >                                                                                                                                                                                                       "Is 7 an integer? " .
checkInteger(7) . "
n");
print("3.5?" . checkInteger(3.5) . "
n");
print("What about -5?" . checkInteger(-5) . "
n");
print( "And -9.2?" . checkInteger(-9.2) . "
n");
?>

< ;/HTML>
Initialization array



Initialization array


$monthName = array(1=>"January", "February", "March",//Initialize an array
"April", "May", "June", "July", "August",
"September", "October", "November", "December");
print(" "May" in English is< ;B> $monthName[5]
.
n");//Print the 6th element in the array
?>



Get the elements in the array


Get the 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"] . "< ;/B>
n");
?>


Create a multidimensional array


Create a multi-dimensional array

"North China" =>array(
"Beijing",
"Tianjin",
"Shijiazhuang"
),
"Northwest Region" => Array ( "Xi'an",
"Lhasa"
)
);
Print ("North China:". $ Cities ["North China"]; // Print $Cities["North China"][0]
?>



www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/318860.htmlTechArticleClassic loop example HTML HEAD TITLE Classic loop example/TITLE /HEAD BODY ? for($counter=1;$counter =6;$counter++)//Loop 6 times { print("Bcounteris$counter/BBRn");//Print 6 times} ? /BODY...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template