<?php $fileName = "php大師.test.php"; //補充程序,顯示文件名(不包括擴展名) $start = strrpos($fileName, "."); $newStr = substr($fileName,0, $start); var_dump ( $newStr ); ?>
<?php for($i=100;$i<1000;$i++){ if(($i*$i)%1000==$i) { echo $i; echo "<br/>"; } } ?>
<?php $x = "hello"; switch ($x) { case 1 : echo "Number 1"; break; case 2 : echo "Number 2"; break; case "hello" : echo "hello"; break; default : echo "No number between 1 and 3"; } ?>
<?php $students = array(array("name"=>"張三","age"=>25,"height"=>180),array("name"=>"李四","age"=>22,"height"=>170)); echo $students[0]["name"]; echo "<br/>"; echo $students[1]["name"]; ?>
<?php // 顯示右邊n個字符 $n = 5; $oldStr = "dsfasfasf"; $rightStr = subStr ( $oldStr, strlen($oldStr) - $n ); var_dump ( $rightStr ); ?>
<html> <body> <form action="welcome.php" method="post"> Name: <input type="text" name="name" /> Age: <input type="text" name="age" /> <input type="submit" /> </form> </body> </html>
好好學習,每天向上 <br/> <?php echo "hello world"; define("ABC",1000); var_dump(defined("ABC")); echo "<br/>"; echo constant("ABC"); echo PHP_OS; echo "<br/>"; echo PHP_VERSION; echo "<br/>"; echo __FILE__; define('NAME','php'); define('NAME','linux'); echo NAME; ?> <img src="/test/a.jpg"></img>
修改端口號php
經過網絡命令netstat –aon 找到進程號html
php學習linux
常量通常是大寫字母構成,常量只能定義一次數組
bool define ( string name, mixed value [, bool case_insensitive] ) bool defined ( string name ) <?php echo "中文測試<br/>"; print "hello world!"; echo "<br/>"; echo "<img src='/test/a.jpg'></img>"; define("PI",3.14); var_dump(defined("Pi")); echo PHP_OS; echo "<br/>"; echo PHP_VERSION; echo "<br/>"; echo __FILE__; ?>
常量、變量的命名規則:
以字母、下劃線開頭,後接字母、數字、下劃線瀏覽器
以$打頭命名變量,變量要先賦值後使用
同一個變量,便可以存儲數字也能夠存儲字符串,也就是能夠存儲任意類型的數據
變量不用指定數據類型,但必須賦值後才能使用服務器
求字符串的長度:int strlen ( string 字符名 )
查找第一次出現的子串位置:int strpos
查找最後一次出現的子串位置:int strrposcookie
求字符串右邊n個字符構成的子串
substr(原串,-$n) 或者:substr(原串,strlen(原串)-$n)網絡
顯示去掉擴展名的文件名session
$dotpos = strpos($fileName,"."); echo substr($fileName,0, $dotpos);
$y = $x++ 至關於{$y=$x; $x=$x+1;} $y = $x-- 至關於{$y=$x; $x=$x-1;} $y = ++$x 至關於{$x=$x+1; $y=$x;} $y = $x-- 至關於{$x=$x-1; $y=$x;}
對於表達式:A && B,若是A爲假,則再也不計算表示式B的值 對於表達式:A || B,若是A爲真,則再也不計算表示式B的值
date函數用於將日期格式化爲指定格式dom
構造數組:$names = array("a","b","c"); 訪問數組元素:$names[0]、 $names[1]、 $names[2].
函數explode,用於將字符串分割成多個子串構成的數組
關聯數組
方法: 「鍵」=>值
數據類型
Boolean型
integer型
浮點型
字符串型
僞類型
mixed、number、void、callback
<?php // 求1!+2!....+10! // 聲明一個控制變量,初始化 $i = 1; // 聲明一個存儲和的變量 $sum = 0; // 聲明一個變量存儲n!,初始化化爲1; $rank = 1; // 計算$i的階乘,計算完立刻累加科 while ( $i <= 10 ) { // 計算$i!= ($i-1)!*$i $rank *= $i; // 累加 $sum += $rank; // 改變循環變量值 $i ++; } echo $sum . "<br/>"; // 求1!+2!....+10! $x = 1; $sum2 = 0; while ( $x <= 10 ) { // 計算$x! $rank2 = 1; $y = 1; while ( $y <= $x ) { $rank2 *= $y; $y ++; } // 累加 $sum2 += $rank2; // 改變循環變量值 $x ++; } echo $sum2 . "<br/>"; // 求1.....100之間素數 // 8 = 2*4 7是素數,由於7/2 7/3 7/4....7/6,7不能被整除 for($i = 2; $i <= 100; $i ++) { // 假設是素數 $isPrime = true; //除數不用到$i-1,到sqrt($i) for($j = 2; $j <= sqrt($i); $j ++) { if ($i % $j == 0) { $isPrime = false; break; } } //驗證假設有沒有被修改 if ($isPrime == true) { echo $i . " "; } } $arr=array("one", "two", "three"); //依次取出數組每個元素放到$aa foreach ($arr as $aa){ echo $aa. " "; } echo "<br/>"; // 求1!+2!....+10! function Rank($n){ $rank = 1; for($i=1;$i<=$n;$i++){ $rank *= $i; } return $rank; } $sum = 0; for($i=1;$i<=10;$i++){ $sum += Rank($i); } echo $sum; ?>
檢入
自定義函數模板
開發環境:wamp3.06 + Zend studio 12
調試配置
打開php配置文件php.ini
去掉xdebug前的全部註釋符號‘;’,也就是說使用wamp自帶的調試器
容許訪問服務器
修改httpd.conf,容許訪問服務器
zend Studio 中設置
Servers
配置local Apache HTTP Server
配置exe文件
配置debug
修改Document Root爲
配置默認字符集
配置默認字體大小
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <?php // if((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == // "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && // ($_FILES["file"]["size"] < 20000)) { // if ($_FILES["file"]["error"] > 0) { // echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; // } // else { if (file_exists ( "upload/" . $_FILES ["file"] ["name"] )) { echo $_FILES ["file"] ["name"] . " already exists. "; } else { $newname = iconv ( "utf-8", "gb2312", $_FILES ["file"] ["name"] ); move_uploaded_file ( $_FILES ["file"] ["tmp_name"], "upload/" . $newname ); // move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" // .$_FILES["file"]["name"]); echo "Stored in: " . "upload/" . $_FILES ["file"] ["name"]; } // } // } else { echo "Invalid file"; } ?> </body> </html>
<html> <body> <?php $week = array("星期日","星期一","星期二","星期三","星期四","星期五","星期六"); $day = date("w"); $mydate = date("你好! 如今是Y年n月j日H點i分,$week[$day]"); echo $mydate; ?> </body> </html>
<?php session_start(); if(isset($_SESSION['user'])){ header("Location:main.php");//自動跳轉到main.php }else{ //獲取用戶輸入 $username = $_POST [ 'username' ]; $passcode = $_POST [ 'passcode' ]; $cookie = $_POST [ 'cookie' ]; //判斷用戶是否存在,密碼是否正確 if ($username =="hhh" && $passcode == "12345") { $_SESSION['user']=$username; header("Location:main.php" );//自動跳轉到main.php } else{ echo "用戶名或密碼錯誤"; } } ?>
<?php header('Content-type:text/html;charset=utf-8'); ?> <html> <head> <meta http-equiv= "Content-Type" content=" text/html; charset=UTF-8"> </head> <body> <?php if (isset ( $_COOKIE ["username"] )) { header ( "location: main1.php" ); } else { $username = $_POST ['username']; $passcode = $_POST ['passcode']; $cookie = $_POST ['cookie']; if ($username == "hhh" && $passcode == "12345") { switch ($cookie) { case 0 : setcookie ( "username", $username ); break; case 1 : setcookie ( "username", $username, time () + 24 * 60 * 60 ); break; case 2 : setcookie ( "username", $username, time () + 30 * 24 * 60 * 60 ); break; case 3 : setcookie ( "username", $username, time () + 365 * 24 * 60 * 60 ); break; } header ( "location: main.php" ); } else { echo ""; } } ?> </body> </html>
發送郵箱
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>無標題文檔</title> </head> <body> <html> <body> <?php if (isset($_REQUEST['emailto'])) { $emailto = $_REQUEST['emailto']; $subject = $_REQUEST['subject']; $message = $_REQUEST['message']; if(mail($emailto,$subject,$message,"From:23232323@hzj.com")){ echo "謝謝使用本程序!"; }else{ echo "未能發送成功!"; } }else{ echo "<form method='post' action='sendemail.php'>EmailTo:<input name='emailto' type='text' /><br />Subject: <input name='subject' type='text' /><br />Message:<br /><textarea name='message' rows='15' cols='40'></textarea><br /><input type='submit' /></form>"; } ?> </body> </html>
PHP Date() 函數可把時間戳格式化爲可讀性更好的日期和時間
語法
date(format,timestamp)
d - 月中的天 (01-31) m - 當前月,以數字計 (01-12) Y - 當前的年(四位數)
<?php echo date("Y/m/d"); echo "<br />"; echo date("Y.m.d"); echo "<br />"; echo date("Y-m-d"); ?>
PHP 引用文件
include() 或 require()
它們處理錯誤的方式不一樣
include() 函數會生成一個警告
require() 函數會生成一個致命錯誤
PHP 文件處理
fopen ( string $filename , string $mode )
關閉文件
fclose() 函數用於關閉打開的文件
逐行讀取文件
fgets() 函數用於從文件中逐行讀取文件
PHP 文件上傳
<html> <body> <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Filename:</label> <input type="file" name="file" id="file" /> <br /> <input type="submit" name="submit" value="Submit" /> </form> </body> </html>