Several PHP code segments worth learning by beginners

Source: Internet
Author: User
Classic loop example HTMLHEADTITLE classic loop example/TITLE/HEADBODY? For ($ counter = 1; $ counter = 6; $ counter ++) // Loop 6 Times {print (Bcounteris $ counter/BBR \ n ); // Print 6 times }? /BODY/HTMLf

Classic loop example



Classic loop example


For ($ counter = 1; $ counter <= 6; $ counter ++) // 6 cycles
{
Print ("Counter is $ counter
\ N "); // Print 6 times
}
?>


Advanced application of


Advanced application of


/*
** Print necessary instructions
*/
Print ("How many days are there from Monday?\ N ");
Print ("

    \ 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 ("
  1. ". Date (" l ", $ currentDate)." \ n ");
    }

    Print ("

\ N ");
?>

Simple function call:



Simple functions



Function printBold ($ inputText) // defines the function printBold ()
{
Print ("". $ InputText .""); // Print $ inputText
}
Print ("This line is not aggravated!
\ N "); // Print the string directly
PrintBold ("This line is aggravated !!! "); // Call the function printBold () function
Print ("
\ N ");
Print ("This line is not aggravated!
\ N "); // Print the string directly
?>



Functions with return values


Functions with return values



Function makeBold ($ inputText) // defines the function makeBold () function
{
$ BoldedText ="";
$ BoldedText. = $ inputText;
$ BoldedText. ="
";
Return ($ boldedText); // return the variable $ boldedText
}
Print ("This line is not aggravated !!!
\ N "); // Print the string directly
Print (makeBold ("This line is aggravated !!! ")."
\ N "); // call the 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") // defines the function.
{
Print ("$ Text"); // Obtain the content and color of the string.
}
PrintColored ("This is a black color word! "); // Call the function
Print ("

\ N ");
PrintColored ("This is a blue color word! "," Blue "); // call the function
Print ("
\ N ");
?>


Use a regular algorithm to determine whether it is an integer



Judge integer


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 ("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 ");
?>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.