PHP Learning Book-The sixth chapter

Source: Internet
Author: User
Types in PHP

This chapter highlights

Learn about the eight types of PHP ... Boolean,

NULL, String, array, object, and resource

Create, read, display output and manipulate different types of objects

Convert from some type to other type

All programming languages have some type of system and specify the kinds of values that can appear in the program. These different types often correspond to different levels of representation in the computer's memory, and in many cases the programmer does not consider the representation of the bits (or the ability to handle them). PHP's type system is simple, reasonable, and flexible, separating the programmer from some of the low-level details.

In this chapter, we will explain the basic types of PHP (integer, double, Boolean, NULL, String, array, object, Resource) and explain how to read, how to output the display, how to assign to variables, How to convert and how to group together. In this chapter it is both an introduction and a reference, for those who are already an experienced programmer can skip, but only some of the less familiar people can read the first few sections, no matter who can return to the problem in the future to look for those who did not seem insignificant details.

First principle: Relax yourself

PHP makes it easy to type in variables and values because it does not need to specify the number of molestation and determines the type, but also because it can handle many types of conversions.

Variable type don't have to declare first

As already mentioned in the previous chapter, the type of the variable does not need to be declared in advance, and the programmer can specify the value directly using an expression, and PHP itself is responsible for figuring out what type of value to specify, as in the following example:

$first _number=55.5;

$second _number= "Not a number @ all";

Type automatic conversion

PHP automatically converts the type as needed. As with most programming languages today, PHP can perform well in mixed-type numeric calculations, such as an expression

$pi =3+0.14159

The result is a multiple-precision floating-point number, and the integer "3" is converted into a floating-point number before the addition is performed.

Type based on context

PHP performs an automatic type conversion more deeply than most languages, please take a look at the following example

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

Print ("Sub is $sub
”);

The SUBSTR function is designed to take a string as an input value, and then return a string of elements of that string, which is determined by the other two parameters of the function to retrieve the starting and length of the substring. In this example, instead of passing a string value to it, we pass the integer 12345, what happens? There is no problem in actually executing, and the browser will output

Sub is 34

Since substr wants to get strings instead of integers, PHP automatically converts the number 12345 into a string "12345", and then substr can do its own thing.

Because of this kind of automatic type conversion feature, PHP is difficult to make mistakes in the type, but actually PHP programmers need to be careful to ensure that the type of mixed use does not produce "nothing wrong, but it is not the correct result" situation.

Type Total finishing]

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

* Integral part of integer number, no decimal point, such as 495.

* Double-fold precision floating-point number, similar to 3.14159 or 49.0.

* Boolean type, only two values can appear, true or false (true or non-true).

* NULL a special type whose value is only possible null.

* string strings, which are sequences of characters, similar to "PHP4 support string Operations" notation.

* array arrays, which are sets of other values that have been named and indexed.

* Object object, an instance of the class (class) defined by the designer, which can be wrapped specifically

Other types of values and functions.

* Tesource is a special variable used to store resources referenced outside of PHP (e.g. database connection)

Note that boolean, NULL, and resource are types that are added by PHP4, and PHP3 does not apply.

Of course, the first five types belong to the basic type of the other, and the continuation of two (array and object) belong to the compound type, these composite types can be used to make arbitrary types of arbitrary values of a group of variables, but the basic type is not. We only have a short composite type (array and object) in this chapter because they are described in detail in their respective chapters. Finally, the resource type is a special type that the PHP designer does not directly handle, but instead accesses the resource through a special function or passes it to other functions that are needed.

Simple Type

Simple Types in PHP (integer, double, Boolean, NULL, and string) must be familiar to people with programming experience (although we are not targeting skilled people, we will explain them in detail). The only thing that would surprise C programming designers is how few PHP types can be.

Most programming languages have several different sizes of numeric types, and larger numeric types allow a wider range of values, but also occupy more memory space. For example, C has a short type (for a relatively small integer), a long type (for a potentially large integer), and an int type (this is an intermediate type, but it is not actually the same as short or long), and it also has different precision floating-point types. Picking up between memory usage and functionality can be annoying, but this type of choice makes sense. PHP design developers have helped us do a good job of picking up and simplifying the puzzle by using only two types of numbers, which correspond to integers and floating-point numbers in the C language, respectively.

Integer type

An integer is a simple type that corresponds to a simple integer that includes positive and negative numbers. An integer may specify a value to a variable, or it can be used in an expression, as shown in the following example:

$int _var=12345;

$another _int=-12345+12345;//equals

Read format

Integers can actually be read in three ways relative to different cardinality, decimal (base 10), octal (base 8), and 16 carry (base 16). The decimal format is a preset value, and the predecessor of the octal integer is specified as "0", and the 16-digit predecessor is "0x". Any format can have a "-" symbol in front of it to make an integer negative. 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 the integer is read, and the value stored in the $integer_8 does not note that it was originally written in base 8.) People, from the inside, of course, these numbers are represented in binary, and the most output is a 10-based conversion result, because this is a preset printing, and it will also merge int variables into the string's pre-set.

Range

How large (or how small) can an integer be? Because the PHP integer is a long type that corresponds to C, and the type depends on the length of the word you use on the machine system, it is difficult to answer this question explicitly. However, on most common systems, the largest 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, there is currently no PHP constant (similar to maxint in C) that can indicate the largest integer in a completed style, and if you have questions, see the supplement at the very end of this chapter if you really need to use very large integers, PHP is a number of precision (arbitrary-precision) functions, please refer to Chapter 12th about "bc" the section.

Double type

Double is a double-precision floating-point number, such as

$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 mean that it is an integer type. integers and multiples are stored in different base-chu formats, such as the following expressions:

$five = $even _double+3;

The result is a multiple-precision type number, not an integer type, even if it lists the result as "5". However, in all cases, it is freely possible to mix double-precision floating-point numbers and integers in mathematical expressions, so that PHP can choose not to handle them.

The minimum number of decimal digits, for example, code, that is used when you list the double-precision number in a preset situation

$many = 2.2888800;

$many _2 = 2.2111200;

$few = $many + $many _2;

Print ("$many + $many _2 = $few
”);

The following output is produced in the browser:

2. 28888 + 2.21112 = 4.5

If you need more precise control over the output display, refer to the printf function in chapter tenth

Read format

The typical format of a double-precision floating-point number is "-x.y", where "-" is optional, used to specify negative numbers, and X and Y are sequences of numbers 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). 0 of the front end can be ignored, and the following are examples of the effect of the double precision floating point numbers:

$small _positive = 0.12345;

$small _negative =-.12345

$even _double = 2.00000;

$still _double = 2.;

In addition, the number of times can be specified by scientific notation notation, in the front of the end of the format to add the letter E and 10 of the integer to be in the screen, for example, "2.2e-3" will correspond to "2.2x10-3". The number of floating-point parts of numbers does not need to be strictly limited to the range between 1.0 and 10.0. All of the following range example 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
”);

He's in the browser output for:

Small_positive is 0.0055

Larger_positive is 2.8e+16

Sall_negative is-2.222e-07

Large_negative is-1890

Note that, as with the octal and 16 integer, once PHP ends the process of reading the number, the read format is irrelevant to the previous variable, and it does not remember that it was originally specified with the scientific notation notation, and when the printout value was printed, PHP would make its own decision to use the number of the most extreme size in scientific notation notation. However, this is independent of the original read format.

The above is the PHP learning Treasure-The sixth chapter of the content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.