PHP variable: Flexible handling of changing values in the program
Core points
$
symbol followed by letters or underscores. Subsequent characters can be a combination of letters, numbers, and underscores. Consistent naming conventions are essential for writing clear and easy-to-understand code. echo
or print
to display the value of the PHP variable. Interpolation can be used to replace the variable name in the string with its value, making the code easier to read. What are PHP variables?
In PHP scripts, the variable is used to represent values. As the name implies, the value of a variable can be changed during program execution. Variables are one of the key features that distinguish programming languages such as PHP and markup languages such as HTML. Variables allow you to write code in a common way. To illustrate this, consider a web form that requires users to enter their name and favorite color.
The data will be different every time you fill out the form. A user might say his name is John and his favorite color is blue. Another user might say her name is Susan and her favorite color is yellow. We need a way to process the values entered by the user. The way to achieve this is to use variables. PHP will automatically create some standard variables, but in most cases, variables are created by you (the programmer) (or declare ). By creating two variables named $name
and $color
, you can create common code to handle any input value. For John, this code:
<?php echo "Hello, $name. Your favorite color is $color.";
will display:
<code>Hello, John. Your favorite color is blue.</code>
Susan, on the other hand, will see:
<code>Hello, Susan. Your favorite color is yellow.</code>
We will discuss variable names and displaying the values of variables in the rest of this article, but it is important now to understand how using common variables can make data processing easy.
Create variable
In PHP, just write the name of the variable for the first time in the script to create it. You do not need to perform any additional actions. However, variable names must follow some standard rules:
$
symbol. $
The first character after the symbol must be a letter or an underscore. $customerName
is a valid variable name because it follows all three rules mentioned above. 3customer
is invalid because it violates the second rule, and the first character after the $
symbol must be a letter or an underscore. It is best to give variables a meaningful name. If the data you are storing is the customer's name, a sensible name may be $customerName
. You can also call it $abc123
, but I hope you agree with the previous suggestion better. You can write variable names by following different conventions. Whatever you choose, it is important to be consistent and follow that convention throughout the script. For example, you can use underscores to separate words (e.g. $customer_name
), or use capital letters to distinguish words, a style called camel case (e.g. $customerName
). When naming variables, you can use both upper and lowercase letters, but note that $CustomerName
is different from $customerName
. PHP treats these two variables as different variables! This strengthens the necessity of adhering to naming agreements.
Variable assignment
Now that you know you can always make PHP create a new variable by writing a new name, let's look at another example to see how to assign values to them.
<?php echo "Hello, $name. Your favorite color is $color.";
First, the variable $customerName
is assigned the value "Fred". This is called assignment. Since this is the first time using $customerName
, the variable is automatically created. After that, every time you write $customerName
, PHP will know to use the value "Fred". Then, write $customerID
. It is possible to create variables without assigning values to them, but this is not usually considered a good practice. It is better to assign a default value so that you know it has a value. Remember that variables are mutable, so you can change their value at any time. After that, the variable $customerID
is assigned a value of 346646. Finally, the value of $customerID
is assigned to $customerName
. You can assign the value of one variable to another; in this case the value of $customerID
(346646) overrides the value of $customerName
("Fred"), so both variables now represent 346646! Note that the data types referenced by variables have different "types". This attribute is called the data type data type . "Fred" is given in quotes, so it is a string (string is just a fancy name for text). 346646 is obviously a number (more specifically, an integer ). Here are some examples of assignment using different data types:
<code>Hello, John. Your favorite color is blue.</code>
<?php echo "Hello, $name. Your favorite color is $color.";
symbol is assigned to the variable name to the left of the =
symbol, so the value 4 is assigned to =
. Look carefully at the last line. Although I haven't explained it before, the $firstNumber
symbol is an operator, in which case the addition is performed. So what do you think is the value in
? If your answer is 10, then congratulations, this is correct! If not, check the example again and read the instructions carefully. $result
As you can see at the beginning, you can use
to display the values represented by the variable. You can also use echo
if you prefer, because there is almost no difference between the two at this time except that print
is typing less. echo
<code>Hello, John. Your favorite color is blue.</code>
<code>Hello, Susan. Your favorite color is yellow.</code>
interpolation. Interpolation is when a variable name appears in a string and is replaced by its value. Taking advantage of this can sometimes make your code easier to read.
<?php $customerName = "Fred"; $customerID; $customerID = 346646; $customerName = $customerID;
<?php $total = 0; // 整数 $total = "Year to Date"; // 字符串 $total = true; // 布尔值 $total = 100.25; // 双精度浮点数 $total = array(250, 300, 325, 475); // 数组
<?php $firstNumber = 4; $secondNumber = 6; $result = $firstNumber + $secondNumber;
(Survey content, regarding FAQ, can be selectively retained or streamlined as needed, because the original text has been fully pseudo-originalized.)
The above is the detailed content of phpmaster | PHP Variables. For more information, please follow other related articles on the PHP Chinese website!