Home >Backend Development >PHP Tutorial >PHP Learning Guide-Chapter 6
Types in PHP
Key points of this chapter
◆ Understand the eight types of PHP... Boolean,
◆ NULL, string, array, object and resource
◆ Create, read, display output and manipulate different types Other objects
Convert from one type to another
All programming languages have some type system to specify the types of values that can appear in the program. These different types often correspond to different levels of representation in computer memory, and in many cases programmers do not need to consider bit representation (or have enough power to handle it). PHP's type system is simple, rational, and flexible, and it separates programmers from 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 and display, how to assign to variables, and how to convert And how to put it together. This chapter is both an introduction and a reference material. People who are already experienced in programming can skip it. However, only those who are not familiar with it can read only the first few sections. No matter who they are, they can learn more in the future. When you have a problem, you can't go back and look for details that may seem inconsequential at first.
First Principle: Relax Yourself
PHP makes it easy to type in variables and values because it does not require specifying numbers and types, and because it can handle many types of conversions.
The variable type does not need to be declared first
As mentioned in the previous chapter, the variable type does not need to be declared in advance. The programmer can directly use the operator to specify the value, and PHP itself will be responsible for figuring out what to specify. What type is the 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 when needed. Like most programming languages today, PHP can perform well when performing mixed numerical type calculations. For example, the result of the operation formula
$pi=3+0.14159
will be a double-precision float. Point, before performing the addition, the integer "3" will be secretly converted to a floating point number.
Configure types based on context
PHP goes further than most languages when performing automatic type conversions, please take a look at the example below
$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 certain substring of the string, which is determined by the other two parameters of the function Retrieve the starting point and length of the substring. In this example, we are not passing it a string value, but the integer 12345. What will be the result? In fact, there will be no problem in execution. The browser will still output
Sub is 34
Since substr hopes to get a string instead of an integer, PHP will automatically convert the number 12345 into 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 types. However, PHP programmers still need to be careful to ensure that the mixed use of types does not produce "no errors, but also is not the correct result".
Type summary]
PHP has only eight types...Boolean, string, array, object, NULL and resource.
* integer The integer part of the number, without decimal point, such as 495.
* double double precision floating point number, similar to 3.14159 or 49.0, etc.
* Boolean Boolean type, only two values are possible, TRUE or FALSE (true or not true).
* NULL is a special type whose value can only be NULL.
* string A string is a sequence of characters, similar to the expression "PHP4 supports string operations".
* array An array is a collection of other named and indexed values.
* object object, an instance of a class defined by the programmer, can wrap other types of values and functions specific to that class
.
* tesource is a special variable used to store references to resources outside PHP (such as database connections)
Please note that Boolean, NULL and resource are types added by PHP4 and are not applicable to PHP3 .
Of course, the first five types are basic types, and the following two types (array and object) are composite types. These composite types can be used to form any value of any type into a group of variables, but the basic types But no. We will only briefly describe the composite types (array and object) in this chapter, as they are covered in detail in their respective chapters. Finally, the resource type is a special type that PHP designers do not deal with directly, but use special functions to access resources or pass them to other functions as needed.
Simple types
The simple types (integer, double, Boolean, NULL and string) in PHP must be familiar to people with programming experience (although we are not targeting skilled people, we still want to detail explain them). The only thing that may surprise C programmers is how few types PHP has.
Most programming languages have several numeric types of different sizes. Larger numeric types allow a wider range of values, but also take up more memory space. For example, the C language has a short type (for relatively small integers), a long type (for potentially large integers), and an int type (which is an intermediate type, but is actually either a short or a sum). long type), it also has floating point types of different precisions, and the trade-off between memory usage and functionality may annoy you, but the type choice still makes sense. The PHP developers did a great job of simplifying this problem by using only two numeric types, which correspond to the integer and floating point types in C.
Integer type
Integer is a simple type, corresponding to simple integers including positive and negative numbers. Integers can be assigned values to variables, or can be used in calculation expressions, as shown in the following example:
$int_var=12345;
$another_int=-12345+12345; //Equal to
Reading format
Integer actually It can be read in three ways relative to different bases, decimal (base 10), octal (base 8) and hexadecimal (base 16). The decimal format is the default value. The leading digit for octal integers is specified with "0", and the leading digit for hexadecimal is "0x". Any format can be preceded by a "-" sign to make an integer 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
");
The browser output is:
integer_10: 1000
integer_8: -512
integer_16: 4096
(Please note that the reading format only affects integers) How it is changed when reading, the value stored in $integer_8 does not remember that it was originally written in base 8. Internally, it is natural that these numbers are expressed in binary form, and The final output is the base 10 conversion, since this is printed by default, and it also merges the int variable into the string default).
Range
How big (or how small) can an integer be? Because PHP integers correspond to C's long type, and that type depends on the word length on the machine you're using, it's difficult to answer this question unequivocally. However, on most common systems, the largest integer is "231-1" (or 2,147,483,647), and the smallest (negative) integer is "-(231-1)" (or - 2,147,483,647).
As far as we know, there is currently no PHP constant that can point out the largest integer in the perfect expression (similar to MAXINT in C language). If you have any questions, please see the supplementary content at the end of this chapter. If you really need to use very large Integer, PHP has some arbitrary-precision functions, please refer to the section about "BC" in Chapter 12.
double type
double is a double-precision floating point number, for example
$first_double = 123.456;
$second_double = 0.456;
$seven_double = 2.0;
Please note that $even_double is "rounded" Enter an integer (round) ” number, but it does not mean that it is an integer type. Although integer types and multiple precision types are stored in different base formats, for example, the following operation formula:
$five = $even_double+3; the result of
is a multiple precision type number, not an integer type, even though the result listed is "5". However, in all cases, you are free to mix double-precision floating point numbers and integers in mathematical expressions and let PHP choose the types to handle.
By default, multiples are listed with the minimum number of decimal places required, e.g., program code,
$many = 2.2888800;
$many_2 = 2.2111200;
$few = $ many + $many_2;
Print(“$many + $many_2 = $few
”);
produces the following output in the browser:
2.28888 + 2.21112 = 4.5
if more precise control is required For output display results, please refer to the printf function in Chapter 10
Reading format
The typical format of double-precision floating point numbers is "-X.Y", where "-" is optional and is used to specify negative numbers, X and Y is a sequence of numbers between 0-9. The X part can be omitted (if the number is between -1.0 and 1.0), and the Y part can also be omitted (no decimal point). The leading and trailing zeros can be ignored. The following are examples of effective double-precision floating point numbers:
$small_positive = 0.12345;
$small_negative = -.12345
$even_double = 2.00000;
$still_double = 2.;
In addition, the double industry length can be specified according to the scientific notation method. Add the letter e and the desired integer multiplication of 10 at the end of the previous format. For example, "2.2e-3" will Corresponds to "2.2x10-3". The floating point portion of the number does not need to be strictly limited to the range between 1.0 and 10.0. All of the following lines of range routine code 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 is browsing here The output of the generator is:
small_positive is 0.0055
larger_positive is 2.8e+16
sall_negative is -2.222e-07
large_negative is -1890
Please note that as with octal and hexadecimal integers, once PHP After the process of reading numbers is over, the reading format has nothing to do with the previous variables. It will not remember that it was originally specified in scientific notation. When outputting the printed value, PHP will make its own decision to Scientific notation uses the most extreme numerical values possible, regardless of the original reading format.
The above is the content of Chapter 6 of the PHP Learning Guide. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!