Home  >  Article  >  Backend Development  >  What characters cannot be included in php variable names?

What characters cannot be included in php variable names?

青灯夜游
青灯夜游Original
2019-10-10 15:18:166308browse

php variables are "containers" used to store information. PHP variables can be assigned values ​​or expressions. So, how should PHP variables be named? What characters cannot be included in php variable names? Let's take a look at the naming rules of PHP variables.

What characters cannot be included in php variable names?

Let’s look at an example first:

<?php
$x=5;
$y=6;
$z=$x+$y;
echo $z;
?>

Similar to algebra, you can assign a certain value (x=5) or an 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 naming rules:

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

● Variable names must start with letters or underscore characters Start

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

● Variable names cannot contain spaces, punctuation marks, and special characters

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

PHP variable naming rules and other notes

1. When naming a variable with two or more words, you can capitalize the first letter of all but the first word. Such as $myName, $yourFamilyName.

2. Variables named starting with underscore _ usually represent special variables. Such as creating protected attributes, PHP predefined variables ($_GET), global arrays, etc. in the class.

3. When defining variables, do not be greedy for brevity. Instead, use descriptive names to define variables.

The above is the detailed content of What characters cannot be included in php variable names?. 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