Basic Learning notes for php and basic learning notes for php. Php Basics study notes, php Basics This article introduces in detail the most basic content of php learning, including basic data types, variable types, operators, etc, php entry notes
This article describes in detail the most basic content of php learning, including basic data types, variable types, operators, and other things that will be exposed to beginners.
-
Basic data types supported by php:
Integer: Integer
Float (Double, Double): floating point, indicating all real numbers
String: String
Boolean: Boolean, true/false
Array: stores multiple data items of the same type.
Object: Object, the instance that saves the class
NULL: a variable that is not assigned a value, has been reset, or has been assigned a special value of NULL.
A specific built-in function (such as a database function) will return a resource-type variable.
Four scalar types:
String (string)
Integer)
Float (float, also double)
Boolean (boolean)
Two composite types:
Array)
Object)
Two special types:
Resource)
NULL)
View variable types
The gettype () function allows you to conveniently view the type of a variable:
The code is as follows:
$ Var_bool = TRUE; // a boolean
$ Var_str = "foo"; // a string
$ Var_int = 12; // an integer
Echo gettype ($ var_bool); // output boolean
Echo gettype ($ var_str); // output string
Echo gettype ($ var_int); // output integer
?>
Prompt
For historical reasons, for float data, the gettype () function returns double instead of float.
To view the value and type of an expression, use the var_dump () function.
Judge variable type
To determine the next logical action by determining the variable type, use the is_type series functions instead of gettype:
The code is as follows:
$ Var_int = 12;
// If $ var_int is of the int type, add
If (is_int ($ var_int )){
$ Var_int = $ var_int + 4;
}
Echo $ var_int; // output 16
?>
Both NULL and resource represent external resources, such as database connections.
Basically, a resource variable cannot be operated directly, but they are usually returned by the function and must be passed as parameters to other functions.
In php, the variable type is determined by the value assigned to the variable. (Dynamic language)
PHP can determine the type of a variable based on the value stored in the variable at any time, that is, it can transparently change the type of the variable at any time.
PHP automatically retrieves the input data type. Once a variable value is retrieved from the variable, it returns data of the same data type.
Habit: constant names are composed of uppercase letters. This makes it easy to distinguish between variables and constants.
A major difference between a constant and a variable is that when a constant is referenced, there is no $ symbol before it.
Another difference between constants and variables is that constants can only store Boolean, integer, floating point, or string data. these types are scalar data.
Variable type change (juggling)
PHP does not need (or does not support) to specify the variable type in the name variable; the type of a variable is determined by the relationship between the variables used, that is, if you assign a string value to a variable var, var becomes a string variable. If you assign an integer to var, it becomes an integer variable. An example of PHP's automatic conversion of variable types is the addition operator '+ '. If any operand is a double-precision number, all operands are evaluated as double-precision numbers, and the result is also a double-precision number. Otherwise, the operand is considered an integer, and the result is also an integer. Note that this does not affect the variable type of each operand. the only change is how the operand is processed during calculation. $ Foo = "0"; // $ foo is a string with a value of "0" (ASCII 48)
PHP code
The code is as follows:
$ Foo = "http://www.hzhuti.com/HTC/G11/"; // $ foo is a string with a value of "0" (ASCII 48)
$ Foo ++; // $ foo is a string with the value "1" (ASCII 49)
$ Foo + = 1; // $ foo is now an integer (2)
$ Foo = $ foo + 1.3; // $ foo is a number of double precision (3.3 ).
$ Foo = 5 + "10 Little Piggies"; // $ foo is an integer (15)
$ Foo = 5 + "10 Small Pigs"; // $ foo is an integer (15)
If you think the last two expressions in the above example seem a bit strange, please refer to the "string conversion" section. If you want to force a variable to be calculated as a fixed type, see the "casting" section. If you want to change the type of a variable, see the description of the function "settype.
Determine the type of a variable
Because PHP decides the type of variables and converts them as needed, the type of a specific variable is not very obvious at any time. PHP includes some functions to find out the type of this variable. These functions are gettype (), is_long (), is_double (), is_string (), is_array (), and is_object ().
Type casting)
In PHP, the type is forced to be roughly the same as in C: write the required type in parentheses before the variable to be forced.
PHP code
The code is as follows:
$ Foo = 10; // $ foo is an integer.
$ Bar = (double) $ foo; // $ bar is a double number.
The following mandatory methods are allowed: (int), (integer)-forced to be an integer (real), (double), (float)-forced to be a double precision (string) -force to string (array)-Force to array (object)-Force to object. Note that in parentheses, tabs (tabs) and spaces (spaces) are allowed ), therefore, the following statement is equivalent: $ foo = (int) $ bar;
String conversion
When a string is calculated as a numeric value, its results and types are determined as described below. If the string contains characters '.', 'e', or 'e', it is treated as a double-precision variable. Otherwise, it is treated as an integer. The value of this string is determined by the first part of the word. If the string starts with any valid numeric data, the numeric data is the value of this string. Otherwise, the value is zero ). Valid numeric data follows the following tags, followed by one or more numbers (which can contain decimal points), followed by an optional index. An index consists of one or more numbers following 'E' or 'e.
PHP code
The code is as follows:
$ Foo = 1 + "10.5"; // $ foo is a double precision (11.5)
$ Foo = 1 + "-1.3e3"; // $ foo is a double precision (-1299)
$ Foo = 1 + "bob-1.3e3"; // $ foo is an integer (1)
$ Foo = 1 + "bob3"; // $ foo is an integer (1)
$ Foo = 1 + "10 Small Pigs"; // $ foo is an integer (11)
$ Foo = 1 + "10 Little Piggies"; // $ foo is an integer (11 );
// This string contains the character 'e'
Identifier
Operator:
1) value assignment operator: =
2) arithmetic operators: +,-, *,/, % (modulo)
3) join operator:. no matter what the operand is, it is treated as a String and the result returns a String.
4) Total value Assignment operator (Combined Assignment Operators): + =, * =,/=,-=, % =,. =
5) auto increment/Decrementing operator (Automatically Incrementing and Decrementing ):
(1) $ variable + = 1 variable $ variable ++; $ variable-= 1 variable $ variable-. perform other operations like C, and then ++ or-
(2) ++ $ variable,-$ variable, first ++ or-, and then perform other operations
6) comparison operators:
= (Left equals to right ),! = (Left is not equal to right ),
===( The left side is equal to the right side, and the data type is the same ),
>=, >,<, <=
7) logical operators: | or, & and, xor (returns true if only one of the left and right sides is true ),!
A reference table
Arithmetic Operators Operator
Description
Description Example
Case Result
Result
+ Addition
Plus sign x = 2
X + 2 4
-Subtraction
Minus x = 2
5-x 3
* Multiplication
Multiplication number x = 4
X * 5 20
/Division
Division number 15/5
5/2 3
2.5
% Modulus (pision remainder)
Modulus (remainder) 5% 2
10% 8
10% 2 1
2
0
++ Increment
Auto-increment x = 5
X ++ x = 6
-- Decrement
Auto-subtract x = 5
X -- x = 4
Assignment Operators
Assignment Operators)
Operator
Symbol Example
Case Is The Same
Equivalent
= X = y
+ = X + = y x = x + y
-= X-= y x = x-y
* = X * = y x = x * y
/= X/= y x = x/y
% = X % = y x = x % y
Comparison Operators
Comparison Operators)
Operator
Description
Example
Case
= Is equal
Returns false if the value is 5 = 8.
! = Is not equal
Not equal to 5! = 8 return true
> Is greater
If the value is greater than 5> 8, false is returned.
<Is less
Less than 5 <8 returns true
> = Is greater than or equal
Returns false if the value is greater than or equal to 5> = 8.
<= Is less than or equal
Return true if the value is less than or equal to 5 <= 8
Logical Operators
Logical Operators)
Operator
Description
Description Example
Case
& And
And x = 6
Y = 3
(X <10 & y> 1) returns true.
| Or
Or x = 6
Y = 3
(X = 5 | y = 5) false
! Not
Non x = 6
Y = 3
! (X = y) returns true.
Other operators:
Ternary operators, error suppression operators, execution operators, array operators, type operators
Operator "? "Previous expression
If an expression containing binary operators appears in the ternary operator "? "? "Before, you should add a pair of parentheses to the expression. For example:
(X> = 0 )? X:-x;
Next let's look at a simple one? Number expression instance
The code is as follows:
$ A = 'http: // www.zhutiai.com/zhuti/c7 /';
Echo $? 'True': 'false ';
The output result is trur;
Check again
If ($)
{
Echo 'true ';
}
Else
{
Echo 'false ';
}
Summary This article describes in detail the most basic content in php learning, including basic data types, variable types, operators, and so on...