Php 简明教程

PHP - Variables

PHP 中的变量是一个命名内存位置,它保存属于某种数据类型的数据。

A variable in PHP is a named memory location that holds data belonging to one of the data types.

  1. PHP uses the convention of prefixing a dollar sign ($) to the name of a variable.

  2. Variable names in PHP are case-sensitive.

  3. Variable names follow the same rules as other labels in PHP. A valid variable name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

  4. As per the naming convention, "$name", "$rate_of_int", "$Age", "$mark1" are examples of valid variable names in PHP.

  5. Invalid variable names: "name" (not having $ prefix), "$rate of int" (whitespace not allowed), "$Age#1" (invalid character #), "$11" (name not starting with alphabet).

变量使用“=”运算符赋值,左侧是变量,右侧是要计算的表达式。

Variables are assigned with the "=" operator, with the variable on the left hand side and the expression to be evaluated on the right.

No Need to Specify the Type of a Variable

PHP 是一种动态类型语言。不需要指定变量的类型。相反,变量的类型由分配给它的值决定。变量的值是其最近赋值的值。

PHP is a dynamically typed language. There is no need to specify the type of a variable. On the contrary, the type of a variable is decided by the value assigned to it. The value of a variable is the value of its most recent assignment.

看看下面的 example

Take a look at this following example

<?php
   $x = 10;
   echo "Data type of x: " . gettype($x) . "\n";

   $x = 10.55;
   echo "Data type of x now: " . gettype($x) . "";
?>

它将生成以下 output

It will produce the following output

Data type of x: integer
Data type of x now: double

Automatic Type Conversion of Variables

当有必要时,PHP 会很好地自动将类型从一种转换为另一种类型。在下面的代码中,PHP 将字符串变量“y”转换为“int”,对另一个整数变量执行加法,并将结果打印为 30。

PHP does a good job of automatically converting types from one to another when necessary. In the following code, PHP converts a string variable "y" to "int" to perform addition with another integer variable and print 30 as the result.

看看下面的 example

Take a look at this following example

<?php
   $x = 10;
   $y = "20";

   echo "x + y is: ", $x+$y;
?>

它将生成以下 output

It will produce the following output

x + y is: 30

Variables are Assigned by Value

在 PHP 中,变量始终按值分配。如果将表达式分配给变量,则原始表达式的值将被复制到其中。如果分配后表达式中任何变量的值发生变化,它不会对分配的值产生任何影响。

In PHP, variables are always assigned by value. If an expression is assigned to a variable, the value of the original expression is copied into it. If the value of any of the variables in the expression changes after the assignment, it doesn’t have any effect on the assigned value.

<?php
   $x = 10;
   $y = 20;
   $z = $x+$y;
   echo "(before) z = ". $z . "\n";

   $y=5;
   echo "(after) z = ". $z . "";
?>

它将生成以下 output

It will produce the following output

(before) z = 30
(after) z = 30

Assigning Values to PHP Variables by Reference

您还可以通过引用方式将值分配给 PHP 变量。在这种情况下,新变量仅引用或成为原始变量的别名或指向原始变量。新变量的更改会影响原始变量,反之亦然。

You can also use the way to assign values to PHP variables by reference. In this case, the new variable simply references or becomes an alias for or points to the original variable. Changes to the new variable affect the original and vice versa.

要按引用分配,只需在正在分配的变量(源变量)的开头添加一个和号 (&)。

To assign by reference, simply prepend an ampersand (&) to the beginning of the variable which is being assigned (the source variable).

看看下面的 example

Take a look at this following example

<?php
   $x = 10;
   $y = &$x;
   $z = $x+$y;
   echo "x=". $x . " y=" . $y . " z = ". $z . "\n";

   $y=20;
   $z = $x+$y;
   echo "x=". $x . " y=" . $y . " z = ". $z . "";
?>

它将生成以下 output

It will produce the following output

x=10 y=10 z = 20
x=20 y=20 z = 40

Variable Scope

作用域可以定义为变量在其中声明的程序中可用性的范围。PHP 变量可以是四种作用域类型之一−

Scope can be defined as the range of availability a variable has to the program in which it is declared. PHP variables can be one of four scope types −

Variable Naming

命名变量的规则是−

Rules for naming a variable is −

  1. Variable names must begin with a letter or underscore character.

  2. A variable name can consist of numbers, letters, underscores but you cannot use characters like + , - , % , ( , ) . & , etc

变量没有大小限制。

There is no size limit for variables.