菜单

Detailed explanation of type conversion, constant, system constant and magic constant in PHP

2017年11月23日 - PHPer

Detailed explanation of type conversion, constant, system constant and magic constant in PHP

1. automatic type conversion;

Automatic type conversion in operation and judgment;

1) other types into bool, judgment time conversion;

1) integer to boolean type: 0 to false, non 0 to true;

2) empty strings and ‘0’ ‘(‘ 0 ‘) are converted to false, and others to true;

3) empty array to false, non empty array is converted to true;

4) null turns to false

5) unsuccessful resource opening for false

Is 0 or empty, open unsuccessful turn to ‘false’,’0′;

2) other types are converted to strings (string concatenation);

Null bool int float

1) null conversion is empty (empty string not shown)

2) the integer is converted directly to the corresponding string 5=>’5′;

3) the real number is directly converted to the corresponding string, but the tail 0 does not rotate (the last 0 of the real decimal point);

4) true converted to’1, “false is empty;

Strings, Boolean, integer, floating point, null

3) type conversions that occur during computations

1) true/false conversion 1/0;

2) null conversion to 0;

3) the value at the beginning of the string is replaced by the corresponding value;

4) string beginning without value, then directly to 0;

(null, string, bool) < int < float

2, forced type transformation

That is, manually converting one type of value to another;

1, type transformation functions intval, strval, floatval

$num1 = 1.2;

$num2 = strval ($num1); / / $num1 value is converted to a string;

Var_dump ($num1);

Var_dump ($num2);

2, (type) $variables cannot be converted to resource

$num2 = (unset) $num2; turn to empty

3, setType (variables, types) must be written as strings;

Change the type and value of variables;

Matters needing attention

1) floating points to integer, eliminate decimal directly;

2) echo (int) ($num1 + 0.5) and four to five

3, constant

1) definition of constant

Define (constant name, constant value)

Define (SONG’, 22);

Be careful:

1) constant names are strings;

2) constant values must be scalar

3) constant names are generally capitalized and distinguished from variables;

4) constant names and variable names, same naming rules;

2) system constants

__LINE__ current line number

__FILE__ the name of the current file

__DIR__ directory of the current file;

PHP_OS operating system

The version of PHP_VERSION PHP

Magic constants:

__FUNCTION__ function name

__FUNCTION__ function name;

__CLASS__ class name

__CLASS__ class name

__METHOD__ method name

__METHOD__ method people

__NAMESPACE__: name space

__NAMESPACE__ namespace;

3) constant judgment

Defined (constant name) constant name must be string;

Determine whether or not to define a constant;

4. operators and expressions;

1) arithmetic operator + – (multiplication) / (Division)%

% modulo / remainder

0% 3 = 0

12% 10 = 2

5% 19 = 4

1% 3 = 1;

2% 3 = 2;

3% 3 = 0;

-9% 4 = -1

9% -4 = 1;

The sign of the result of modulo operation depends on the first number.

Judge parity

X%2 = = 0 is an even number divisible by

X%2 = = 0 is odd and cannot be divisible

2 * 3 = 8

Exponential operation;

23 = 8;

32 = 9;

发表评论

电子邮件地址不会被公开。