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

慕斯
Release: 2023-03-09 19:58:01
Original
1995 people have browsed it

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;
?>
Copy after login

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;
?>
Copy after login

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!

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