A brief discussion on the 8 types of PHP variables and weak types

little bottle
Release: 2023-04-06 11:42:02
forward
2930 people have browsed it

In this article, the editor will talk to you about the 8 types of PHP variables and weak types. Interested friends can learn more.

1, 8 types of PHP variables

Four scalar types:

  • boolean (Boolean)
  • integer (integer type)
  • float (floating point type, also calleddouble)
  • string (string)

Two composite types:

  • array (array)
  • object (object)

Finally there are two special types:

  • resource (Resource)
  • NULL (NULL)

Second, PHP weak type

We know that PHP is a It is a weakly typed language (when initializing a variable, you do not need to specify its type), but the previous 8 types of PHP variables are provided. These two sentences seem a bit contradictory. How do we understand the types of PHP?

First of all, there are 8 types at the bottom of PHP.

Secondly, when you initialize or assign a value to a variable, the bottom layer of PHP will automatically identify the value that is about to be assigned, and then set the type of the variable.

# Again, when you make a logical judgment on two variables, the bottom layer of PHP will perform type conversion according to the following rules and then make a logical judgment.

#1, the logical judgment is an arithmetic operator, addition, subtraction, multiplication (-*), if any operand is a floating point number, then all operands are Treated as a floating point number, the result is also a floating point number. Otherwise the operand will be interpreted as an integer, and the result will also be an integer,

2. The logical judgment is the arithmetic operator, division (/), the division operator is always Returns a floating point number. The only exception is if both operands are integers (or integers converted from strings) and are exactly divisible, in which case it returns an integer.

#3, logical judgment is an arithmetic operator, modulo (%), all operation bits are converted into integers, and the result is also an integer.

#4, the logical judgment is an arithmetic operator, negation (-), if the operand is a floating point number, the result is also a floating point number, otherwise it is always first Convert the operand to an integer, and the result is also an integer.

#5, logical judgment is a bit operator, all operands are converted into integers, and the result is also an integer.

#6, the logical judgment is a comparison operator. If one of the operands is a Boolean value, the other operation fee will be converted to a Boolean value. If one If the operand is a floating-point type, the other operand will also be converted to a floating-point type. In other cases, both operands will be converted to integers. Regardless of the type of the operands, the result of a comparison operator is always a Boolean value.

Related tutorials:PHP video tutorial

The above is the detailed content of A brief discussion on the 8 types of PHP variables and weak types. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
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
Latest Articles by Author