Home  >  Article  >  Backend Development  >  Several PHP code snippets worthy of study by beginners_PHP Tutorial

Several PHP code snippets worthy of study by beginners_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:38:29972browse

Classic loop example



Classic loop example


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


Advanced uses of for


Advanced uses for


/*
​ ** Print necessary description text
*/
Print("How many days until Monday? ");
Print("

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

    print("

");
?>

Simple call of function:



Simple function



Function printBold($inputText) //Define function printBold()
{
              print("" . $inputText . "");         ////Print $inputText
}
Print("This line is not emphasized!
"); //Print the string directly
printBold("This line is aggravated!!!"); //Call function printBold() function
Print("
");
Print("This line is not emphasized!
"); //Print the string directly
?>



Functions with return values ​​


Function with return value



Function makeBold($inputText) //Define function makeBold() function
{
          $boldedText = "";
          $boldedText .= $inputText;
$boldedText .= "
";
           return($boldedText);                                                                                                                                                                                                                          . }
Print("This line is not emphasized!!!
"); //Print the string directly
Print(makeBold("This line has been highlighted!!!") . "
");//Call function makeBold() function
Print("This line is not emphasized!!!
"); //Print the string directly
?>



Function with default parameters


Function with default parameters



function printColored($Text, $Color="black") //Define function
{
Print ("& lt; font color =" $ color "& gt; $ text & lt;/font & gt;"); // Get the content and color of the string
}
PrintColored("This is a black word!"); //Call function
Print("

");
PrintColored("This is a blue word!", "blue"); //Call function
Print("
");
?>


Use the rule 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, */
                                                            /* Can analyze 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 integers                                                                                                                      /* According to relevant mathematical definitions */
                 return("Yes");
            }
}
}
Print("Is 0 an integer?" .
           checkInteger(0) . "
");
Print("7 is

http://www.bkjia.com/PHPjc/486485.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486485.htmlTechArticleClassic loop example HTML HEAD TITLE Classic loop example/TITLE /HEAD BODY ? for($counter = 1; $counter = 6; $counter++) //Loop 6 times { print(Bcounter is $counter/BBR); //Print 6 times} ?...
Statement:
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