How to define a PHP variable

  • 2020-03-31 20:15:41
  • OfStack

Definition of variables
The & # 9873; Type of variable
The & # 9873; Use of variables
Variables is exist in memory can changing objects one by one, we might think of memory as a street, there are many households in the street, every household has its own address, it can be in memory address (often referred to in the C language of a concept, we will not discuss here), for we are one of the residents can be said to be the no. 1, no. 1 floor is also can say into wang2 xiao3 ming2, use a name instead of an address. And in wang xiaoming's home some time engraved a few people is a change of quantity, there may be 3 people at noon, only 1 person in the afternoon, there are 5 people in the evening. So, for an address in memory that we want to refer to, we can also call it A, or area. That's the variable.
Let's demonstrate the declaration of variables in PHP.
Use "$" plus a variable name, such as $a, $var_name.
Notice three things about declaring variables in PHP:
, variable names can only be composed of English letters (a-z, a-z), Numbers (0-9) and underscores.
, variable names in PHP are case sensitive, that is, $VAR_NAME and $VAR_NAME are two different variables.
, a variable declaration or assignment must end with a semicolon (;) .
PHP is very simple to specify the type of variables, generally do not need to declare with keywords, with the value of the form of the expression.
For example, declare an integer variable
$x = 100;
Declare a character variable
$STR = "Iam a Chinese!" ;
Declare a Boolean variable
$bool = true;
Use variables in web pages.
For example, we will display a sentence on the website, "I am a Chinese", "I am 28 years old".
<?php 
$str=" I'm a Chinese "; 
$age=28; 
echo$str."<br>"; 
echo" I this year ".$age." At the age of "; 
?> 

Line 1 "< ? "PHP", this is a tag at the beginning of a PHP file, indicating that it is PHP code starting from down.
Line 2 $STR = "I am a Chinese"; , defines a string variable STR whose value is "I am a Chinese".
Line 3 $age = 28; , define an integer variable age and assign it a value of 28.
Line 4 echo $STR. "< Br>" ; Echo is the keyword used for output in PHP, and the content following it indicates the content that needs to be output, that is, $STR is the variable that needs to be output, and after $STR. Br> .
Echo "I am this year ".$age. "Is understood in the same way as line 4. The sentence "I am 28 years old" is divided into three parts. "I am 28 years old" is the first part. 28 is replaced by the variable $age.
Line 6 "? >" The end of the PHP file.
At this point, task 1 ends. At this point, you can express what you want to say in PHP on a web page.

Related articles: