1. PHP data type

  • 1. PHP data type
    • 1.1. String (String)
    • 1.2. Integer
    • 1.3. Float
    • 1.4. Boolean (Boolean)
    • 1.5. Array
    • 1.6. Object
    • 1.7. Resource
    • 1.8. NULL
    • 1.9. Data type detection
    • 1.10. Data type acquisition
    • 1.11. Data type conversion
      • 1.11.1. Automatic type conversion
      • 1.11.2. Cast

PHP data types fall into three main categories:

  • Scalar data type
    • String (String)
    • Integer (Integer)
    • Float (Float)
    • Boolean (Boolean)
  • Compound data type
    • Array
    • An Object
  • Special data type
    • Resource
    • NULL

Scalar data types are the most basic unit of a data structure and can store only one data.

1.1. String (String)

A string is a sequence of consecutive letters, numbers, or characters. PHP provides four ways to represent strings: single quotes, double quotes, heredoc syntax, and nowDoc syntax (delimiter <<<).

① The differences between single and double quotation marks are as follows.

  • “” Variables inside double quotes are parsed by the compiler and then printed.
  • Variables inside single quotes are not parsed and are printed directly, so single quotes execute faster than double quotes.

Such as:

$number= 100;
echo '$number='.$number; $number=100
echo "$number=".$number; // Result: 100=100
Copy the code

Note: When defining strings, use single quotation marks if there is no special requirement.

  • The use of escape characters

Special characters in double quotes that can be output using the “\” escape character are as follows:

Escape sequences character
\n A newline
\r A carriage return
\t Tabs Tab
\ \ The backslash
\ $ The dollar sign
\” Double quotation marks

The only special characters in single quotes that can be output by the “\” escape are:

Escape sequences character
\ \ The backslash
\ ‘ Single quotes

② Heredoc and nowDoc syntax (delimiter <<<)

The syntax structure for both hereDoc and NowDoc is:

Start with <<<, followed by the start identifier name, and end with a semicolon (;) by referring to the same identifier name.

<<;
Copy the code

The use of hereDoc and NowDoc constructs has the following characteristics:

  • The opening and closing tags use the same string, usually written in capital letters.
  • There must be no Spaces or extra characters after the start tag.
  • The closing tag must be written head – on, without indentation or Spaces.
  • Variables in the heredoc structure are resolved without the need for a hyphen. To splice.

The syntactic differences between heredoc and NowDoc constructs are:

  • The start identifier name, heredoc, has no quotation marks, and NowDoc must be wrapped in single quotation marks.
  • Variables in the heredoc structure are parsed, like double quotes; Variables in nowDoc structures are printed as is, like single quotes.

      
$name = 'PHP';
$heredoc = <<<EOD
<p>$nameIt's the best language in the world! </p> EOD;
echo $heredoc;   PHP is the best language in the world!
$nowdoc= < < <'EOD'
<p>$nameIt's the best language in the world! </p> EOD;echo $nowdoc;  $name is the best language in the world!
? >
Copy the code

For details on string operations, see the string section.

1.2. Integer

Integer data can only contain integers.

The rules for integer data are:

  • Integer data must contain at least one digit (0 to 9).
  • Integer data cannot contain commas or Spaces.
  • Integer data has no decimal point.
  • Integer data can be positive or negative.

Integer data can be specified in three formats, namely decimal, hexadecimal (prefixed with 0x), and octal (prefixed with 0).

$oct = 012;  / / octal
$dec = 10;   / / decimal
$hex = 0xa;  // Hexadecimal
Copy the code

Note that in PHP 7, strings containing hexadecimal characters are no longer treated as numbers, but as regular strings.

1.3. Float

Floating-point data can be used to store both integers and decimals and exponents.


      
$num = 3.14;
? >
Copy the code

1.4. Boolean (Boolean)

There are only two Types of Boolean data, true and false, and Boolean variables are usually used in conditional statements.

All other data types have Boolean attributes:

The data type true false
The integer non-zero 0
floating-point non-zero 0.0
String type Not empty non-zero “Empty string or” 0 “string
The array type Including elements It doesn’t contain any elements
Object type Always be true
resource-based Always be true
A NULL Forever to false

1.5. Array

An array is an operable collection of data that can include scalar data, objects, resources, and other syntactic structures supported in PHP.

Generally, individual data in an array is called an element, which is divided into two parts: an index (key name) and a value. The index (key name) can be a number or a string, and the value can be any data type.

In PHP, you typically use array() to create arrays or to assign values directly. To be exact, array() is a structure, not a function, in the following format:

$array1 = array('val1'.'val2'.'val3'...). ;$array2 = array(key1 => 'val1', key2 => 'val2', key3 => 'val3',...). ;$array[key1] = 'val1';
Copy the code

See the section on arrays for details.

1.6. Object

There are two approaches to programming languages: procedural and object oriented. In PHP, users are free to use both methods.

Object data types can also be used to store data. In PHP, objects must be declared. First, you must declare class objects using the class keyword. A class is a structure that can contain properties and methods. The data types are then defined in the class and used in the instantiated class.

1.7. Resource

A resource is a special type of variable, also known as a handle, that is created and used by a special function. Resources can be created, used, and released. ren

Resources should be released when they are not needed. The system also automatically enables the garbage collection mechanism to reclaim resources after page execution to avoid running out of memory.

Resource types are almost never touched during programming.

1.8. NULL

A null value, as the name implies, indicates that no value has been set for the variable.

There are three cases in which a null value can be assigned:

  • No value has been assigned yet
  • The assigned value is null
  • A variable processed by the unset() function.

1.9. Data type detection

PHP provides a series of built-in functions for checking data types. You can check different types of data to see if they belong to a certain type, and return true if they do, false otherwise.

The functions for detecting data types are as follows:

function Detection of type For example,
is_bool() Whether it is a Boolean type is_bool(srue) , is_bool(false)
is_string() Whether the value is a string Is_string (‘ string ‘), is_string (1234).
is_float()/is_double() Whether it is a floating point type Is_float is_float (3.1415), (” 3.1415 “)
is_integer()/is_int() Whether it is an integer Is_integer (34), is_integer (‘ 34 ‘)
is_null() Whether the value is null is_null(null)
is_array() Array or not is_array($arr)
is_object() Whether it is an object is_object($obj)
is_numeric() Whether it is a number or a string consisting of numbers Is_numeric (‘ 5 ‘), is_numeric (‘ bcc110)

       
    $boo = '0123456';
    if(is_numeric($boo))
        echo 'yes! ';
    else
        echo 'no! ';    
? >
Copy the code

1.10. Data type acquisition

1. If you want to view the value and type of an expression, use var_dump(). 2. If you just want a readable representation of a type for debugging, use getType(). 3. To view a type, use is_type() instead of getType ().

1.11. Data type conversion

Although PHP is a weakly typed language, sometimes you need to use type conversions. There are two main ways to convert data types in PHP.

1.11.1. Automatic type conversion

Automatic type conversion means that when an operation is required or does not match the desired result type, PHP will automatically convert it without requiring any action by the developer.

During program development, there are three most common automatic type conversions: to Boolean, to integer, and to string. The following points need to be noted when doing automatic conversions for each type.

  • When converted to a Boolean value, null, integer value 0, floating point value 0.0, empty string, and string “0” are all converted to false, and other values are converted to true.
  • When a Boolean is converted to an integer, the Boolean value true is converted to the integer 1. The Boolean value false is converted to the integer 0.
  • When a floating point number is converted to an integer, the decimal part is removed and rounded down.
  • When a string is converted to an integer, the value is used if the string starts with a number, otherwise it is converted to 0.
  • When a Boolean is converted to a string, the Boolean value true is converted to the string “1”; The Boolean value false is converted to an empty string.
  • When an integer or floating-point type is converted to a string, the number is directly converted to a string.

1.11.2. Cast

The so-called cast is to manually convert data types when writing programs. There are three main conversion methods.

1) Enclose the target type in parentheses before the variable to be converted. This method simply precedes the variable with the type name enclosed in parentheses and allows the conversion of the types as shown in the table:

function Detection of type For example,
(int),(integer) To an integer (int)
s t r . ( i n t e g e r ) str,(integer)
boo
(bool),(boolean) Convert to Boolean (bool)
n u m . ( b o o l e a n ) num,(boolean)
str
(float),(double),(real) Convert to floating point (float)$str
(string) Convert to character (string)$boo
(array) Convert to an array (array)$str
(object) Convert to object (object)$str
Copy the code

2) Use three concrete types of conversion functions: intVal (), floatVal (), and strval(). The intval() function is used to convert to integer, floatVal () to float, and strval() to character.

Copy the code

3) Use the function setType () to convert. The setType () function sets the data type of a variable. This function converts the specified variable to the specified data type.

bool settype (mixed $var,string type)

Copy the code