PHP Basic Development Code Demonstration

Source: Internet
Author: User
PHP Basic Development Code Example ~
Recently intends to re-pick PHP to develop some small applications, a long time useless PHP, some of the syntax is unfamiliar, this morning wrote three examples, basically the previous PHP reviewed a bit.

Basic syntax Operations:
 '; echo ' test output 1 (single quote)
' Echo ' Test molded 2 (double quotes)
";? > '?> ';?> '; $testInt = 1; $testStr = "string"; $testFloat = 1.23; $testBoolean = false; False/true: If the string is converted to null/"1", the conversion to integer is 0/1$testarray = Array ("First" =>1,2,3,4,5); $testStr 2 = $testStr; Teststr modification, TESTSTR2 will not modify $TESTSTR3 = & $testStr; Teststr modified, TESTSTR3 will also modify echo ' int: '. $testInt. '
'; Echo ' string type: '. $testStr. '
'; $testStr = ' string modified '; Echo ' String type modified: '. $testStr. '
'; echo ' String type modified (2): '. $testStr 2. '
'; echo ' String type modified (3): '. $testStr 3. '
'; Echo ' floating-point type: '. $testFloat. '
'; Echo ' Boolean: '. (int) $testBoolean. '
'; Echo ' array Test (1): '. $testArray [0]. '
'; Echo ' array Test (2): '. $testArray [' first ']. '
';p rint_r ($testArray); Echo '
'; foreach ($testArray as $i = + $value) {echo ' array iteration result: '. $i. " ". $value."
";} Io operation Echo ' ################# #IO操作 ####################
'; Echo ' reads the contents of the file:
'; $theFileName = "test_base.php"; File name or full path $handle = fopen ($theFileName, "RB"); $contents = Fread ($handle, FileSize ($theFileName)); fclose ($handle); Echo '
'. Htmlspecialchars ($contents). '
';? >


Processing of the database:
 '; 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);} /** * Database Data add * Enter description here ... */function Testinsert () {Global $hostname, $username, $password, $database, $datab Asecharset; $insertSql = "INSERT into E_user (Uri,username,password) values"; $insertSql. = "("; $insertSql. = "'". Generateid (). "', ' Test user ', ' 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 ' Add data '. $insertSql. '
'; $result = mysql_query ($INSERTSQL); The Insert action returns BOOLEANIF (! $result) {die (' Error: '. mysql_error ());} Mysql_close ($currentConn);} /** * Database Modification * Enter description */function testupdate () {Global $hostname, $username, $password, $database, $databas Echarset; $updateSql = "Update E_user"; $updateSql. = "Set username= ' modified user name ' where uri = ' 001 '"; $currentConn = null; $curr Entconn = 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); The Insert action returns BOOLEANIF (! $result) {die (' Error: '. mysql_error ());} Mysql_close ($currentConn);} /** * automatically generated 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 = ' + '; $this->type = self:: User_type_normal;} /* Test the handling of static functions */static function teststatic () {//$this->username = ' static ';//The method is wrong, only static variables can be manipulated in static methods return Self::user_ Type_normal;} The/*get set method is used to manage internal field properties */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;} /* Implement the underlying abstract method */function Showinfo () {echo ' I am myuser object. ';} Implement interface method Public function start () {echo ' Start MyUser object .... ';} Implement interface method public function stop () {echo ' Stop MyUser object ....} Extended from MyUser class Myextenduser extends MyUser implements Module {/* Overwrite parent class constructorNumber */function __construct ($uri = ', $username = ', $password = ') {//Call the parent class's constructor parent::__construct ($uri, $username, $ password);//implement some of your own initialization actions $this->flag = ' 200 ';} /* Overrides the parent class's GetUserName method */public function GetUserName () {return ' inherits from MyUser, '. $this->username;} Implement interface method Public function start () {echo ' Start Myextenduser object .... ';} Implement interface method public function stop () {echo ' Stop Myextenduser object ....} Test the User Object $theuserobj = new MyUser (' 001 ', ' Test user 1 ', ' 123 '); Echo ' User name: '. $THEUSEROBJ->getusername (). '
';p rint_r ($THEUSEROBJ); Echo '
'; Echo ' Tests static function 1: '. $THEUSEROBJ->teststatic (). '
'; Echo ' Tests static function 2: '. Myuser::teststatic (). '
'; Echo ' test implements the interface: '; $theUserObj->start (); Echo '
';//test Inherit $theuserobj2 = new Myextenduser (' 002 ', ' Test User 2 ', ' 123 '); Echo ' User Name 2 (inheritance): '. $THEUSEROBJ 2->getusername (). '
';p Rint_r ($theUserObj 2); Echo '
'; Echo ' Test implements Interface 2: '; $theUserObj 2->start (); Echo '
';? >

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.