One way to store data in php is the amount that i can change. This method is to open up a space in the memory that can store data, and give this space a name, the space at this time can be called a variable. This value can be changed during operation
The name of the current space is the variable name, and the data (eight data types) of the current space is called the variable value
Definition Variables and assign values (recommended learning: PHP video tutorial)
Define variables: $variable name; Note that variables defined in this way are OK, but cannot be used, and an error will be reported if output directly , must be followed by a variable value before it can be used
notice: Note
undefined: undefined
variable: variable
Define variables and assign values: $ Variable name = variable value;
<?php// $a;// echo $a;$a =1;echo $a;?>
Rules for defining variables
Variable names are case-sensitive
Variable name It is recommended to use meaningful names. When you see the variable name, you will know what it means, such as $name, $age, $sex, $get_user_name
Naming rules: it must be composed of numbers, letters, and underscores and cannot start with Start with numbers. For example: $a1, $A1, $a_1, $_a1, $_2 are all acceptable but cannot be $1a.
Three naming rules:
Camel case naming method: the first letter of the first list is lowercase, and the other letters are uppercase. For example, $getUserName
underscore nomenclature: each word is separated by an underscore. For example, $get_user_name
Pascal nomenclature: the first letter of each word is capitalized. For example: $GetUserName
Several commonly used functions for variables:
echo: Print the value of the variable
var_dump(): Print the type of the variable , length, value
isset(): Determine whether the variable exists. If the variable exists and is not null, it returns true, otherwise it returns false
unset(): Destroy the variable
Eight data types of variables
Scalar types: int (integer type), float (floating point type), boolean (Boolean type), string (string type)
Composite types: array (array), object (object)
Special types: null (empty), resource (resource)
int: integer type
Integer types include positive integers, negative integers and 0. Just write numbers directly when defining variable assignments
float: floating point type
Description: Including decimals, negative decimals, positive decimals and 0.0
string: String type
Description: As long as it is any type of data plus single quotes or Double quotes are the string type,
boolean: Boolean type
Description: The Boolean type has only two values, true and false, and the Boolean value is combined with some other methods For example, use process control
array array
Description: An array is a special variable that can store multiple values in a single variable
object object
Empty type
Description: A way to define an assignment to a variable without any value
resource resource
Note: It is not a specific value, but a collective name for a variety of data
The above is the detailed content of What are the basic data methods for variables in php. For more information, please follow other related articles on the PHP Chinese website!