Home >Backend Development >PHP Problem >How to set variables in php

How to set variables in php

藏色散人
藏色散人Original
2021-11-02 10:04:263171browse

How to set variables in php: 1. Create a PHP sample file; 2. Set variables through "$ symbol variable name", code such as "$txt="Hello world!";".

How to set variables in php

The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer

How to set variables in php

PHP variables

Variables are "containers" used to store information:

Similar to algebra, PHP variables can be assigned a certain value (x=5) or the expression (z=x y).

Variables can have very short names (such as x and y) or more descriptive names (such as age, carname, totalvolume).

PHP variable rules:

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

The variable name must start with a letter or underscore character

Variable names can only contain letters, numbers and underscores (A-z, 0-9 and _)

Variable names cannot contain spaces

Variable names are case-sensitive ($y and $Y are two different variables)

lamp PHP statements and PHP variables are case-sensitive.

Create (declare) PHP variables

PHP has no command to declare variables.

A variable is created when you first assign a value to it:

Example

<?php
$txt="Hello world!";
$x=5;
$y=10.5;
?>

In the execution of the above statement, the variable txt will hold the value Hello world!, And the variable x will hold the value 5.

Note: When you assign a text value to a variable, please add quotation marks around the text value.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to set variables in php. 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