Explore PHP
Although the front-end separation, but as the frontend or to work with the data, so the background language understanding is still very necessary. PHP is the only thing to learn today.
What is PHP?
PHP (foreign name: Php:hypertext Preprocessor, Chinese name: "Hypertext Preprocessor") is a common open source scripting language. The grammar absorbs the C language, Java and Perl features, is conducive to learning, widely used, mainly for the field of web development. PHP's unique syntax mixes the syntax of C, Java, Perl, and PHP's own creation. It can execute Dynamic Web pages more quickly than CGI or Perl. Dynamic pages made in PHP are compared to other programming languages, and PHP is executed in HTML (an application under the standard Universal Markup Language), which is much more efficient than CGI, which generates HTML markup entirely; PHP can also execute post-compilation code, Compilation can achieve encryption and optimize code execution, making code run faster.
Basic Syntax:
variable : Create a variable declared with the $ flag (PHP will recognize the variable based on the $ identifier), but the variable itself does not include the $ identifier.
output : PHP Output statement has 2: one is the echo statement can output any single statement at a time; one is a print statement, and only one statement is output at a time. 2 differences: Echo executes faster, no return value, and print execution is slow (because the return value is always 1). Recommended echo output statement by store chief
stitching Strings (collocated): PHP uses "." to stitch strings (much different from JavaScript, note the distinction)
Global variables and local variables: Variables inside a function are called local variables, and variables outside the function are called global variables. Inside the function, there is no direct access to the variables outside the function (this is very different from JavaScript, note the distinction, but you can use the $globals array to access
<? PHP $x= ' Global x '; function myTest () { //echo $x;//error //correct wording echo$GLOBALS[' x ' ]; // Create a global variable in a function $GLOBALS $GLOBALS [' X '] ; } MyTest (); Echo $y ;? >
Shown in Chrome:
keywordGlobal: For in-function access global variables call global variables in a letter, in addition to using the $global array, you can also add the global keyword before the variable
<? PHP $x=5; $y=10; function myTest () { global$x,$y; $y=$x+$y; } MyTest (); Echo $y // Output?>
Shown in Chrome:
keyword static: When the function is finished, internal variables are usually deleted to reduce memory usage, but if you want to specify that a local variable is not deleted, you can use the Static keyword when you declare the variable for the first time
<?PHPfunctionmyTest () {Static $x=0; Echo $x; $x++;} MyTest ();//=>0Echo' <br> ';Echo' <br> '; MyTest ();//=>1Echo' <br> ';Echo' <br> '; MyTest ();//The result of the =>2//variable not being deleted is that each execution function is based on the original +1?>
Shown in Chrome:
Super Global Variables : Variables that can be accessed anywhere in the script
1. $GLOBALS
2.$_post/$_get (widely used in form data collection and AJAX requests)
3.$_cookie is used to collect COOKIE data from the front end.
4.$_request The variable contains the contents of the $_cookie $_post/$_get
5.$_session Server version of Cookies
Constants :
1. The naming convention is consistent with the variable, but the constant name does not need to add the $ modifier.
2. When a constant value is defined, it cannot be changed anywhere else in the script.
3. The default is a global variable that can be used anywhere in the entire running script.
4. All caps are recommended for constant names
Format: Define (Name,value[,case_insensitive]) name: Required parameter, constant name, or marker. Value: A required parameter, a constant. Case_insensitive: Optional parameter, if set to TRUE, the constant is case insensitive. The default is case-sensitive.
Instance:define("ENNAME", "laoxie");
Arrays : PHP's array form and JavaScript are the big difference; here, take a look. First: The PHP array is of a type: a Valarray-an array of arrays with numeric index values-an array with the specified key, with each key associated with a value multidimensional array-an array containing an array or multiple arrays. In PHP, the array () function is used to create arrays echo is not able to print the array directly (need to convert Json_enconde () to a string)
<? PHP // array of values $cars=array("Volvo", "BMW", "Toyota"); // Associative Arrays $age=array("Peter" and "+", "Ben" = "Notoginseng", "Joe" and "n"); >
The methods for arrays are:
Gets the length of the array: count ()
Traversing arrays: For/foreach ()
<?PHP//iterating through a valarray: For loop $cars=Array("Volvo", "BMW", "Toyota")); $arrlength=Count($cars); for($x= 0;$x<$arrlength;$x++){ Echo $cars[$x] . "<br>"; } //traversing associative arrays: foreach $age=Array("Peter" and "Ten", "Ben" and "Panax Notoginseng", "Joe" and "43"); foreach($age as $x=$x _value){ Echo"Key=".$x. ", value=."$x _value; Echo"<br>"; }?>
Show in Chrome:
Object-oriented : PHP's object-oriented and JavaScript are also very different.
Class: Defines a class that uses the Class keyword followed by a string of curly braces ({}), within which variables and methods can be defined in parentheses. class variables use the Var declaration to define the function definition similar to the JavaScript function definition.
<?PHPclassperson{//member Variables var $name; var $age= 18; //member functions functionSetName ($name){ $this->name =$name; } functionGetage () {return $this-Age ; } }?>
Instantiated object: $p = new person;
Call member Properties/methods: Call $p->setname (' Tom ') using "-a" (like JavaScript "."); $p->name;
Constructor: The __construct constructor is a special method. Primarily used when creating objects, initialize object declaration methods:
function __constrcut ($par 1, $par 2..) {#code}
destructor: The __destruct constructor is the inverse of the constructor, when the object ends its life cycle (for example, when the object's function has been called), the system automatically executes the destructor note: A class uses only one destructor. Declaration method: Function __destruct () {#code}
Access control: Public (publicly, default)--public class members can be accessed anywhere by declaring the way: Var $par
Private-Private members can only be accessed within the class declaratively: private $par
protected (protected)--protected class members can only be declared within the class, subclass, Parent class: protected $par
Static--declares that a class property or method is static, is accessed within a class, and cannot be instantiated by an object access declaration: Static $name
Access mode: Class Name:: Method/Property (very special, not "--" access)
PS:-Class attributes must be defined as public, protected, and private. If defined with Var, it is considered public.
-Methods in a class can be defined as public, private, or protected. If these keywords are not set, the method defaults to public.
Inheritance: Inherits a class using the keyword extends, which inherits the properties and methods of the parent class (except private)
The format is as follows:
class Man extends Person { // Add member functions function setage ($age) { $this ->age = $age; } // overriding Method function SetName ($name) { $this->name = $name; return $name; }}
Generate dynamic Pages : This involves several aspects---form submission: $_post, $_get, $_request; Sessions: Session Several methods about session: Session_Start ()---Start new session session_destory ()---Destroy all data for a session time ()---get the current times
interface : Writing data interfaces with PHP
When reading local data, the direct return Data Json_encode ()---------the array is converted to string Json_decode (JSON, Assoc)--When the string is converted to an array/object assoc to True, an array is returned.
Reading and writing of files
fopen (filename, mode): Open file
Fread (): Read content
Fwrite (): Write content
Fclose (): Close file
FileSize (): Read file character length
// Open a file in read mode $myfile fopen ('./data/weibo.json ', ' R '); // Read file contents $content fread ($myfilefilesize('./data/weibo.json ')); // close files and reduce resource usage fclose ($myfile);
Get remote JSON data:
API data
Curl:client URL
$url= ' http://wthrcdn.etouch.cn/weather_mini?city= Guangzhou ';//initializing a Curl session$ch=curl_init ();//set the options you wantcurl_setopt ($ch, Curlopt_url,$url); //Execution Session$contents= Curl_exec ($ch); //Close SessionCurl_close ($ch); //Output ContentEcho $contents;
Reptile: file_get_contents () Preg_match_all ();
PHP's basic syntax is written so much (not mentioned as JavaScript), and the details will continue to be added later.
2017-3-23 15:30
PHP---on PHP