The second step of the php basic tutorial is easy to understand to learn variables, constants and data types

Introduction to variables, constants and data types

In programming, a variable refers to a box, or container, that stores a value. For example, a box is used to put sundries, this box is a container, and the value is sundries. Containers can be divided into many types, such as bottles, quilts, boxes, etc. There are also many types of values, such as kitchen waste, recyclable waste, non-recyclable waste, plastic, clothes, etc. From this we can find that there are different categories for containers and different categories for values. These are called types. In programming, general variables are set to the same type as the stored value.

Variables are described in programming and can be changed while the program is running.
Constants are the opposite of variables and cannot be changed while the program is running.

Variables
In the previous section, it was explained that the php code is written in the <?php ?> tag. The variable should then also be written in the tag.
In php, variables start with $, followed by a variable name. For example, if I create a variable (container) named a, it should be written as $a in the PHP code. I let this container store a number 0, which can be written as $a=0. Suppose I create another variable, the name b can be written as $b, and the value is given when creating the variable, which can be written as $b=0. If you want to add two variables, do the addition operation can be written as $a+$b, then I want to display the added value of variable a and variable b, then the code can be written as echo $a+$b. The complete code is as follows:

<?php
	$a=0;
	$b=0;
	echo $a+$b;
?>

There is a point in the above code. We found that after the end of each line of statement, there needs to be a semicolon at the end. This semicolon is the grammar of the php language. After writing a code, you need to add a semicolon at the end to indicate the end of the statement (All the punctuation marks in the programming are the punctuation marks in the English comfort method).

Save the file, save the file as the WWW root directory, the file name is index.php, and you will directly access the php file when you visit localhost or 127.0.0.1 (be sure to open the service in phpstudy or other integrated software).
The results of the operation are as follows:
Insert picture description here
Since the values ​​of our variable a and variable b are 0, 0+0 is 0, and the output display value is also 0. You can try to modify the value of the variable, a is changed to 1, b is changed to 10, the code is as follows:

<?php
	$a=1;
	$b=10;
	echo $a+$b;
?>

The result is as follows, the value is 11:
Insert picture description here
In the above programming, readers who are new to programming will be confused. As the article said at the beginning, variables also have types, so how does the type reflect here? The reason is that the php language is a weakly typed language, which means that implicit type conversion can be performed. When we assign a value, it is automatically converted to the type matched by the assignment content. In the above code, the value assigned is 0, which is an integer type. At this time, there is no obvious description of the type of the current variable. The type is automatically changed directly according to the variable. Examples will be used in subsequent learning to introduce this.

constant

Just introduced what a variable is, let's look at what a constant is.
From a literal point of view, a variable refers to a variable that can be changed, and a constant refers to an immutable amount. This variable and immutable occurs when the program is running.
For example, when a variable is defined (assigned) as 0 at the very beginning, and then a value (such as 2) is given to the variable, it can be changed at this time, but it cannot be changed if a constant is used.
Here is an example to explain:

<?php
	$a=1;
	echo $a;
	echo '更改后';
	$a=2;
	echo $a;
?>

Although the writing quality of the above code is not good, it can help novices to learn this knowledge point very well.
Code interpretation:

  • $a=1: Define a variable a and assign the value 1

  • echo $a: display the value of variable a

  • echo'after change': output the content after a change

  • $a=2: Re-assign a to 2

  • echo $a: Display the value of a variable after reassignment

The result is as follows:
Insert picture description here
This can be changed, let's try a constant.
The definition of a constant is inconsistent with the variable, so use the define function to define it. A function is a functional block, just like an electric rice cooker. It has the function of cooking rice. You can cook by putting rice in it. The define function has the function of defining constants. To define constants in a specified way, use the define function to complete.
code show as below:

<?php
	define("b", 10);
	echo b;

?>

Code interpretation:

  • define("b", 10): Use define to define a constant, the constant name is b and the value is 10
  • echo b: No need to use the $ sign when outputting constants

Visit localhost in the browser to test, the result is as follows:
Insert picture description here
Then we modify the constant in the form of a variable:

<?php
	define("b", 10);
	echo b;
	b=11
?>

b=11 Modified the value of the constant, and an error will be reported at this time: The
Insert picture description here
constant is not allowed to change the value while the program is running, so the = symbol cannot be used, and a syntax error will occur.

type of data

In php, use the gettype() function to get the data type of the current variable. The function is a function block, and gettype is the function block to get the data type of the current variable.
To use it, fill in the variable name in the parentheses () of the gettype() function, for example:

<?php
	define("b", 10);
	echo gettype(b);
?>

The above code defines a constant b, which is assigned a value of 10, which is an integer, which is an integer type. Use the gettype function to pass in a value b in parentheses. This value b can be collectively referred to as a parameter. At this time, use echo output, gettype will get the type of the passed parameter, and output the value returned by gettype to see this type. The process of transferring ginseng is like throwing rice into a rice cooker when cooking. After the meal is cooked, there will be a result, which is steaming rice.
The result is as follows:
Insert picture description here
integer will be displayed. Integer represents the integer type.

Guess you like

Origin blog.csdn.net/A757291228/article/details/107219885