Variables and constants 1.1, variable names (identifiers)
1) Variable: $ opening flag
2) Variable name: can be by letter. Numbers, _ 3, cannot start with numbers
3) identifiers are distinguished by uppercase and lowercase. However, the function name does not distinguish between uppercase and lowercase.
4) The variable name can be the same as the function name, although it is agreed. However, confusion should be avoided as far as possible.
PHP does not require a variable to be declared before it is used, when a variable is assigned to it for the first time. This variable was created.
Like what:
$3age;//wrong $_ = 6;//can $*p = 30;//error
1.2. Data type
1) Integer: Used to represent integers.
2) Float: Used to denote all real numbers.
3) String: Used to represent a string.
4) Boolean: Used to indicate true or false.
5) Array: Used to save multiple data items with the same type.
6) Object: Used to save an instance of the class.
PHP can determine the type of a variable at any time based on the value stored in the variable, and can be converted according to the Java syntax when it is necessary to force the type conversion
Like what:
$icount = 0; $fcount = (float) $icount;
1.3, the definition of constants
Constants can hold a value, but once the program is initialized, the value of the constant cannot be changed.
Define (' pi ', 3.14159); Echo pi;
Constants are generally capitalized, defined with the Define function, and are not identified with the $ symbol.
- The constant is preceded by a dollar sign ($);
- Constants can only be defined with the define () function. and cannot pass the assignment statement;
- Constants can be defined and interviewed wherever they are, regardless of the scope of the variable;
- Once a constant is defined, it cannot be defined again or undefined;
- The value of a constant can only be scalar.
1.4. Variable Scope
Scope refers to a range of variables that can be used or visible in a script. PHP has 6 major scope rules.
1) built-in super global variables can be used anywhere in the script.
Like what:
$GLOBALS: An array of all global variables. $_server:server An array of environment variables $_get:get The array of variables passed to the script $_post:post method passed to the script array of variables $_cookie:cookie variable array $_files: array of variables related to file uploads $_ ENV: Environment variable array $_request: variable data entered by all users, including $_get, $_post, and $_cookie$_session: array of Session variables
2) constants are declared once. can be globally visible.
3) The global variables declared in the script are visible in the full script.
4) When a variable used inside a function is declared as a global variable, the name is identical to the global variable name.
5) A variable that is created inside a function and declared as static is not visible outside the function, but is persisted during multiple runs of the function.
6) The variable created inside the function is local, and the variable does not exist when the function terminates.
Second, operator or operator 2.1, arithmetic operator
Arithmetic operators are also numeric operators. Commonly used for integer or double type data. Assume that the string is applied. PHP will attempt to convert these characters into a number, assuming that they include "E" or "E", which will be treated as scientific notation and converted to floating-point numbers, otherwise they will be converted to integers. PHP will look for numbers at the beginning of the string. and use these numbers as values for the string. Assuming no number is found, the value of the string is 0.
Operator |
Name |
Demo sample |
+ |
Add |
$a + $b |
- |
Reducing |
$a-$b |
* |
By |
$a * $b |
/ |
Except |
$a/$b |
% |
Mode |
$a% $b |
2.2. String connection
Use "."
$string = "Hello". "World";
2.3. Assignment operation
Compound assignment operator
Operator |
Usage |
Equivalent to |
+= |
$a + = $b |
$a = $a + $b |
-= |
$a-= $b |
$a = $a-$b |
*= |
$a *= $b |
$a = $a * $b |
/= |
$a/= $b |
$a = $a/$b |
%= |
$a%= $b |
$a = $a% $b |
.= |
$a. = $b |
$a = $a. $b |
2.4. References
The reference operator "&" can be used in associative assignments. A reference is equivalent to an alias, not a pointer, which causes two variables to point to the same memory address and can be reset using unset
Like what:
$a = 1; $b = & $a; $b = 2;unset ($a);
2.5. Comparison operation
comparison operator
Operator |
Name |
Usage |
== |
Equals |
$a = = $b |
=== |
Identical |
$a = = = $b |
!= |
Range |
$a! = $b |
!== |
Bu Heng, etc. |
$a!== $b |
<> |
Range |
$a <> $b |
< |
Less than |
$a < $b |
> |
Greater than |
$a > $b |
<= |
Less than or equal |
$a <= $b |
>= |
Greater than or equal |
$a >= $b |
2.6. Logical operation
logical operators
Operator |
Name |
Usage |
! |
Non - |
! $b |
&& |
And |
$a && $b |
|| |
Or |
$a | | $b |
and |
And |
$a and $b |
Or |
Or |
$a or $b |
Xor |
XOR or |
$a XOR $b |
2.7, bit arithmetic
Bitwise operators
Operator |
Name |
Usage |
& |
Bitwise-AND |
$a & $b |
| |
Bitwise OR |
$a | $b |
~ |
Bitwise non- |
~ $a |
^ |
Bitwise XOR OR |
$a ^ $b |
<< |
Left displacement |
$a << $b |
>> |
Right shift |
$a >> $b |
2.8. Other
1) comma operator
The comma operator "," is used to separate function parameters and other list items, and this operator is often used as a companion.
2) class operator
The "new" and "-I" are used to initialize the instance of the class and the members of the Access class, respectively.
3) Ternary operator
Ternary operator "? : "Expression version number similar to conditional statement if-else, syntax format such as the following:
Condition?Value if True:value if False
4) Error suppressor
The error suppressor "@" can be used before any expression, and this operator is used. Ability to suppress warnings generated by code.
5) Run operator
The run operator is a pair of operators, which is a pair of inverted single-quotes "", and PHP will try to run the command between the reverse single-pin as the server-side command line, and the value of the expression is the result of the command's operation, such as:
Echo ' ls-l ';
6) Type operator
The "instanceof" operator agrees to check whether an object is an instance of a particular class, such as:
if ($object instanceof SampleClass) echo "Object is an instance of SampleClass";
2.9, operator priority and binding
By low to high for example the following:
Operator Precedence
Binding nature |
Operator |
Left |
, |
Left |
Or |
Left |
Xor |
Left |
and |
Right |
Print |
Left |
= + = = *=/=. =%= &=! = ^= ~= <<= >>= |
Left |
? : |
Left |
|| |
Left |
&& |
Left |
! |
Left |
^ |
Left |
& |
Not relevant |
== != === !== |
Not relevant |
< <= > >= |
Left |
<< >> |
Left |
+ - . |
Left |
* / % |
Right |
! ~ + +--(type) @ |
Right |
[ ] |
Not relevant |
New |
Not relevant |
() |
Third, test function and test variable 3.1, PHP provides a specific type of test function.
Like what:
1) Is_array (): Checks if the variable is an array.
2) is_double (), Is_float (), Is_real (): Check if the variable is a floating-point number, and all functions are the same.
3) Is_long (), Is_int (), Is_integer (): Checks if the variable is an integer. All functions are the same.
4) is_string (): Checks if the variable is a string.
5) Is_bool (): Checks if the variable is a Boolean value.
6) Is_object (): Checks if the variable is an object.
7) Is_resource (): Checks if the variable is a resource.
8) Is_null (): Checks if the variable is null.
9) Is_scalar (): Checks if the variable is a scalar, that is, an integer, Boolean, String, or floating-point number.
Is_numeric (): Checks whether the variable is a numeric or numeric string.
One) is_callable (): Checks if the variable is a valid function name.
3.2. Test variable State
PHP has several functions to test the state of variables.
Like what:
1) isset ()
BOOL Isset (mixed var[, mixed var[, ...])
Issset () requires a variable as a parameter. Suppose this variable exists. Returns TRUE, otherwise false is returned. It is also possible to pass a list of variables separated by commas, assuming that all variables are set, that is, return true.
You can also use unset () corresponding to Isset () to destroy a variable:
void unset (mixed var[, mixed var[, ...])
2) Empty ()
bool Empty (mixed Var)
Empty () can be used to check if a variable exists, and if its value is non-null and non-0, the corresponding return value is TRUE or false.
Iv. Control Statement 4.1, if Else
if (condition) { expression;} ElseIf (condition) { expression;} else { expression;}
4.2. Switch
switch (expression) {case value1: expression; break; Case value2: expression; break; Defalut: expression; break;}
The switch statement works like an if statement. But it agrees that the condition can have more than two possible values. In the switch statement, only the condition is a simple data type, can provide a case statement to handle each condition value, and provide the corresponding action code, in addition to the other default cases to deal with the situation does not provide a specific value.
When a specific case in the switch statement is matched. PHP will run the code under the case until the break statement is encountered, assuming there is no break statement. Switch will run the code in case where all the values below this case are true.
4.3. While
while (condition) { expression;}
4.4. For
for (expression1; condition; expression2) { expression3;}
Expression1 is only run once at the beginning. The initial value of the counter is usually set here.
Before each cycle begins, the condtion expression is tested, assuming that the value of the expression is false and the loop ends.
The expression2 runs at the end of each cycle. The value of the counter is usually adjusted here. The Expression3 is run once in each cycle.
4.5. Do While
do { expression;} while (condition);
4.6. Jump out of control structure
Suppose you want to stop a piece of code from running, there are 3 ways to do it.
1) Suppose you want to terminate a loop. Ability to use break statements. The script starts running from the first statement that follows the body of the loop.
2) Assuming that you want to skip to the next loop, you can use the Continue statement.
3) Assume that you want to end the entire PHP script run, and that you can use the Exit statement.
phalcon--php basic Knowledge (i)