PHP基礎開發程式碼範例~
最近打算重拾PHP來開發一些小型應用,很久沒用PHP了,有些文法都生疏了,今天上午寫了三個例子,基本上把之前的PHP複習了一下。
基礎文法操作:
';echo '測試輸出1(單引號)
';echo "測試塑出2(雙引號)
";?>'?>'; ?>';$testInt = 1;$testStr = "字串";$testFloat = 1.23;$testBoolean = false; //false/true:如果轉換成字串則為空白/"1",轉換成整型則為0/1$testArray = array("first"=>1,2,3,4,5);$testStr2 = $testStr; //testStr修改,testStr2不會修改$testStr3 = &$testStr; //testStr修改,testStr3也會修改echo '整型:'.$testInt.'
';echo '字串型:'.$testStr.'
';$testStr = '字串修改了';echo '字串型修改後:'.$testStr.'
';echo '字串型修改後(2):'.$testStr2.'
';echo '字串型修改後(3):'.$testStr3.'
';echo '浮點型:'.$testFloat.'
';echo '布爾型:'.(int)$testBoolean.'
';echo '數組測試(1):'.$testArray[0].'
';echo '數組測試(2):'.$testArray['first'].'
';print_r($testArray);echo '
';foreach ($testArray as $i => $value) { echo '數組迭代結果:'.$i."->".$value."
";}//IO操作echo '##################IO操作####################
';echo '讀取該檔案的內容:
';$theFileName = "test_base.php"; //檔案名稱或者全路徑$handle = fopen ($theFileName, "rb");$contents = fread ($handle, filesize ($theFileName));fclose ($handle); echo ''.htmlspecialchars($contents).'
';?>
資料庫的處理:
';if($actionSubmit != null && $actionSubmit != '') {if($reqTheType == '1') {testSearch();}if($reqTheType == '2') {testInsert();testSearch();}if($reqTheType == '3') {testUpdate();testSearch();}}/** * 資料庫查詢 * Enter description here ... */function testSearch() {echo '查詢資料
';global $hostname,$username,$password,$database,$databaseCharset;$currentConn = null;$currentConn = mysql_connect ( $hostname, $username, $password );mysql_select_db ( $database );mysql_query("set names charset ".$databaseCharset);mysql_query("set names ".$databaseCharset);$result = mysql_query ( "select * from e_user" ); //查詢動作返回的是result結果集while ( $row = mysql_fetch_object ( $result ) ) {echo $row->uri . "\t" . ($row->username) . "
";}mysql_free_result ( $result );mysql_close ( $currentConn );}/** * 資料庫資料添加 * Enter description here ... */function testInsert() {global $hostname,$username,$password,$database,$databaseCharset;$insertSql = "insert into e_user(uri,username,password) values";$insertSql .= "(";$insertSql .= "'".generateId()."','測試使用者','123456'";$insertSql .= ")";$currentConn = null;$currentConn = mysql_connect ( $hostname, $username, $password );mysql_select_db ( $database );mysql_query("set names charset ".$databaseCharset);mysql_query("set names ".$databaseCharset);echo '添加資料'.$insertSql.'
';$result = mysql_query($insertSql); //插入動作返回的是booleanif(!$result) {die('Error: ' . mysql_error());}mysql_close ( $currentConn );}/** * 資料庫修改 * Enter description here ... */function testUpdate() {global $hostname,$username,$password,$database,$databaseCharset;$updateSql = "update e_user";$updateSql .= " set username='修改後的使用者名稱稱' where uri = '001'";$currentConn = null;$currentConn = mysql_connect ( $hostname, $username, $password );mysql_select_db ( $database );mysql_query("set names charset ".$databaseCharset);mysql_query("set names ".$databaseCharset);echo '修改資料'.$updateSql.'
';$result = mysql_query($updateSql); //插入動作返回的是booleanif(!$result) {die('Error: ' . mysql_error());}mysql_close ( $currentConn );}/** * 自動產生ID號 * @param unknown_type $count */function generateId($count = 6) {$resultId = '';for($i=0;$i<$count;$i++) {$resultId .= (string)rand(0, 9);}return $resultId;}?>
物件導向編程:
uri = $uri;$this->username = $username;$this->password = $password;$this->flag = '100';$this->type = self::USER_TYPE_NORMAL;}/*測試靜態函數的處理*/static function testStatic() {//$this->username = 'static'; //該方法是錯誤的,靜態方法中只能操作靜態變數return self::USER_TYPE_NORMAL;}/*get set 方法用於管理內部的欄位屬性*/public function getUri() {return $this->uri;}public function getUsername() {return $this->username;}public function getPassword() {return $this->password;}public function setUri($uri) {$this->uri = $uri;}public function setUsername($username) {$this->username = $username;}public function setPassword($password) {$this->password = $password;}public function getType() {return $this->type;}public function setType($type) {$this->type = $type;}/*實現底層的抽象方法*/function showInfo() {echo '我是MyUser對象.';}//實現介面方法public function start() {echo '啟動MyUser對象....';}//實現介面方法public function stop() {echo '停止MyUser對象....';}}//擴充自MyUser的類class MyExtendUser extends MyUser implements Module {/*覆蓋父類的建構函式*/function __construct($uri = '',$username = '', $password = '') {//調用父類的建構函式parent::__construct($uri,$username,$password);//實現自己的一些初始化動作$this->flag = '200';}/*覆蓋父類的getUsername方法*/public function getUsername() {return '繼承自MyUser,'.$this->username;}//實現介面方法public function start() {echo '啟動MyExtendUser對象....';}//實現介面方法public function stop() {echo '停止MyExtendUser對象....';}}//測試使用者物件$theUserObj = new MyUser('001','測試使用者1','123');echo '使用者名稱稱:'.$theUserObj->getUsername().'
';print_r($theUserObj);echo '
';echo '測試靜態函數1:'.$theUserObj->testStatic().'
';echo '測試靜態函數2:'.MyUser::testStatic().'
';echo '測試實現的介面:';$theUserObj->start();echo '
';//測試繼承$theUserObj2 = new MyExtendUser('002','測試使用者2','123');echo '使用者名稱稱2(繼承):'.$theUserObj2->getUsername().'
';print_r($theUserObj2);echo '
';echo '測試實現的介面2:';$theUserObj2->start();echo '
';?>