PHP learning book-Chapter 6

Source: Internet
Author: User
PHP types in this chapter focus ◆ understanding of PHP's eight types of Boolean, ◆ NULL, string, array, object and resource ◆ creation, read objects are converted from a type to other types. all programming languages have types in PHP.

Highlights of this Chapter

◆ Understand eight PHP types... Boolean,

◆ NULL, string, array, object, and resource

◆ Create, read, display, and control objects of different types

Convert from a type to another type

All programming languages have some type systems and can specify the numerical types that can appear in the program. These different types often correspond to different levels of representation in computer memory. in many cases, programmers do not have to consider the representation of bitwise (or have sufficient capacity to process ). PHP's type system is simple, reasonable, and flexible. it separates program designers from some low-stage details.

In this chapter, we will explain the basic types of PHP (integer, double, Boolean, NULL, string, array, object, resource ), it also explains how to read, how to output and display, how to specify variables, how to convert and how to combine them. In this chapter, both an overview and reference materials can be skipped and ignored by people who are already experienced in programming. However, only some people who are not familiar with this chapter can read the previous sections, no matter who you are, you can leave it alone to look for the seemingly insignificant details.

First principle: Relax yourself

PHP makes it easy to type variables and values, because it does not need to specify the number of arguments and determine the type, but also because it can process many types of conversions.

Variable types do not need to be declared first

As mentioned in the previous chapter, the type of a variable does not need to be declared in advance. The program designer can directly use the formula to specify the value, PHP is responsible for figuring out the type of the specified value, as shown in the following example:

$ First_number = 55.5;

$ Second_number = "Not a number at all ";

Automatic type conversion

PHP automatically performs type conversion as needed. Like most programming languages, PHP can perform well in mixed numeric type calculation, such as computation

$ Pi = 3 + 0.14159

The result is a double-precision floating point number. before the addition is executed, the integer "3" is first converted to a floating point number in the dark.

Type based on context

PHP performs more in-depth type conversion than most languages. See the following example.

$ Sub = substr (12345, 2, 2 );

Print ("sub is $ sub
");

The Substr function is designed to use a string as an input value, and then return a part of the string. the start and length of the substring are determined by the other two parameters of the function. In this example, we do not pass a string value to it, but an integer of 12345. what is the result? In fact, the execution will not be a problem, and the browser will output

Sub is 34

Because substr wants to get a string rather than an integer, PHP will automatically convert the number 12345 to the string "12345", and then substr can do its own thing.

Because of this automatic type conversion feature, it is difficult for PHP to make errors in the type aspect. However, it is still necessary for PHP programmers to be careful, to ensure that the hybrid use of the type does not produce "no error, but it is not the correct result.

Total types]

PHP only has eight types... Boolean, string, array, object, NULL, and resource.

* The integer part of an integer without a decimal point, such as 495.

* Double-precision floating point number, such as 3.14159 or 49.0.

* Boolean Blin type, only two values, TRUE or FALSE (TRUE or not TRUE), may appear ).

* NULL is a special type. its value may only be NULL.

* String is a character sequence. it is similar to the notation similar to "PHP4 supports string operations.

* Array is a set of other values that have been named and indexed.

* An object is an instance of a class defined by the program designer. it can be encapsulated into a class-specific instance.

Values and functions of other types.

* Tesource is a special variable used to store external resources (such as database connections) that reference PHP)

Note that Boolean, NULL, and resource types are the types added by PHP4. PHP3 is not applicable.

Of course, the first five types belong to the basic type, and the following two types (array and object) belong to the composite type. These composite types can be used to make any value of any type into a group of variables, but the basic type does not work. In this chapter, we will only brief the composite types (array and object), because they will be detailed in their respective chapters. Finally, the resource type is a special type that PHP designers do not directly process. Instead, they access resources or pass the resources to other types.

Simple type

Simple types in PHP (integer, double, Boolean, NULL, and string) must be very familiar to people with programming experience (although we are not very familiar with skilled people, but we still need to explain them in detail ). The only thing that surprised C language programmers is how few PHP types are available.

In most programming languages, there are several numeric types of different sizes. larger numeric types allow a wider range of values, but they also occupy more memory space. For example, the C language has the short type (used for relatively small integers), the long type (used for potentially large integers), and the int type (this is an intermediate type, but in fact, it is not the same as short or long type. it also has floating point numbers of different precision types. it may be troublesome to pick up memory usage and functions, however, this type selection is quite meaningful. The PHP designer helped us to pick up the problem and simplified the problem by using only two numeric types, which correspond to the integer and floating point types in the C language respectively.

Integer type

Integer is a simple type, corresponding to a simple Integer that includes positive and negative numbers. An integer can be a fixed value to a variable or can be used in a formula, as shown in the following example:

$ Int_var = 12345;

$ Another_int =-12345 + 12345; // equal

Read format

Integers can be read in three ways relative to different base numbers: decimal (base 10), octal (base 8), and hexadecimal (base 16 ). The decimal place format is the preset value. The prefix of the octal place integer is "0", and the prefix of the hexadecimal place is "0x 」. Any format can be prefixed with the "-" symbol to convert the integer to a negative number. For example:

$ Integer_10 = 1000;

$ Integer_8 =-01000;

$ Integer_16 = 0x1000;

Print ("integer_10: $ integer_10
");

Print ("integer_8: $ integer_8
");

Print ("integer_16: $ integer_16
");

Its browser output is:

Integer 10: 1000

Integer_8:-512

Integer_16: 4096

(Note that the read format only affects how integers are exchanged when being read. the value stored in $ integer_8 is not written on the base of 8. People, from the internal point of view, naturally these numbers are expressed in binary form, and the most output is the conversion result based on 10, because this is printed by default, in addition, it also combines int variables into the pre-settings of the string ).

Range

How big (or how small) can an integer be? Because the PHP integer corresponds to the long type of C, and the type depends on the length of the word on the machine system, it is difficult to answer this question clearly. However, on most common systems, the maximum integer is "231-1" (or 2,147,483,647), and the minimum (negative) integer is "-(231-1) "(or-2,147,483,647 ).

As far as we know, we have not yet pointed out the PHP constant of the maximum integer in the completion formula (similar to MAXINT in C). If you have any questions, please refer to the supplementary content at the end of this chapter, if you really need to use a very large integer, PHP has some special precision (arbitrary-precision) functions. for details, see section 12th about "BC.

Double type

Double is a double-precision floating-point number, for example

$ First_double = 123.456;

$ Second_double = 0.456;

$ Seven_double = 2.0;

Note that $ even_double is a number that rounds an integer (round), but does not indicate that it is an integer type. Integer and double precision types are stored in different basic Chu formats, such as the following formula:

$ Five = $ even_double + 3;

Is not an integer type, even if the result is "5 」. However, in all circumstances, you can freely mix the floating point number and integer with double precision in the mathematical formula, so that PHP can choose not to deal with it.

The minimum number of decimal places required to list times of precision numbers, for example, the code,

$ Timeout = 2.2888800;

$ Many_2 = 2.2111200;

$ Few = $ alias + $ many_2;

Print ("$ signature + $ many_2 = $ few
");

Generate the following output in the browser:

2.28888 + 2.21112 = 4.5

For more precise control over output display results, see the printf function in chapter 10.

Read format

The typical format of a double-precision floating point is-X.Y, where-is optional and used to specify a negative number. X and Y are numerical sequences between 0-9. The X part can be omitted (if the number is between-1.0 and 1.0), the Y part can also be omitted (no decimal point ). The 0 at the end of the front can be ignored. the following example shows a floating point number with a better precision:

$ Small_positive = 0.12345;

$ Small_negative =-. 12345.

$ Even_double = 2.00000;

$ Still_double = 2 .;

In addition, you can specify the length by scientific notation, and add the letter e and the integer 10 to the end of the previous format, for example, "2.2e-3" corresponds to "2.2x10-3 」. The floating point number of a number does not need to be strictly limited to the range between 1.0 and 10.0. All the following range routine code lines are valid:

$ Small_positive = 5.6-3;

Ptint ("small_positive is $ small_postive
");

$ Large_positive = 2.8e + 16;

Ptint ("large_positive is $ large_postive
");

$ Small_negative =-2222e-10;

Ptint ("small_negative is $ small_negative
");

$ Large_negative =-0.00189e6;

Ptint ("large_negative is $ large_negative
");

Their output in the browser is:

Small_positive is 0.0055

Larger_positive is 2.8e + 16

Sall_negative is-2.222e-07

Large_negative is-1890

Note that, like the octal and hexadecimal integers, once PHP finishes the process of reading numbers, the reading format is irrelevant to the preceding variables, it does not remember that it was originally specified by scientific notation. when printing a value, PHP will make its own decision to use scientific notation as far as possible to use the most extreme value, however, this is irrelevant to the original read format.

The above is the content in chapter 6 of the PHP Learning Guide. For more information, see The PHP Chinese website (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.