Home >Backend Development >PHP Tutorial >Several essential pieces of code that beginners should master when learning PHP development_PHP Tutorial

Several essential pieces of code that beginners should master when learning PHP development_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:58756browse

经典循环例子



经典循环例子


for($counter = 1; $counter <= 6; $counter++) //循环6次
{
print("counter is $counter
");    //打印6次
    }   
?>


for的高级运用


for的高级运用


/*
** 打印必要的说明文字
*/
print("距离星期一还有几天? ");
    print("

    ");
        for($currentDate = date("U");             //定义$currentDate时间格式
            date("l", $currentDate) != "Monday";     //判断是不是当前系统时间是Monday
            $currentDate += (60 * 60 * 24))        //当前时间加上1天
        {
            /*
            ** 打印时间名称
            */
            print("
  1. " . date("l", $currentDate) . " ");
        }

        print("

");
?>

函数的简单调用:



简单的函数



function printBold($inputText) //定义function printBold()
{
print("" . $inputText . "");    ////打印$inputText
    }
    print("这行没有加重!
");            //直接打印字符串
    printBold("这行加重了!!!");            //调用function printBold()函数
    print("
");
    print("这行没有加重!
");            //直接打印字符串
?>



有返回值的函数


有返回值的函数



function makeBold($inputText) //定义function makeBold()函数
{
$boldedText = "";
        $boldedText .= $inputText;
        $boldedText .= "
";
        return($boldedText);        //返回变量$boldedText
    }
    print("这行没有加重!!!
");    //直接打印字符串   
    print(makeBold("这行被加重了!!!") . "
");//调用function makeBold()函数
    print("这行没有加重!!!
");    //直接打印字符串
?>


Function with default parameters


Function with default parameters



function printColored($Text, $Color="black") //Define function function
{
print( "$Text"); //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 integer


function checkInteger($Number)
{
if($Number > 1)
{
/* An integer minus 1 is still an integer*/
return(checkInteger($ Number- 1));
}
elseif($Number < 0)
{
return(checkInteger((-1)*$Number-1));//Take the absolute value and analyze negative numbers as integers
                                                                                                                                                                  ) AND ($Number < 1))
                                                                                                                                                                                                                       /* 0 and 1 are integers                                                                                         🎜>                                                                                                                                                                                             ; Is 0 an integer? < /B>" .
                                                                                                                                                                                                                                       checkInteger(0)                                     ");
print("Is 7 an integer? " .
checkInteger(7) . "
");
print("What about 3.5?" . checkInteger(3.5) . "
");
print("What about -5?" . checkInteger(-5) . "
");
print("And -9.2?" . checkInteger(-9.2) . "
");
?>


Initialization array



Initialization array


< ;?
$monthName = array(1=>"January", "February", "March",//Initialize an array
"April", "May", "June", "July", "August",
"September", "October", "November", "D

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486620.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