Home > Backend Development > PHP Problem > What does the php code $sum=0 mean?

What does the php code $sum=0 mean?

青灯夜游
Release: 2023-03-17 18:04:01
Original
3950 people have browsed it

php code "$sum=0" means creating (declaring) a variable "$sum" and initializing it, assigning 0 to the variable. The declaration of a variable in PHP must be represented by the dollar sign "$" followed by the variable name, and the assignment operator "=" is used to assign a value to the variable. PHP variables are identifiers prefixed with the dollar sign "$". An identifier is a symbol that identifies different objects and must comply with some rules: it can only be composed of letters, numbers, underscore characters, and other ASCII characters from 127 to 255. , must start with a letter or underscore, etc.

What does the php code $sum=0 mean?

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

php code "$sum=0" means Create (declare) a variable "$sum" and initialize it, assigning 0 to the variable.

Introduction to PHP variables

Variables in PHP are identifiers prefixed with the dollar sign ($). The identifier is A symbol that identifies different objects, such as the name of a variable, the name of a function, or the name of other user-defined objects. In PHP, the naming of identifiers must comply with the following regulations:

  • Identifiers can consist of one or more characters, but must start with a letter or an underscore. Additionally, identifiers may only consist of letters, numbers, underscore characters, and other ASCII characters from 127 to 255. Identifier names such as my_a, Ss, and _value are all legal, but variable names such as q^a and 4tt are illegal.

  • Identifiers are case-sensitive. Therefore, the variable $recipe is different from the variables $Recipe, $rEciPe or $recipE.

  • Identifiers can be of any length. This is beneficial because it allows the programmer to accurately describe the purpose of the identifier through its name.

  • #The identifier name cannot be the same as any PHP predefined keyword.

php variable declaration

The declaration of variables in PHP must be expressed with a dollar sign followed by the variable name, using the assignment operator (=) Assign a value to a variable. Example:

$sum=0;
Copy after login

In the process of creating a variable, it is a good habit to declare the variable first and then assign a value to the variable.

Since PHP is a weakly typed language, there is no need to explicitly declare variables when declaring variables. Variables can store any type of value. In PHP, variables are type checked at runtime and can be used with another A value of a different type replaces the variable's value.

The following declares a variable, replaces the value of the variable with another value of a different type, and then declares a variable without assignment. The specific code is as follows:

$what = “Yound Tang”;
$what = 25;
$name;
Copy after login

In PHP, There are two ways to assign values ​​to variables, namely value assignment and reference assignment. Value assignment is to directly copy a value to a variable through an assignment expression, which will overwrite the original value of the variable. If there is no assignment when declaring the variable, its behavior will be the same as NULL. Assigning a value when declaring a variable is a commonly used variable assignment method. The usage example is as follows:

<?php
header("Content-type:text/html;charset=utf-8");
$name = "唐晓阳";
$age = "23";
$sex = "男";
echo "你的姓名是:".$name."<br/>";
echo "你的年龄是:".$age."<br/>";
echo "你的性别是:".$sex."<br/>";
?>
Copy after login

Execute this code and the execution result is as follows:

What does the php code $sum=0 mean?

In PHP, the declared variable can be assigned a value directly or not. When a variable needs to be used to store a value, the variable can be assigned a reference. The reference assignment means that the created variable has the same content as that referenced by another variable. Therefore, if multiple variables refer to the same content, modifying any one of them will be reflected in the remaining variables. Reference assignment can be completed by adding an & symbol after the equal sign. An example form of reference assignment is as follows:

$value1="Hello World";
value2=&value1;
$value2="GoodBye";
echo $value1.”<br/>”;
echo $value2.”<br/>”;
Copy after login

In the above code, a variable value1 is created and assigned the value "Hello World". In the following statement, the variable $value2 uses reference assignment, that is, value1 The value of is assigned to value2. At this time, these two variables are a life community. When one changes, the other one will display the result. The execution result of this code is as follows:

GoodBye
GoodBye
Copy after login

Recommended learning : "PHP Video Tutorial"

The above is the detailed content of What does the php code $sum=0 mean?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template