Quick bi (1)-PHP: Overview, constant, variable, operator, expression, control statement-webabcd [source code download]
Quick Decision (1)-PHP: Overview, constant, variable, operator, expression, control statement
Author: webabcd
Introduction
PHP
- Overview
- Constant
- Variable
- Operator
- Expression
- Control statement
Example
1. Overview
Basic/summary. php
";?>
I am html (used to demonstrate php and html mixing)
Here is html (used to demonstrate php and html mixing)
";
2. constants
Basic/constant. php
"; // After PHP 5.3.0, const can be used to define the constant const MyConst2 =" MyConst2 "; echo MyConst2; echo"
";/*** Magic constant ** magic constant, the value * // _ LINE _-indicates the current row number in the file in different scenarios. The echo _ LINE __is determined by the row in the script __; echo"
"; // _ FILE _-the complete path and FILE name of the current FILE. if the FILE is used in the contained FILE, echo _ FILE __; echo" is returned"
"; // _ DIR _-Directory of the file. if it is used in included files, echo _ DIR __; echo" is returned"
"; // _ FUNCTION _-FUNCTION name/_ CLASS _-CLASS name/_ METHOD _-METHOD name/_ NAMESPACE _-NAMESPACE name/_ TRAIT _-Trait name
3. variables
Basic/variable. php
"; // Use variables with $. the variable echo" $ a "is also used even if double quotation marks are added to the outside; // output: aecho"
"; // Define the variable name through {}, for example," $ AB ", php will use the variable $ AB, but what we actually want to use is $, in this case, you can use {} to define the variable name, as shown below: echo "{$ a} B"; // output: abecho"
"; // If you do not want to use variables, use the escape character" \ "to convert" $ "into a string echo" \ $ a "; // output: $ aecho"
"; // If a single quotation mark is added, echo '$ A' is processed according to the string; // output: $ aecho"
"; // Value $ B = $ a between variables; // Use & to define references. The following example shows how to make $ c reference $ a $ c = & $ a; // assign a value to $ a again for $ a = "aa"; echo "$ B "; // output: aecho"
"; Echo" $ c "; // output: aaecho"
"; Function f1 () {// Here $ a references the local variable $ a. However, $ a is not defined in the local range of f1, so if you want to use $ a, it will not have any echo $ a; // output: echo"
";} F1 (); function f2 () {// Mark $ a with the global keyword, after this local range, when $ a is used, the global version $ a global $ a; echo $ a; // output: aa echo "is used"
";}F2 (); function f3 () {// directly use the global version of the specified variable through $ GLOBALS echo $ GLOBALS ['A']; // output: aa echo"
";}F3 (); function f4 () {// uses static to define static variables. the resident memory is static $ I = 0; echo $ I; echo"
"; $ I ++;} f4 (); // output: 0f4 (); // output: 1f4 (); // output: 2 $ x =" aaa "; // variable variables (variable), using the value of the variable as the variable name // the following sentence is equivalent to $ aaa = "bbb"; (because the value of $ x is aaa, so $ x is equivalent to $ aaa) $ x = "bbb"; echo $ aaa; // output: bbbecho"
"; Echo $ x; // output: bbbecho"
"; $ S =" 12345 "; // braces: used to indicate a single character in a string variable (subscript starts from 0) $ s {1} = 'x '; echo $ s; // output: 1x345echo"
"; // The role of braces: used to define the name of a variable (for example, the following variable is $ s instead of $ ss) echo" {$ s} s "; // output: 1x345secho"
";
4. operators, expressions, and control statements
Basic/statement. php
"; $ S1. =" wanglei "; // output: hello wangleiecho $ s1; echo"
"; // The Difference Between = and = $ a1 =" 1 "; // String $ b1 = 1; // Integer $ c1 =" 1 "; // string if ($ a1 = $ b1) // compare two different types of data. The system automatically converts {echo '$ a1 = $ b1 '; // output: $ a1 = $ b1 echo"
";}If ($ a1 ===$ b1) // compare two different types of data ====, the system will not automatically convert {// not valid} if ($ a1 ===$ c1) {echo '$ a1 ==$ c1'; // output: $ a1 ===$ c1 echo"
";} // For the if/else statement in the {} mode or with the {} mode omitted, elseif and else if are the same if (1 = 2) echo "1 = 2"; else if (1 = 1) echo "1 = 1"; elseif (2 = 2) echo "2 = 2 "; else echo "else"; echo"
"; // For the if/else statement in the mode, elseif can only be used instead of else ifif (1 = 2): echo" 1 = 2 "; // else if (1 = 1): // echo "1 = 1"; elseif (2 = 2): echo "2 = 2"; else: echo "else"; endif; echo"
"; // Foreach statement usage $ array1 = array (1, 2, 3, 4); foreach ($ array1 as $ value) {echo $ value; echo"
";} Foreach ($ array1 as $ key => $ value) {echo $ key."-". $ value; echo"
";}// Try catch finally usage try {echo" try "; echo"
"; Throw new Exception (" error message ", 999); // If you want to write a custom Exception, you can inherit the Exception.} catch (Exception $ ex) {echo "catch"; echo"
"; Echo sprintf (" message: % s, code: % d, file: % s, line: % d, trace: % s ", $ ex-> getMessage (), $ ex-> getCode (), $ ex-> getFile (), $ ex-> getLine (), $ ex-> getTraceAsString (); echo"
";}Finally {echo" finally "; echo"
";}
OK
[Download source code]