About initialization and assignment variables in the way of php

What is variable

It is a popular variable container. Depending on the type of variable, like the container sizes, naturally the size of the data stored is not the same. Data stored in a variable, we call variable values.

Variables in PHP are represented by a dollar sign followed by the variable name. Variable names are case-sensitive. Naming variables in PHP usually begin with a letter or an underscore, letters, numbers, underscores. Variable names are usually represented by a variable declaration meaning of the English word composition. Between words separated by _ or the first letter of the first word lowercase, after each word capitalized.

This we call the hump naming nomenclature. Good naming convention helps to improve the readability of our code.

E.g:

<?php

    $name = '屋脊猫';

    $_age = 1;

    $sex = '男';        

    echo "姓名:{$name},年龄:{$_age}, 性别:{$sex}";

    $1sex = '男';  // 错误,不能以数字开头

?>

Variable initialization

Variable is initialized to the variable to set a default value (or values ​​required), the initialization process PHP variables will allocate storage space as well as storage space where the value of the variable address is stored in a variable.

<?php

    $a = 1; // 初始化一个整形变量

?>

Although not required to initialize variables in PHP, but initialize variables is a good habit. Uninitialized variables have a default value for its type

● Boolean variables default value is FALSE

● shaping and float variables default value is zero

Default ● string variable (for example, echo) is an empty string

● The default value is an empty array of array variables

Uninitialized variables E_NOTICE error will be issued, but not when the cell array from an uninitialized.

Dependence uninitialized variables have a default value in some cases issues, such as after the encoding, we often need to include a file into the current file through include, if two files have the same variable name, this time the variable value is not what we need is the default value. Also in versions prior to PHP 5.4.0 register_globals in the open is a major security risk. This configuration has been removed after the PHP 5.4.0.

Variable assignment mode

There are two ways by value in PHP, respectively assignment by value and reference assignment.

1, assignment by value

PHP assignment by value is the default by value. That is to say, when the value of an expression to a variable, the entire value of the original expression is copied into the destination variable. This means that when the value of a given variable of another variable, change the value of one variable, will not affect another variable. Such as:

<?php

    $var1 = 1;

    $var2 = $var1;

    $var1 = 3;

    echo $var1;     // 3

    echo $var2;     // 1

?>

2, reference assignment

Assignment by reference means that the new variable simply references the original variables, change the values ​​of the new variables will affect the value of the original variable, and vice versa. Assign by reference, a & simply added to the former symbol will be assigned the variable (source variable) such as:

<?php

    $oldVar = 1;

    $newVar = &$oldVar;

    $newVar = 2;

    echo $oldVar;   // 2

    echo $newVar;   // 2

?>

Ampersand in C language is called fetch address, variables stored in the variable value stored in the address memory, the address by taking the address character may be a variable that holds the value of the variable is assigned to another variable. The values ​​of two variables point to the same memory address, so when we modify the value of one variable, the value of another variable will naturally be changed.

Important thing must be noted that there is only the name of the variable can only be assigned by reference.

<?php

    $foo = 25;

    $bar = &$foo;      // 合法的赋值

    $bar = &(24 * 7);  // 非法; 引用没有名字的表达式

?>

In (24 * 7) this expression, because there is no stored in a variable, so there is no clear memory address, by taking the address character to get the address stored in the memory is not desirable.

Published 36 original articles · won praise 20 · views 10000 +

Guess you like

Origin blog.csdn.net/monkeyapi/article/details/104004317