Php 简明教程

PHP - Type Juggling

PHP 被称为动态类型语言。PHP 中变量的类型会动态更改。此特性在 PHP 中称为“类型转换”。

PHP is known as a dynamically typed language. The type of a variable in PHP changes dynamically. This feature is called "type juggling" in PHP.

在 C、C++ 和 Java 中,您需要在随后代码中使用变量之前声明该变量及其类型。该变量只能取与声明的类型相匹配的值。

In C, C++ and Java, you need to declare the variable and its type before using it in the subsequent code. The variable can take a value that matches with the declared type only.

在 PHP 中既不需要显式类型声明变量,也不支持显式类型声明。因此,PHP 变量的类型由分配给它的值决定,反之亦然。此外,当变量被分配了不同类型的值时,其类型也会更改。

Explicit type declaration of a variable is neither needed nor supported in PHP. Hence the type of PHP variable is decided by the value assigned to it, and not the other way around. Further, when a variable is assigned a value of different type, its type too changes.

Example 1

了解 PHP 中的以下变量赋值。

Look at the following variable assignment in PHP.

<?php
   $var = "Hello";
   echo "The variable \$var is of " . gettype($var) . " type" .PHP_EOL;

   $var = 10;
   echo "The variable \$var is of " . gettype($var) . " type" .PHP_EOL;

   $var = true;
   echo "The variable \$var is of " . gettype($var) . " type" .PHP_EOL;

   $var = [1,2,3,4];
   echo "The variable \$var is of " . gettype($var) . " type" .PHP_EOL;
?>

它将生成以下 output

It will produce the following output

The variable $var is of string type
The variable $var is of integer type
The variable $var is of boolean type
The variable $var is of array type

你会看到,“$var” 的类型会根据分配给它的值动态改变。PHP 的这一特性被称为“类型混用”。

You can see the type of "$var" changes dynamically as per the value assigned to it. This feature of PHP is called "type juggling".

Example 2

在计算表达式时也会进行类型混用。在这个示例中,包含数字的字符串变量会自动转换为整数以评估加法表达式。

Type juggling also takes place during calculation of expression. In this example, a string variable containing digits is automatically converted to integer for evaluation of addition expression.

<?php
   $var1=100;
   $var2="100";
   $var3=$var1+$var2;
   var_dump($var3);
?>

以下是它的 @{s0}

Here is its output

int(200)

Example 3

如果一个字符串以数字开头,在执行计算时会忽略后面的非数字字符(如果有)。然而,PHP 解析器会发出如下所示的通知 −

If a string starts with digits, trailing non-numeric characters if any, are ignored while performing the calculation. However, PHP parser issues a notice as shown below −

<?php
   $var1=100;
   $var2="100 days";
   $var3=$var1+$var2;
   var_dump($var3);
?>

您将获得以下内容 output

You will get the following output

int(200)

PHP Warning:  A non-numeric value encountered in /home/cg/root/53040/main.php on line 4

Type Casting vs Type Juggling

注意,PHP 中的“类型转换”与“类型混用”略有不同。

Note that "type casting" in PHP is a little different from "type juggling".

  1. In type juggling, PHP automatically converts types from one to another when necessary. For example, if an integer value is assigned to a variable, it becomes an integer.

  2. On the other hand, type casting takes place when the user explicitly defines the data type in which they want to cast.

Example

类型转换强制将变量用作某种类型。以下脚本展示了不同的类型转换运算符示例 −

Type casting forces a variable to be used as a certain type. The following script shows an example of different type cast operators −

<?php
   $var1=100;
   $var2=(boolean)$var1;
   $var3=(string)$var1;
   $var4=(array)$var1;
   $var5=(object)$var1;
   var_dump($var2, $var3, $var4, $var5);
?>

它将生成以下 output

It will produce the following output

bool(true)
string(3) "100"
array(1) {
  [0]=>
  int(100)
}
object(stdClass)#1 (1) {
  ["scalar"]=>
  int(100)
}

Example

还可以通过用双引号字符串括起来的方式将变量转换为字符串 −

Casting a variable to a string can also be done by enclosing in double quoted string −

<?php
   $var1=100.50;
   $var2=(string)$var1;
   $var3="$var1";
   var_dump($var2, $var3);
?>

在这里,你会得到以下 output

Here, you will get the following output

string(5) "100.5"
string(5) "100.5"