PHP Learning Book-sixth chapter (sequel)

Source: Internet
Author: User
Tags array to string learn php
Line break symbols in a string

Although PHP provides a skip escape sequence for line break symbols (
, but it's also good to let you know that you can literally add a newline symbol to the middle of a string, and PHP can handle this form as well. This is handy when creating HTML strings, because the browser ignores (
A newline symbol, so you can format the string with a newline symbol to make the PHP code line shorter:

In the text editor, hide the end of the first two lines by pressing the enter"keys, these newline symbols remain in the string, so a print narrative can produce three lines of PHP output (the length of the line depends on a variety of editors, and if the editor automatically wraps when it displays them, you will see a bar of three lines of code actually). However, the browser program ignores these line-break symbols and decides whether to wrap them and where to wrap them, and if you look at the original code using your browser's view source, you'll see these line breaks.

Limit

There is no artificial limit to the string length, as long as within the available memory limits can, generally should be able to not limit the string length

Array

The array type of PHP allows programmers to organize many different types of values together and index them by word (or by name). If you find yourself using variables with names like $ thing1, $thing 2, $thing 3, it is recommended that you use arrays ($things [1], $thing [2], $thing [3], and so on). Array elements are used by the index in square brackets (which is in this example) [1], [2], [3], and so on, and different types of elements can be assigned to the same array.

The best way to learn PHP arrays is through examples. Here are some simple examples of how to view the contents of an array variable before and after the first assignment:

Let's explain what happens the first time before and after the designation. Before the designation, despite the name, PHP does not know that the purpose of the variable $my_array is to become an array, only as an unspecified variable as with other variables. This means that when the variable is enclosed in a double-quoted string, the variable is interpreted as an empty string. The array index references the reference ([5]) to an unspecified variable as if it were not specified. The result is that the first three print narratives end with [is].

Once specified, the $my _array formally becomes an array, and the result is that the "array" string is printed when it is included in a double-quote string. Array cells indexed by the number 5 are populated with string "slot#6" (in most programming languages, the array element is counted from 0) so the index of the number 5 can be used to fetch the string. This is the only place where things change--$my _array's No. 0 lattice or nothing, so it hasn't changed as before.

Execution of the array

Arrays are one of the coolest and most useful features of PHP, and although they look like arrays in other languages, they are actually quite non-trafficked.

In most programming languages, an array is declared with a statement similar to the following:

Int int_array[10];//This is not PHP syntax call and Oh!

A total of 10 consecutive integers are set in the memory, which can be accessed according to the numbered index of the Int_array range of 0.

On the other hand, the PHP array can be combined, and when the array is specified, a new tightly column is actually added, which is combined with a new index, which can be used to get the value [it makes sense for programmers who are familiar with the hash table], The PHP array is more like a quasi-hash table in other languages.

The implication here is that the reader does not have to worry about specifying a very high number of arrays, such as:

My_ayyay[100000000]= "not scary"//This is no problem.

Because this result does not really appear a lot of the lattice, the middle of those bits are not there, and therefore will not occupy any memory.

String as array index

So far, our array Paradigm sub-index numbers are indexed using only integers, but PHP can also be indexed using string values, 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 string indexes can be used for the same array, and they will not conflict.

Structure type Why are you missing?

Some programming languages (C, Pascal) provide a "structure (structure)" or "record" type, allowing different types of variables to be packaged together. In such a language, the basic principle of choosing a compound type is that if all the values that contain water are of the same type, then the structure is used.

Now PHP has an object type, which, in addition to having a more specific attribute, has a similar record or structure-type feature. However, before the introduction of the object, PHP does not really need structural type, because the PHP array does not restrict the use of only one type of value. If you move the code-respecting dictionary from a structured language into PHP, it is possible to use a combination of arrays and a string index that corresponds to the original field name.

Other array features

In fact, this chapter will only superficial introduction to the array, in addition, the array can be multidimensional, can be specified in many different ways, and there are many useful functions, you can use the array of observation, reuse and manipulation easier. The array is explained in more depth in chapter 11th of this book.

Object

Another basic type of PHP is the object, which is the channel for PHP to enter the object-oriented language. Like an array, an object is also a compound type, and allows you to integrate other types into an object that has an additional and more accurate property, which can be considered as data integration, and can use other object-oriented concepts, and expects you to read this section to learn more information.

Resources

Resources is a special value outside of PHP itself used to refer to memory, you don't need to know much about resources to write PHP, we will briefly describe all possible resources, but greet beats the following "How to handle the resource" section.

What is the resources?

Resources are the types that PHP uses to communicate with external programs (which can be database or image processing programs), and PHP may need to use these programs in memory to configure the resource, in general PHP programmers do not need to worry about the design of the PHP memory release, If you create a string within a PHP program (which must occupy a portion of the memory), you can forget all the details of it until the end of the program, and PHP (or the Wed server) will release the appropriate memory after your program has finished executing, even if it is not immediately.

External programs (libraries, etc.) may not be able to release the memory so intelligently, and when your program is already operational you may be thinking of preserving the appropriate memory for your database, and the way PHP handles it is to return all special functions like this external program access memory to the resources, and used to let PHP check if your program can get these resources, if no one can use these resources, PHP will be able to count this resource reference, so that the external program can correctly solve such problems, if the reference count is 0, the resource can be released back.

What do you do with resources?

Typically, PHP programmers do not have to build these resources themselves, and they pass on these resource types by calling special functions and communicating them to other functions that require resource types. For example (we will cover the second part of this book), you can mysql_connect () function (used to return the connection to the M y S Q-l data Reference), and store the result to a variable, and to transmit it to the mysql_query () function (Use this connection to access the database).

Basically the steps you need to use to connect to this resource are to store it in a variable and pass it to the required function, and you can clear the resource through PHP after the program ends. In any case, if you feel that the resource has exhausted many memory in program execution before the program finishes and wants to release it early, you should be able to do the following:

$my _resource=mysql_connect ();//Store variables

Program fragments using the Wired resource

$my _resource=null;//variables no longer refer to the original resource

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

Type test

Because variables can change type based on re-designation, it is sometimes necessary to figure out the type of a value when the program executes. PHP provides both a generic type test function (GetType ()) and a Boolean function for five types of data types. Some of these functions also have replaceable names, and table 6-1 totals these functions:

Table 6-1 type tests for functions

Specifying and casting

As already mentioned, PHP often converts one type to another when needed in this article, but PHP programmers can also force such conversions to occur. In either case, the pilot should know what is expected of the result.

Type conversion behavior

Here are some general rules for converting PHP from one type to another:

integer-to-fold precision floating-point number: establishes a precision-compliant, double-precision floating-point number (for example, int4 to double4.0 times).

Double-precision floating-point number to integer: The fractional part is abandoned.

Number to Boolean: False if it is equal to 0, otherwise true.

Number to string: The string is created by the appearance of the digital display. The integer output is displayed as a sequence of numbers, and the double-precision floating-point number is output at the minimum precision required, and the multiple-precision floating-point value after the decimal point is converted to the notation notation.

Boolean to number: If true, gets 1, or 0 if False.

Boolean string: If True, gets 1, or false to get an empty string.

Null to number: 0

Null to Boolean:false

string-to-number: the equivalent of "reading" a number from a string and then casket it to a given type. If the number cannot be read, the value is 0, and not all strings need to be readable to be successful.

String to Boolean: False if it is an empty string or 0, otherwise true.

Array of simple Type (number or string): equivalent to creating a new array with this value assigned to index 0.

Array to number: undefined (see instructions below)

Array to Boolean: False if the array has no elements, otherwise true.

Array to string: that is "arryay".

object to number: undefined (see the instructions below).

Object to Boolean: True if the object has a value that contains an any-member variable, false otherwise.

Object to string: "object".

Resources to Boolean:false.

Resource to Number: undefined (see below for instructions)

Resources to numbers: sometimes like "resource ID #1" (but not very dependent on this Convention).

In the above list, we find that some types are undefined when converting to numeric types, and in the above description, undefined means that the PHP designer does not have a final conversion rule in the unused PHP version, so the bad news must be determined according to your programming. You may find that the specific PHP version you are using can be converted to a numeric type, but it may not work in the next version.

The above is the PHP learning book-The sixth chapter (sequel) 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.