PHP beginners learn variables and constants. In a change process, the constant volume is called a constant, while the variable is a sequential storage space with a name. The following describes the constant amount of the base of PHP variables and constants in a process of change. We call it
ConstantAnd the variable is
VariableIs a sequential storage space with a name. Which of the following is
PHPBasic knowledge of variables and constants.
This tutorial introduces PHP variables in the following sections: how to identify PHP variables, how to declare PHP variables, how to assign values to PHP variables, how to introduce PHP variables, and common PHP variable functions.
This article introduces the definition and usage of PHP constants.
I. how to identify PHP variables
The so-called identifier, which is actually the PHP variable name, is mainly composed of letters, numbers, underscores, and dollar signs ($). The length can be any length and cannot start with a number. remember in PHP, variables are case-sensitive (the built-in functions in the PHP System are case-insensitive ).
Note: it is recommended that you do not use the same name as the built-in function or built-in system variable of the PHP System when defining PHP variables. In addition, when defining PHP variables, to maintain good coding habits, when a variable is composed of multiple words, the first word starts with lowercase letters, and the second word starts with uppercase letters ..., This is my personal suggestion.
II. PHP variable declaration and assignment
Unlike C ++ and other programming languages, PHP can be used when you assign values to variables without prior declaration. '=' is used when you assign values to variables '. For example
- $ Test = 'Welcome to www.leapsoul.cn. here is the latest PHP Getting Started Tutorial ';
III. PHP variable types
Like other languages, PHP variable types also support integer, string, array, and object types. The difference is that other languages, such as C language, need to declare the data type of the variable before using the variable, the data type of the PHP variable does not need to be declared in advance. it is determined when you assign a value to it. For example
- $ Leapsoul = 1; // defines the data type of the PHP variable as an integer.
- $ Leapsoul = array (); // defines the data type of the PHP variable as an array
- $ Leapsoul = "Welcome to www.leapsoul.cn. This article describes how to learn PHP variables and constants"; // defines the data type of PHP variables as strings
In PHP, there is a special data type-variable, which enables us to dynamically modify the variable name. previously we mentioned that the definition of PHP variables starts with the dollar sign ($, if you add a dollar sign ($) at the beginning, it becomes an indefinite variable, that is
- $ Test = 'leapsoul ';
- $ Test = "Welcome to www.leapsoul.cn. This article describes how to learn PHP variables and constants ";
Equivalent
- $ Leapsoul = "Welcome to www.leapsoul.cn. This article describes how to learn PHP variables and constants ";
IV. PHP variable functions
The PHP variable function is mainly used to determine the data type and existence of the variable. the functions used to test the PHP variable type include:
Gettype (): return the data type of the passed variable. if it is not a standard data type, such as integer, string, array, and object, the unknown type is returned;
Settype (): change the data type of the passed variable, similar to forced type conversion.
- $ Test;
- $ Leapsoul = "PHP variable and constant learning in the PHP Getting Started Tutorial ";
- Echo gettype ($ test); // output NULL
- Echo gettype ($ leapsoul); // the output variable type is string.
- Settype ($ leapsoul, "int"); // set the $ leapsoul variable type to int.
- Echo gettype ($ leapsoul); // The Data variable type is integer.
Functions that determine whether PHP variables are specific data types include
Is_array (): determines whether the PHP variable type is an array type.
Is_string (): determines whether the PHP variable type is string.
Is_object (): determines whether the PHP variable type is an object type.
For more similar functions, refer to the help documentation of PHP.
Function for testing the existence of PHP variables
The isset and empty functions are mainly used. The difference is that the isset function is used to determine whether the variable exists. if the variable exists, true is returned; otherwise, false is returned, the empty function is used to determine whether the value of this variable is null, or whether the value is assigned. if it is null, true is returned. otherwise, false is returned, these two functions are very useful when submitting PHP form variables to the backend for processing. in principle, we first use isset to determine the existence of variables. if a variable exists, you can use the empty function to determine the required variable values.
Now we have finished the introduction of PHP variables in the PHP getting started tutorial. let's take a look at how to use and define PHP constants.
How to define and use PHP constants
PHP constants are defined by the define function. the constant names generally use uppercase letters. once a constant is defined, it cannot be changed during the script process. generally, in the development of large projects, we usually put some common functions and constants in a configuration file in advance and include them in the configuration file for ease of management.
- Define ("INTRO", "This code shows how to define and use PHP constants ");
- Echo INTRO;
Through the code example above, we can see that the difference between a PHP constant and a variable is that when a constant is used, there is no dollar sign before it. you only need to use its name, the variable is used with a dollar sign.
You can see more PHP System Constants and environment variables through the phpinfo () function. PHP environment variables $ _ SERVER and system constants
Now, the basic knowledge of PHP variables and constants in the basic syntax of the PHP getting started tutorial has been introduced. in the next PHP getting started tutorial, I will mainly introduce the definition and use of PHP functions, this section also introduces the scope of PHP variables.
Original article address:Http://www.leapsoul.cn /? P = 427
Variable is a sequential storage space with a name. The following describes the base of PHP variables and constants...