初學者學習PHP開發應該掌握的幾段精華代碼_PHP教程

來源:互聯網
上載者:User

經典迴圈例子



經典迴圈例子


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 printColored($Text, $Color="black") //定義function函數
{
print("$Text"); //擷取字串的內容和顏色
}
printColored("這是黑顏色的字!"); //調用function函數
print("

");
printColored("這是藍顏色的字!", "blue"); //調用function函數
print("
");
?>


用的規演算法判斷是否是整數



判斷整數


function checkInteger($Number)
{
if($Number > 1)
{
/* 整數減1仍然是整數 */
return(checkInteger($Number-1));
}
elseif($Number < 0)
{
/* 對於一個負數,*/
/* 可以分析它的絕對值*/
return(checkInteger((-1)*$Number-1));//取絕對值,把負數按整數分析
}
else
{
if(($Number > 0) AND ($Number < 1))
{
return("當然不是");
}
else
{
/* 0 和 1 是整數 */
/* 根據相關數學定義 */
return("是的");
}
}
}
print("0是整數嗎?" .
checkInteger(0) . "
");
print("7是整數嗎? " .
checkInteger(7) . "
");
print("3.5呢?" . checkInteger(3.5) . "
");
print("那麼-5呢?" . checkInteger(-5) . "
");
print("還有-9.2?" . checkInteger(-9.2) . "
");
?>


初始化數組



初始化數組


$monthName = array(1=>"January", "February", "March",//初始化一個數組
"April", "May", "June", "July", "August",
"September", "October", "November", "D

http://www.bkjia.com/PHPjc/486620.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486620.htmlTechArticle經典迴圈例子 HTML HEAD TITLE經典迴圈例子/TITLE /HEAD BODY ? for($counter = 1; $counter = 6; $counter++) //迴圈6次 { print("Bcounter is $counter/BBR"); //列印6次 }...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.