PHP入門需要掌握的幾種功能代碼_PHP教程

來源:互聯網
上載者:User
經典迴圈例子


經典迴圈例子


for($counter = 1; $counter <= 6; $counter++) //迴圈6次
{
print(" counter is $counter
n"); //列印6次
}
?>



for的進階運用


for的進階運用


/*
** 列印必要的解說文字
*/
print(" 距離星期一還有幾天?n");
print("
    n");
    for($currentDate = date("U"); //定義$currentDate時間格式
    date("l", $currentDate) != "Monday"; //判斷是不是當前系統時間是Monday
    $currentDate += (60 * 60 * 24)) //目前時間加上1天
    {
    /*
    ** 列印時間名稱
    */
    print("
  1. " . date("l", $currentDate) . "n");
    }
    print("
n");
?>



函數的簡單調用:


簡單的函數



function printBold($inputText) //定義function printBold()
{
print(" " . $inputText . ""); ////列印$inputText
}
print("這行沒有加重!
n"); //直接列印字串
printBold("這行加重了!!!"); //調用function printBold()函數
print("
n");
print("這行沒有加重!
n"); //直接列印字串
?>




有傳回值的函數


有傳回值的函數



function makeBold($inputText) //定義function makeBold()函數
{
$boldedText = " ";
$boldedText .= $inputText;
$boldedText .= "
";
return($boldedText); //返回變數$boldedText
}
print("這行沒有加重!!!
n"); //直接列印字串
print(makeBold("這行被加重了!!!") . "
n");//調用function makeBold()函數
print("這行沒有加重!!!
n"); //直接列印字串
?>




有預設參數的函數


有預設參數的函數



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

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




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


判斷整數


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) . "
n");
print(" 7是整數嗎? " .
checkInteger(7) . "
n");
print(" 3.5呢?" . checkInteger(3.5) . "
n");
print(" 那麼-5呢?" . checkInteger(-5) . "
n");
print(" 還有-9.2?" . checkInteger(-9.2) . "
n");
?>

初始化數組


初始化數組


$monthName = array(1=>"January", "February", "March",//初始化一個數組
"April", "May", "June", "July", "August",
"September", "October", "November", "December");
print(" 英語的“5月”是 $monthName[5]
n");//列印數組中的第6個元素
?>




擷取數組中的元素


擷取數組中的元素

$monthName = array(
/*定義$monthName[1]到$monthName[12]*/
1=>"January", "February", "March",
"April", "May", "June",
"July", "August", "September",
"October", "November", "December",
/*定義$monthName["Jan"]到$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",
/*定義$monthName["Jan"]到$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("Month 5 is " . $monthName[5]. "
n");
print("Month Aug is " . $monthName["Aug"] . "
n");
print("Month June is " . $monthName["June"] . "
n");
?>



建立一個多維陣列


建立一個多維陣列

$Cities = array( //二維數組array()
"華北地區"=>array(
"北京市",
"天津市",
"石家莊"
),
"西北地區"=>array(
"西安",
"拉薩"
)
);
print("華北地區: ".$Cities["華北地區"][0]); //列印$Cities["華北地區"][0]
?>



PHP 4.0實現表格狀列印


實現表格狀列印


/*
** 資料表格化
*/
print("













n"); // 表格開始 for($Row=1; $Row <= 12; $Row ++) { print(" n"); // 開始行 // do each column for($Column=1; $Column <= 12; $Column ++) { print(" "); } print(" n"); // 行結束 } print("
");//開始列
print($Row * $Column);//表格元素乘積
print("
n"); // 表格結束
?>



查看系統的一些變數


查看PHP的環境變數


print("你正在用檔案的名字為: ");
print(__FILE__);
print("
n");
print("");
print("你的作業系統為: ");
print(PHP_OS);
print("");
print("你的php的版本為: ");
print(PHP_VERSION)
?>



開啟本地或者遠程檔案


開啟本地或者遠程檔案


print("

通過http協議開啟檔案

n");
// 通過 http 協議開啟檔案
if(!($myFile = fopen("d:web/web/php/test/data.txt", "r")))
{
print("檔案不能開啟");
exit;
}
while(!feof($myFile)) //迴圈
{
// 按行讀取檔案中的內容
$myLine = fgetss($myFile, 255);
print("$myLine
n");
}
// 關閉檔案的控制代碼
fclose($myFile);
?>



開啟檔案的幾種方式比較


讀取檔案內容


// 開啟檔案同時列印檔案的每一個字元
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myCharacter = fgetc($myFile);
print($myCharacter);
}
fclose($myFile);
}
?>

// 開啟檔案同時列印檔案的每一行
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgets($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>

/* 開啟檔案同時列印檔案的每一行,
同時去掉取回字串中的 HTML 語言
*/
if($myFile = fopen("data.txt", "r"))
{
while(!feof($myFile))
{
$myLine = fgetss($myFile, 255);
print($myLine);
}
fclose($myFile);
}
?>



訪問檔案常見屬性


訪問檔案常見屬性




print("檔案的所有者(UID 值):");
print(fileowner("data.txt")."
");
print("檔案的大小:");
print(filesize("data.txt")."
");
print("檔案的類型:");
print(filetype("data.txt")."
");
?>

調用文字檔內容


調用文字檔內容



// 開啟檔案同時,列印每一行
$myFile = file( "data.txt");
for($index = 0; $index < count($myFile); $index++)
{
print($myFile[$index]."
");
}
?>




建立目錄函式


建立目錄函式


if(mkdir("myDir1", 0777)) //建立目錄的函數
{
print("目錄建立成功"); //目錄建立成功
}
else
{
print("目錄建立失敗!"); //目錄建立失敗
}
?>



瀏覽目錄


瀏覽目錄


// 使用表格瀏覽目錄的結構
print("















n"); // 建立表格的頭 print(" n"); print(" n"); print(" n"); print(" n"); $myDirectory = opendir("."); // 建立操作目錄的控制代碼 // 讀出目錄中的每一個子項 while($entryName = readdir($myDirectory)) { print(" "); print(" "); print(" "); print(" n"); } closedir($myDirectory); // 關閉目錄 print("
檔案名稱檔案的大小
$entryName");
print(filesize($entryName));
print("
n");
?>



PHP相關資訊


PHP相關資訊


phpinfo();
?>



常用的數值判斷函數


常用的數值判斷函數


//判斷數組
$colors = array("red", "blue", "green");
if(is_array($colors))
{
print("colors is an array"."
");
}
//雙精確度數判斷
$Temperature = 15.23;
if(is_double($Temperature))
{
print("Temperature is a double"."
");
}
//整數判斷
$PageCount = 2234;
if(is_integer($PageCount))
{
print("$PageCount is an integer"."
");
}
//對象判斷
class widget
{
var $name;
var $length;
}
$thing = new widget;
if(is_object($thing))
{
print("thing is an object"."
");
}
//字元判斷
$Greeting = "Hello";
if(is_string($Greeting))
{
print("Greeting is a string"."
");
}
?>



檔案上傳介面


檔案上傳介面


if($UploadAction){
$UploadAction=0;
$TimeLimit=60;
/*設定逾時限制時間預設時間為 30s,設定為0時為不限時 */
set_time_limit($TimeLimit);
If(($Upfile != "none")&&
($Upfile != ""))
{
$Filepath="d:webwebphptest"; //上傳檔案存放路徑
$FileName=$Filepath.$Upfile_name;
if($Upfile_size <1024) //上傳檔案大小
{$FileSize = (string)$Upfile_size . "位元組";}
elseif($Upfile_size <(1024 * 1024))
{
$FileSize = number_format((double)($Upfile_size / 1024), 1) . " KB";
}
else
{
$FileSize = number_format((double)($Upfile_size/(1024*1024)),1)."MB";
}
if(!file_exists($FileName))
{
if(copy($Upfile,$FileName))
{unlink($Upfile);
echo "

n";
echo "檔案 $Upfile_name 已上傳成功!";
echo "

n";
echo "檔案位置:$FileName";
echo "

n";
echo "檔案大小:$FileSize";
echo "

n";
}
else
{echo "檔案 $Upfile_name上傳失敗!"; }
}
else
{echo "檔案 $Upfile_name已經存在!"; }
}
else
{echo "你沒有選擇任何檔案上傳!"; }
set_time_limit(30); //恢複預設逾時設定
}
?>


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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.