Home >Backend Development >PHP Tutorial >phpmaster | PHP Variables

phpmaster | PHP Variables

William Shakespeare
William ShakespeareOriginal
2025-03-03 08:25:15761browse

PHP variable: Flexible handling of changing values ​​in the program

Core points

  • PHP variables are used to represent possible changes in the program, allowing common code to process any input values, simplifying data processing and increasing efficiency.
  • PHP variables are created by programmers and have names that start with the $ 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.
  • In PHP, variable assignment is done by writing variable names and then values. PHP is a loosely typed language that automatically converts variables to the correct data type based on their value.
  • Use 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.

phpmaster | PHP Variables

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:

    The
  1. name begins with the $ symbol.
  2. $ The first character after the symbol must be a letter or an underscore.
  3. Subsequent characters can be a combination of letters, numbers and underscores.

$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>
Now that you understand the basics of variable naming and assignment, let's look at this example and see if you can figure out the answer:

<?php echo "Hello, $name. Your favorite color is $color.";
The example in the previous section shows that the value to the right of the

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

Show the value of the variable

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>
Maybe you want to make the example more meaningful by adding some quotation marks before the variable content:

<code>Hello, Susan. Your favorite color is yellow.</code>
The dot between the text in quotes and the variable name is the connection operator. It concatenates the values ​​of strings and variables together. You can avoid using connections and instead use

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 will automatically interpolate strings enclosed in double quotes. If you want the name of a variable to be displayed with text, you can use a backslash before the variable name:

<?php
$total = 0;                              // 整数
$total = "Year to Date";                 // 字符串
$total = true;                           // 布尔值
$total = 100.25;                         // 双精度浮点数
$total = array(250, 300, 325, 475);      // 数组
Alternatively, PHP does not perform interpolation on strings enclosed in single quotes. Therefore, this is an equally valid statement:

<?php
$firstNumber = 4;
$secondNumber = 6;
$result = $firstNumber + $secondNumber;
For more information about variables, check out the PHP documentation. You'll review everything you've learned here and learn what special variables are that PHP will automatically define and provide to your script, how variables are bound to the context that declares it, and even how variables are used as names for other variables!

(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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn