Home  >  Article  >  Backend Development  >  How to add variables in PHP? (Source code attached)

How to add variables in PHP? (Source code attached)

慕斯
慕斯Original
2021-05-20 16:18:221933browse

What are PHP variables, and how to set PHP variables? In this chapter, let the "newbie" take you to understand PHP for the first time, and how to become a PHP master little by little. Let's chat together

How to add variables in PHP? (Source code attached)

##What are variables?

Variables are "containers" used to store information;

The code is as follows:

<?php
$x = 3;
$y = 7;
$z = $x + $y;
echo $z;
?>

Just Like algebra in mathematics:

x=3 y=7 z=x y

In mathematics, letters are generally used, such as x And the assigned value is 3;

From the above expression,

z=x y, so we can get the result to be 10;

Therefore, in PHP, these letters are are called variables.

Summary: Variables are the capacity used to store data.

For PHP variables, it is similar to a high number. It can not only be assigned a value or an expression, but also a short name (such as x, y) or even a descriptive name (such as: name age, etc.)

PHP variable rules:

1. Variables start with the $ symbol, followed by the name of the variable

2. The variable name must start with a letter or underscore character

3. The variable name can only contain alphanumeric characters and underscores (A-z, 0-9 and _)

4. Variable names cannot contain spaces

5. Variable names are case-sensitive (

$y and $Y are two different variables)

Create PHP variables

For PHP, there is no command to declare variables. The variable is created when it is assigned for the first time;

The code is as follows;

<?php
$txt = "欢迎来到PHP中文网!";
$x = 5;
$y = 11.5;
?>

In the execution of the above statement, the variable txt will save the value, and the variable x will hold the value 5.


Supplement:

PHP variable scope

The scope of the variable is the variable in the script Sections that can be quoted/used.

PHP has four different variable scopes:

  • local (local)

  • global (global)

  • static (static)

  • parameter (parameter)

Recommended study:

PHP video tutorial

The above is the detailed content of How to add variables in PHP? (Source code attached). 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