PHP basic development code example ~ Recently I plan to re-develop PHP to develop some small applications. I haven't used PHP for a long time, and some syntaxes are unfamiliar. I wrote three examples this morning. I basically reviewed the previous PHP. Basic syntax operation: & lt ;? Php // test the output' ####################### output test ########## # PHP basic development code example ~
Recently I plan to re-develop PHP to develop some small applications. I haven't used PHP for a long time, and some syntaxes are unfamiliar. I wrote three examples this morning. I basically reviewed the previous PHP.
Basic syntax operations:
'; Echo' test output 1 (single quotes)
'; Echo "test to produce 2 (double quotation marks)
";?>
'?>
';?>
'; $ TestInt = 1; $ testStr = "string"; $ testFloat = 1.23; $ testBoolean = false; // false/true: if it is converted to a string, it is null/"1". if it is converted to an integer, it is 0/1 $ testArray = array ("first" =>, 5 ); $ testStr2 = $ testStr; // testStr modified. testStr2 will not change $ testStr3 = & $ testStr; // testStr modified. testStr3 will also modify echo 'integer :'. $ testInt.'
'; Echo' string type: '. $ testStr .'
'; $ TestStr = 'modified string'; echo 'after string type modification: '. $ testStr .'
'; Echo' after the string type is modified (2): '. $ testStr2 .'
'; Echo' after the string type is modified (3): '. $ testStr3 .'
'; Echo 'floating point type:'. $ testFloat .'
'; Echo 'Boolean:'. (int) $ testBoolean .'
'; Echo' array test (1): '. $ testArray [0].'
'; Echo' array test (2): '. $ testArray ['first'].'
'; Print_r ($ testArray); echo'
'; Foreach ($ testArray as $ I =>$ value) {echo 'Array iteration result:'. $ I. "->". $ value ."
";} // IO Operation echo '################### IO operation ############## ######
'; Echo:
'; $ TheFileName = "test_base.php"; // file name or full path $ handle = fopen ($ theFileName, "rb"); $ contents = fread ($ handle, filesize ($ theFileName); fclose ($ handle); echo''.htmlspecialchars($contents).'
';?>
Database processing:
'; If ($ actionSubmit! = Null & $ actionSubmit! = '') {If ($ reqTheType = '1') {testSearch ();} if ($ reqTheType = '2') {testInsert (); testSearch () ;}if ($ reqTheType = '3') {testUpdate (); testSearch () ;}}/*** database query * Enter description here... */function testSearch () {echo 'query data
'; 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"); // The query action returns the result set while ($ row = mysql_fetch_object ($ result )) {echo $ row-> uri. "\ t ". ($ row-> username )."
";}Mysql_free_result ($ result); mysql_close ($ currentConn);}/*** ADD * Enter description here for database data... */function testInsert () {global $ hostname, $ username, $ password, $ database, $ databaseCharset; $ insertSql = "insert into e_user (uri, username, password) values "; $ insertSql. = "("; $ insertSql. = "'". generateId (). "', 'Test user', '000000'"; $ 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 'add data '. $ insertSql.'
'; $ Result = mysql_query ($ insertSql); // booleanif (! $ Result) {die ('Error :'. mysql_error ();} mysql_close ($ currentConn);}/*** modify database * Enter description here... */function testUpdate () {global $ hostname, $ username, $ password, $ database, $ databaseCharset; $ updateSql = "update e_user"; $ updateSql. = "set username = 'modified user name '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 'modify data '. $ updateSql.'
'; $ Result = mysql_query ($ updateSql); // booleanif (! $ Result) {die ('Error :'. mysql_error ();} mysql_close ($ currentConn);}/*** automatically generate ID number * @ param unknown_type $ count */function generateId ($ count = 6) {$ resultId = ''; for ($ I = 0; $ I <$ count; $ I ++) {$ resultId. = (string) rand (0, 9);} return $ resultId ;}?>
Object-oriented programming:
Uri = $ uri; $ this-> username = $ username; $ this-> password = $ password; $ this-> flag = '000000 '; $ this-> type = self: USER_TYPE_NORMAL;}/* test the processing of static functions */static function testStatic () {// $ this-> username = 'static '; // This method is incorrect. in the static method, only the return self: USER_TYPE_NORMAL variable can be operated.}/* get set method is used to manage internal field attributes */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;}/* implements the underlying abstraction method */function showInfo () {echo 'I am a MyUser object. ';} // implement the interface method public function start () {echo' to start the MyUser object .... ';} // implement the interface method public function stop () {echo' to stop the MyUser object .... ';}} // class extended from MyUser MyExtendUser extends MyUser implements Module {/* overwrites the constructor of the parent class */function _ construct ($ uri = '', $ username = '', $ password ='') {// call the constructor parent ::__ construct ($ uri, $ username, $ password) of the parent class ); // implement some initialization actions of your own $ this-> flag = '000000';}/* override the getUsername method of the parent class */public function getUsername () {return 'inherited from MyUser ,'. $ this-> username;} // implement the interface method public function start () {echo 'to start the MyExtendUser object .... ';} // implement the interface method public function stop () {echo' to stop the MyExtendUser object .... ';}} // test user object $ theUserObj = new MyUser ('001', 'Test user 1', '20180101'); echo 'user name :'. $ theUserObj-> getUsername ().'
'; Print_r ($ theUserObj); echo'
'; Echo' test static function 1: '. $ theUserObj-> testStatic ().'
'; Echo' test static function 2: '. MyUser: testStatic ().'
'; Echo' test implementation interface: '; $ theUserObj-> start (); echo'
'; // Test inheritance $ theUserObj2 = new MyExtendUser ('002', 'Test user 2', '20180101'); echo 'user name 2 (inherited ):'. $ theUserObj2-> getUsername ().'
'; Print_r ($ theUserObj2); echo'
'; Echo' test implementation interface 2: '; $ theUserObj2-> start (); echo'
';?>