Data Types of PHP Variables

Data Types of Variables A



data type is a collective term for a group of data with the same characteristics. PHP has long provided rich data types, with more additions in PHP 5. Data types can be divided into 3 categories: scalar data types, composite data types and special data types.

Scalar types (four types):

Integer (int, integer)

Floating point (float, double, real)

Boolean (bool, boolean)

String (string)

Composite type (two types):

array (array)

Object (object ) )

Special type (two types):

resource (resource)

NULL (NULL)



1. Integer (integer)

Integer in PHP refers to data that does not contain a fractional part. In a 32-bit operating system, the valid range of integer data is between "-2147483648~+2147483647". Integer data can be represented in decimal (base 10), octal (base 8, prefixed with 0), or hexadecimal (base 16, prefixed with 0x), and can contain "+" and "-" . The usage of integer data is shown in the following code.

<?php
$a = 100; // decimal integer data
$b = -034; // octal integer data
$c = 0xBF; // hexadecimal integer data
echo $a."<br>";
echo $b."<br>";
echo $c;
?>

The output in the browser is:

100
-28
191

If the given number exceeds the specified range of integer data, a data overflow will occur. In this case, PHP will automatically convert integer data to floating point data.

2. Floating-point (float)

Floating-point data is commonly referred to as real numbers, which can be divided into single-precision floating-point data and double-precision floating-point data. Floating-point numbers are mainly used for forms that cannot be satisfied by simple integers, such as the representation of data such as length and weight. The usage of floating point data is shown in the following code.

<?php
$a = 1.2;
$b = -0.34;
$c = 1.8e4; //The float represents 1.8×104
echo $a."<br>";
echo $b."<br>";
echo $c;
?>

The output in the browser is:

13
1.2
-0.34
18000

3. Boolean (boolean)

Boolean data began to appear in PHP 4, a Boolean data only has two "true" and "false" These values ​​correspond to logical "true" and logical "false" respectively. The usage of boolean variable is shown in the following code. When using the boolean data type, the values ​​"true" and "false" are case-insensitive. That is, "TRUE" and "FALSE" are equally true.

<?php
$a = true;
$b = false;
echo $a;
echo $b;
?>

The output in the browser is:

1

When the boolean value is "true", the output is 1, and when the boolean value is "false", the output is empty.

4. String

A string is a sequence of characters. The characters that make up the string are arbitrary and can be letters, numbers or symbols. There is no strict rule on the maximum length of strings in PHP. There are 3 ways to define strings in PHP: using single quotes ('), using double quotes ("), and using delimiters (<<<). Below is an example of using strings.

<?php
$var = "Chinese";
echo "I am $var"."<br>";
echo 'I am $var'.'<br>';
echo "The weather is nice today! "."<br>";
echo 'Let's go to the library.'."<br>";
echo <<<Eof
I am a {$var}
Eof;
?>

The output in the browser is:

I am Chinese
I am $var
The weather is nice today!
I'm Chinese,
I'm $var
, the weather is nice today!
Let's go to the library.
I'm a Chinese

The biggest difference between single quotes and double quotes in php is that double quotes have one more parsing process than single quotes. Double quotes will parse variables and escape characters in double quotes. The single quote is output as a string regardless of its content. In double quotation marks, when Chinese and variables are used together, it is better to enclose the variable with {}, or use double quotation marks before and after the variable, and then use "." to connect the variable.

<?php
$str = "Young people";
echo "We are all $str and should study more."."<br>";
echo "We are all {$str} and should study more."."<br >";
echo "We are all ".$str.", we should study more.";
?>

The output in the browser is: we

are all young people, we should study more. We are all young people and should study more. The output of the first sentence is because the variable is not enclosed in {}, or the string is not separated, and then "." is used to connect the variable, so the variable and the string following it cannot be output, and the output of the second and third sentences is normal. In general, we try to use single quotes, because in theory, single quotes run faster, and we only use double quotes when there are variables and escape characters that need to be parsed. The following are some commonly used escape characters: Escape sequence description \n newline \r carriage return \t tab \backslash \$dollar \"

























It is worth noting that the three escape characters "\n", "\r" and "\t" cannot be reflected in the browser and can only be seen in the source file.

The function of the PHP delimiter is to output what is inside it, including the newline format; any special characters in the PHP delimiter do not need to be escaped; the PHP variables in the PHP delimiter will be normal. is replaced with its value. The following points should be paid attention to when using the delimiter:

(1) The character Eof after <<< is defined by itself, and anything is allowed, but the characters at the end must be the same as him, and they appear in pairs ;

(2) The Eof; at the end must start on a new line, and there cannot be any other characters except the Eof; delimiter ending identifier, and there cannot be before and after, including spaces;

(3) If in the middle of the delimiter When there is a PHP variable, you just need to write it as output in other strings. The reason why the variable $var is enclosed in {} is to tell the PHP parser that this is a

PHP variable. In fact, it is not necessary. But there may be ambiguity.

5. Array (array)

An array is a collection of related data arranged in a certain way. The data that make up this set can be of basic data type or composite data type; it can be the same data type or different data types.

Each data element in the array has its unique number, called an index. Indexes are used to specify specific data elements in an array. In some languages, the index of an array must be a number, while in PHP, the index can be a number or a string.

A simple PHP array application example code is as follows.

<?php
$network = array(1=>"how",2=>are,'three'=>"you");

echo $network[three];
?>

The output in the browser is:

areyou

6. Object (object)

Object is a core concept in object-oriented language, and an object is an instance of a class. Before understanding objects, let's briefly introduce what a "class" is. In object-oriented languages, people extract the common features of each specific thing to form a general concept, which also constitutes a "class". A class is defined in PHP as follows.

class class name {

what the class contains;

}

In PHP, use the "new" keyword to instantiate a class and get an object of that class. An example of the application of classes and objects is shown in the following code.

<?php
class Book {
function getBookName($book_name){
return $book_name;
}
}
$book1 = new Book(); //Instantiate a Book class object book1
echo $book1->getBookName("PHP")." <br>";
$book2 = new Book(); //Instantiate an object of Book class book2
echo $book2->getBookName("JSP");
?>






7. Resource

A resource is a special data type provided by PHP, which is used to represent an external resource of PHP, such as a database access operation, or a network stream processing. Although a resource is also a data type, we cannot operate on it directly. PHP provides some specific functions for creating and using resources. For example, the "mysql_connect()" function is used to establish a connection to MySQL data, the "fopen()" function is used to open a file, and so on.

Example code to apply the resource data type is as follows.

<?php
$cn = mysql_connect('localhost','root');
echo get_resource_type($cn)."<br>";
$fp = fopen("foo","w");
echo get_resource_type($fp) ;
?>

The output in the browser is:

mysql link
stream

8. Null (NULL)

NULL is a special data type introduced by PHP 4. This data type has only one value, NULL. In PHP, if the variable meets the following conditions, the value of the variable is NULL. The variable is not assigned any value. The variable is assigned the value NULL.

The variable processed by the unset() function.

The following is an example of using the NULL data type.

<?php
$a ;
$b = NULL ; //The variable $b is assigned NULL
18
$c = 1 ;
unset($c); //After using the unset() function, the value of $c is NULL
?>

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326139443&siteId=291194637