Home  >  Article  >  Backend Development  >  Sample code sharing about variable declaration and variable naming rules in PHP

Sample code sharing about variable declaration and variable naming rules in PHP

黄舟
黄舟Original
2017-07-26 13:23:452596browse

What are the rules for naming variables in PHP? I believe you all know the simple but detailed rules. Let’s take a look at the declaration of PHP variables and the introduction of variable naming rules.

Which of the following three ways of writing is correct?

In PHP programs, what are the naming rules for variables? Which of the following three ways of writing is correct?

Type 1:

<?php
  $myname=&#39;aa&#39;;
?>

Type 2:

<?php
  $myName=&#39;aa&#39;;
?>

Type 3:

<?php
  $myname=&#39;aa&#39;;
?>


Experience sharing

Name, age:

<?php
/*
*变量名字以$开头
*变量的名称声明时一定要有意义
*变量不能以数字开头命名,可以用下划线开头,$和变量名不能有空格
*变量名中不能出现运算符号:加减乘除
*别的编程语言变量可能不能出现系统关键字,不过php可以,因为php有一个$
*/
$name = "毕恩竹";
$age = 20;
$php = 40;
echo $age;

The variable name area is only partially cased

<?php
/*
$int和$INT区分大小写,echo不区分大小写
*/
$int = 10;
$INT = 20;
echo $int,"<br>";
eCho $INT;

Camel case naming method: one two three

<?php
$oneTwoThree = 10;

Variables Is a container used for temporary storage of values. These values ​​can be numbers, text, or much more complex permutations. is a simple tool for tracking almost any type of information.
PHP is a very weakly typed language. In most programming languages, variables can only hold one type of data, and this type must be declared before using the variable, such as in C language. In PHP, the type of the variable must be declared before using the variable, such as in C language. In PHP, the type of a variable is usually not set by the programmer. Rather, it is determined at runtime (that is, the value of the variable) based on the context in which the variable is used. PHP does not require you to declare variables before using them; you create the variable the first time you assign it a value.
PHP variable declaration starts with the $ symbol, followed by uppercase and lowercase letters, numbers and underscores, but cannot start with a number.

<?php
$a=100;                            //声明一个变量a,赋予整数100
$b=”string”;                      //声明一个变量b,赋予字符串string
$c=true                            //声明一个变量c,赋予布尔值true
$d=99.99;                        //声明一个变量d,赋予浮尔点99.99
$key=$a;                         //声明一个key变量,并将a变量的值赋予
$a=$b=$c=$d=”value”      //同时声明多个变量,并赋予相同的值

You can use function () to release the specified variable, the isset() function checks whether the variable is set, and empty() counts to check whether a variable is empty
Declare multiple variables at the same time

<?php
$a = $b = $c =$d =10;
echo $a;
echo "<br>";
echo $b;
echo "<br>";
echo $c;
echo "<br>";
echo $d;
echo "<br>";
var_dump();可以打印输出变量类型和内容
<?php
var_dump(10);
$a=100;
var_dump($a);

isset() function detects whether the variable is set

<?php
$bool = isset($a);
var_dump($bool);
$a = 20;
$bool = isset($a);
var_dump($bool);
unset();删除一个变量
<?php
$a = 20;
unset($a);
$bool = isset($a);
var_dump($bool);
empty()数数检查一个变量是否为空,空的话返回true
<?php
$a = 20;
$b = &#39;&#39;;
$c = 0;
$d = null;
$e = &#39; &#39;;
var_dump(empty($a));
var_dump(empty($b));
var_dump(empty($c));
var_dump(empty($d));
var_dump(empty($e));
var_dump(empty($f));

Go back to the beginning of the article, let’s see how to answer it

Summary

1. There are no certain rules for everyone's preferences

2. The current PHP is not case-sensitive for variables (it is said that PHP6 will be case-sensitive), so $myName='aa'; is written as $myname='aa' ; is inconvenient but less readable than $my_name='aa'; refreshing

3. For case-sensitive languages, it is generally recommended to use "Hungarian notation"

Variable name It consists of the variable type and a number of words starting with a capital letter that represent the meaning of the variable.
For example,

$myname=&#39;aa&#39;;

is written as

$sMyName=&#39;aa&#39;;

and

$myname=1;

is written as

$iMyName=1;

4. For case-insensitive languages, it is generally recommended that the variable name consists of several words connected by underscores that represent the meaning of the variable

For example,

$myname=&#39;aa&#39;;

is written as

$my_name=&#39;aa&#39;;

The above is the detailed content of Sample code sharing about variable declaration and variable naming rules 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