PHP is the world's best language ", this sentence everyone is not strange!" May also attract the war of words, in fact, any programming language has its own advantages and disadvantages, in different areas, different environments play a different role. I dare not say PHP is the best language in the world, but I dare say "I love PHP". I am not a big coffee, is a familiar with the PHP part. This article is suitable for some PHP foundation of the People, is an overview, summary, and hope to help everyone!
Introduction to PHP
PHP (foreign name: Php:hypertext Preprocessor, Chinese name: "Hypertext Preprocessor") is a common open source scripting language. PHP original Personal Home page abbreviation, has been formally renamed as "Php:hypertext preprocessor". Already more than 20 years old, the grammar absorbed the C language, the Java and the Perl characteristic, is advantageous for the study, uses extensively, mainly applies in the Web development domain. 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.
PHP Basic Syntax
I. Name-CASE Sensitivity
Variable name, constant name, array index (key name) These are case-sensitive and must be name-consistent.
Function name, method name, class name, magic Constant, null,true,false, coercion type conversion, these are case insensitive
Two. variables, constants
Name: tag variable with "$", cannot be a number, space,. To begin with, but can have kanji, such as $ variable = 123;
variable variable: $a = ' AA '; $ $a = ' BB '; Then $aa = ' BB ';
reference Assignment: $a = ' 123 '; $b = &$a; The equivalent of $ A and $b to the memory of a space, change the value of $ A, $b also follow the change. Similar to the C language &, the difference is unset ($a), $b still exist.
Variable type: shaping int, float float, String, Boolean Boolean, Array, object, resource resource, NULL NULL
Global Variables: The global variables themselves are static storage, all global variables are static variables
$_seesion, $_cookie, $_post, $_get, $_request, $_files, $_EVN, $GLOBALS
static variables:statics
1. Static global variables can be used anywhere in the script (in the method body, to declare that it is global)
2. Static local variables are only effective in the function body and are relatively safe. So it is recommended to use static local variables
Static global variable, also global variable, same as $_get,$_post,$_files, same as storage location (constant also)
static methods: static methods do not require the class being instantiated to be used directly. such as Math::max ($a, $b); The math class is not instantiated to call a static method directly.
constants:define (' Constant name ', ' constant value ', $flag =false); $flag If true, case is not case-the default is false;
pre-defined constants: Php_os, Php_version,
E_error=1 error, resulting in script termination; e_warning=2 warning, script not terminating; E_notice=8 noncritical error
Magic constants: Magic constants can be uppercase or lowercase, is not case-sensitive, all return the physical path, even if the output is included, output is also source code information, non-current contains the information of the file, and $_server distinguish.
(1). __file__ Current File path
(2). __dir__ Current File directory
(3). __line__ the line in the file file
(4). __function__ return function name in the current file
(5). __CLASS__ returns the class name in that class in the current file
(6). __METHOD__ returns the class name in that method in the class of the current file:: Method name
Summarize:
(Global) Constants: (The default constants are global) stored in (static) data segments
Variables: Global variables (static data segments), local variables (stored in stacks), static variables (whether global/local are stored in static data segments)
three. Type conversion
1. Gets the type of the variable getType ($a);
2. Set the type of the variable setType ($a, ' type '); Types include Boolean,integer,float,string,array,object,null
3. Coercion type conversion, (IBID. type)
$b = (int) $a; Convert to Reshape
4. Type conversion function
Intval (), turning into plastic, floatval (), turning into floating-point type; Strval (); Getting the string of a variable
5. Type of judgment
Is_bool/int/float/string/array/object/resource/null
Is_numberic (); ' Determines whether a number or array string of any type
Is_callable ();//Determine if it is a valid function name
Iv. operators
Arithmetic operators: Plus +, minus-, multiply *, except/, modulus%, self-increment + +, self-subtract--
Join operators: . , such as $str = ' one ' . ' Both '; Then $str = ' onetwo ';
Assignment operators: =, + =,-=, *=,/=,%=,. =
Comparison operator: >=, <, <=,==, = = =,! =,!===, <>
Logical operators: Logic and and,&&; Logical OR OR, | |; Take anti-not,!; Logical XOR (different side returns True, same return false)
Bitwise operators: & (Bitwise AND), | (bitwise OR), ^ (bitwise XOR); ~ (bitwise reverse); << (bitwise left); >> (bitwise right SHIFT)
Other cloud operators:
?: three mesh operator, @ Ignore error, = = array subscript,--Call object properties and methods
instanceof type operator, inverted quotation mark (• Not single quote) Execute SHELL statement
V. Process Control
1. Judgment Statement
If statement
if (condition) {When the condition is true, the executed statement} else {The statement executed when the condition is false}
Three types of patterns:
if (condition) {true, statement} else {false, statement}
if (condition) {true, statement}
if (condition) {true, statement} elseif (condition) {Two conditions are true, statement}.....else{false, statement}; Indicates that you can elseif multiple times.
switch Statement
Switch (condition) {
Case 1 Conditions:
statement; break;
Case 2 Conditions:
statement; break;
....
Default: (This can be the default)
Default statement;
}
2. Looping statements
While statement: while (condition) {condition is true, execute inside statement; condition is false, exit loop}
Do ... While statement: do{the first direct execution of the loop body, the second start, according to the condition of true execution, false exit loop} while (condition)
For statement: A for (initial value; a condition; A statement that alters a condition) {A statement in the body of a loop}. such as for ($i =0; $i <8; $i + +) {echo $i;}
3. Exit the Loop body statement
Continue,break;exit differences:
Continue skips the current loop and the loop continues
Break jumps out of the current loop, loop terminates
Exit Terminates the current script, and the code behind this line of code does not execute.