Home > Backend Development > PHP Tutorial > PHP Learning Guide-Chapter 6 (Sequel)

PHP Learning Guide-Chapter 6 (Sequel)

黄舟
Release: 2023-03-04 07:08:01
Original
1055 people have browsed it


Newline symbols in strings

Although PHP provides an escape escape sequence (
) for newline symbols, it’s also good to know that you can literally add newline symbols in the middle of strings, PHP Can handle this form as well. This is convenient when creating HTML strings, because browsers ignore (
) newline characters, so you can format the string with newline characters, making the PHP code lines shorter:

PHP Learning Guide-Chapter 6 (Sequel)

at In a text editor, pressing the "Enter" key is hidden at the end of the first two lines. These newline symbols are retained in the string, so a print statement can produce three lines of PHP output (the length of the lines depends on the various editors. If the editor wraps when displaying them, you'll see what's actually a long three-line line of code). However, the browser program will ignore these line breaks and decide for itself whether and where to break when displaying. In addition, if you use your browser's view source code to observe the source code, you will see these line breaks.

Restrictions

There are no artificial restrictions on the length of the string. As long as it is within the available memory limit, generally there should be no limit on the length of the string

Array

PHP's array type allows programmers to Values ​​of many different types are organized together and indexed by word (and possibly by name). If you find yourself using variables with names like $thing1, $thing2, $thing3, it is recommended that you use arrays ($things[1], $thing[2], $thing[3], etc.). Array elements are used through indexes in square brackets (in this example [1], [2], [3], etc.), and elements of different types can be assigned to the same array.

The best way to learn PHP arrays is through examples. Here are some simple program examples to view the contents of array variables before and after the first specification:

PHP Learning Guide-Chapter 6 (Sequel)

Let’s explain what happens before and after the first designation. Before specifying, despite the name, PHP does not know that the purpose of the variable $my_array is to be an array, and just treats it as an unspecified variable like other variables. This means that when the variable is enclosed in a double-quoted string, the variable is interpreted as an empty string. When the array index refers to an unspecified variable ([5]), it will also be used as an unspecified variable. The result is that the first three print statements all end with [is].

After specifying, $my_array officially becomes an array, and the result is to print out the "Array" string when a double-quoted string is included. The array cell indexed by the number 5 has been filled with the string "Slot#6" (actually, in most programming languages, array elements are counted starting from 0) so the index number 5 can be used to retrieve the string. This is the only thing that has changed - there is still nothing in cell 0 of $my_array, so there is no change as before.

Execution of Arrays

Arrays are one of the coolest and most useful features of PHP. Although they look similar to arrays in other languages, they are actually implemented in quite different ways.

In most programming languages, a statement similar to the following is used to declare an array:

Int int_array[10] ;//This is not a PHP syntax harmony!

This statement will set a total of ten consecutive integers in the memory. These variables can be accessed according to the number index in the int_array range of 0 to 9.

On the other hand, PHP arrays can be combined with each other. When making an array specification, a new column is actually added and combined with a new index. This index can be used to obtain the value [for This makes sense if you are familiar with hash table programming, PHP arrays are more like hash tables in other languages].

This implies one thing, that is, readers don’t have to worry about specifying a very high number of array cells, for example:

My_ayyay[100000000]= “not scary”//This is no problem

Because of the result of this setting There won't really be a lot of grids, and the grids in the middle don't exist, so they won't take up any memory.

String as array index

So far, our array examples only use integers to index numbers. In fact, PHP can also use string values ​​as indexes, for example:

$tasty['Spanish']= "paella";

$tast ['Japanese']= "sashimi";

$tast ['Scottish']= "haggis?";

These indexes are used in the same way as numeric indexes, and numbers and strings Indexes can be used on the same array and they will not conflict.

Why is the structure type missing?

Some programming languages ​​(C, Pascal) provide "structure" or "record" types, which allow variables of different types to be packaged together. In such a language, the basic rule of thumb for choosing a composite type is: if all the values ​​containing the water quantity are of the same type, otherwise use a structure.

Now PHP has an object type, which in addition to more special properties, also has characteristics similar to record or structure types. However, before introducing objects, PHP does not really need structural types, because PHP arrays are not limited to using only one type of value. If you are moving a code dictionary from a structured language to PHP, one possibility is to use combineable arrays and use string indexes to correspond to the original field names.

Other array features

In fact, this chapter will only briefly introduce arrays. In addition, arrays can also be multi-dimensional and can be specified in many different ways. There are also many useful functions that can be used to observe, reuse and manipulate arrays. easier. Arrays will be explained in more depth in Chapter 11 of this book.

Objects

Another basic type of PHP is the object, which is the channel for PHP to enter the object-oriented programming language. Like an array, an object is also a composite type, and allows you to integrate various other types into one object. The object has additional and more precise properties, including the ability to integrate functions as data, and Other object orientation concepts can be used, and you are encouraged to refer to that section for more information.

Resources

Resources is a special value used to refer to memory outside of PHP itself. You don’t need to know too much about Resources to write PHP. We will briefly introduce all possible Resources, but please feel free to skip the following "What to do with resources" section.

What exactly are Resources?

Resources is the type used by PHP to communicate with external programs (which can be databases or image processing programs). PHP may need to use the resources configured in memory by these programs. Generally speaking, PHP programmers do not need to design in PHP Worry about memory release, if you generate a string in a PHP program (which must occupy some memory space), you can forget all its details until the end of the program, PHP (or the Wed server running it) will Release the appropriate memory after your program has finished executing, even if not immediately.

External programs (databases, etc.) may not be able to release memory so smartly. After your program has run, you may think of reserving appropriate memory for your database. The way PHP handles this is All special functions that access memory from external programs like this return Resources and allow PHP to check if your program can access the resources. If no one can use the resources, PHP will do this by counting the resources. The reference of the resource allows external programs to correctly solve this kind of problem. If the reference count is 0, the resource can be released.

How to deal with resources?

Usually PHP programmers do not need to create these resources themselves. They pass these resource types back by calling some special functions and pass them to other functions that require resource types. For example (as we'll cover in Part 2 of this book), you could call the mysql_connect() function (which returns the connection to the MySQL data reference), store the result in a variable, and pass it to mysql_query() function (use this connection to access the database).

Basically all you need to do to use this connection resource is to store it in a variable and pass it to the required function. You can clear this resource through PHP after the program ends. In any case, if you feel that this resource has exhausted a lot of memory during program execution before the program ends and you want to release it early, you should do something like this:

$my_resource=mysql_connect(); // Store variables

//Program fragment that uses connection resources

$my_resource=NULL;//The variable no longer refers to the original resource

The specification of $my_resource will cause PHP to check that no other program is using the MySQL resource before starting to release it.

Type Testing

Because variables can change types by reassignment, sometimes it is necessary to figure out the type of a certain value when the program is executed. PHP not only provides a general type testing function (gettype()), but also provides Boolean functions for each of the five data types. Some of these functions also have replaceable names. Table 6-1 summarizes these functions:

Table 6-1 Type testing of functions

PHP Learning Guide-Chapter 6 (Sequel)

Specification and casting

As mentioned before However, PHP often automatically converts one type to another when needed for this article, but PHP programmers can also force such conversions to occur. In either case, the program designer should know what results to expect.

Type conversion behavior

The following are some general rules when converting from one type to another in PHP:

Integer to multiple precision floating point number: Create an exact matching multiple precision floating point number (for example, int4 becomes double4. 0 times).

Double precision floating point number to integer: the decimal part is rounded off.

Number to Boolean: FALSE if equal to 0, TRUE otherwise.

Number to string: The string is created according to how it looks when displayed as a number. Integer output is displayed as a sequence of numbers. Double-precision floating-point numbers are output at the minimum required precision. Double-precision floating-point values ​​with a large number of characters after the decimal point are converted to Kasaken symbolic notation.

Boolean to number: If TRUE, you get 1, if FALSE, you get 0.

Boolean string: If it is TRUE, it will get 1, if it is FALSE, it will get an empty string.

NULL to number: 0

NULL to Boolean: FALSE

String to number: Equivalent to "reading" a number from the string and then converting it to the given type. If the number cannot be read, the value is 0. Not all strings need to be read to be successful.

String to Boolean: FALSE if it is an empty string or 0, TRUE otherwise.

Arrays of simple types (numbers or strings): Equivalent to creating a new array with the value assigned to index 0.

Array to Number: Undefined (see description below)

Array to Boolean: FALSE if the array has no elements, TRUE otherwise.

Array to string: This is "Arryay".

Object to Number: Undefined (see description below).

Object to Boolean: TRUE if the object contains any member variable with a value, otherwise FALSE.

Object to String: "Object".

Resource to Boolean: FALSE.

Resource to number: Undefined (see description below)

Resource to number: Sometimes like "Resource id #1" (but you can't rely heavily on this convention).

In the above list, we found that some types are undefined when converted to numerical types. In the above description, undefined means that the PHP designer has not determined the final conversion in the future PHP version. rules, so the bad news is that it depends on how you write your program. You may find that you can convert to a numeric type in the specific version of PHP you are using, but it may not work in the next version.

The above is the content of PHP Learning Guide - Chapter 6 (continuation). For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template