PHP6: PHP basic data type

Source: Internet
Author: User
PHP6: basic PHP Data types This chapter describes the basic PHP Data types.
I believe we are familiar with any language in C/C ++, C #, or Java. This chapter uses C # as a comparison language.
OK. if you want to learn PHP, what are your first considerations?
I believe it must be the following questions:
Which data types does PHP have? what are their differences with other languages? Which of the following is a language that divides data types into strong and weak types? what is PHP? What's unique about PHP? Based on the above ideas, I will explain them one by one.
Let's take out the code referenced in the previous article first.

PHP instance code
1 function display_user_urls ($ url_array)
2 {
3 // display the table of URLs
4
5 // set global variable, so we can test later if this is on the page
6 global $ bm_table;
7 $ bm_table = true;
8?>
9

10
36 37}

Let's discuss it one by one.
Which data types does PHP have? what are their differences with other languages? PHP supports eight original data types.
There are four scalar types:
Integer Float String Boolean contains two composite types:
Array Object, but there are two special types:
If NULL Resource is written here, you may start to whisper: how do they represent them in the program? Okay. let's take a look at the first six data types. There are two types to be explained later.
Integer: int Float: float String: string Boolean: bool is actually similar to C.
The Array data type is quite special, because this data type is not available in C #, and does not exist in other languages, C/C ++ and Java. You can treat it as an array in C. In addition, let's see what PHP says on its official website.
Arrays in PHP are actually an ordered graph. A graph is a type that maps values to keys. This type has been optimized in many aspects, so it can be used as a real array, or a list (vector), a scattered list (an implementation of a graph), a dictionary, set, stack, queue, and more possibilities. Because another PHP array can be used as the value, and the tree can be easily simulated.
Let's take a look at the usage of Array. We can create an Array object because the Array is mapped to the key and value. Therefore, Array supports a certain number of value pairs separated by semicolons. For example, the code is as follows:

$ Arr = array ("Hello" => "Word", 12 => true );

Echo $ arr ["Hello"]; // bar
Echo $ arr [12]; // 1
?>

Remember that the key can be an integer or a string. If the key name is a standard expression of an integer, it is rounded up. if the key name is float, it is converted to an integer. In comparison, the value can be any value.
What if you want to implement multi-dimensional arrays? PHP does not provide multidimensional arrays of classes and C #. why? In my opinion, the array here is no longer a pure array in languages such as C. It has extended arrays, hash tables, dictionaries, and so on. Therefore, it is unnecessary to provide multi-dimensional arrays. However, similar functions can be implemented using the following code:

$ Arr = array ("somearray" => array (6 => 5, 13 => 9, "a" => 42 ));

Echo $ arr ["somearray"] [6]; // 5
Echo $ arr ["somearray"] [13]; // 9
Echo $ arr ["somearray"] ["a"]; // 42
?>

Now, you may think about how to create or delete an array object. Let's take a look at the following code:

1? Php
2 $ arr = array (5 => 1, 12 => 2 );
3 $ arr [] = 56; // equivalent to $ arr [13] = 56;
4 // because the key of the second element is 12
5 $ arr ["x"] = 42; // create a new element with the key as a string
6 // "x", with a value of 42.
7 unset ($ arr [5]); // delete the key position 5 element.
8 unset ($ arr); // delete the entire array
9 // unset () is a function, literally
// You can also see that it is the revocation setting.
10?>

How about Array.
As for the Object, the feeling is no different from that in C.

NULL is interesting. this is not found in C #, but C # has a null.

A special NULL value indicates that a variable has no value. The only possible value of the NULL type is NULL. Note that the NULL type is introduced in PHP 4.

The variable is considered NULL in the following cases:

The value is NULL.

Not assigned.

Unset ().

To put it simple, we used null in C # as an object. Smart guys.
In this case, how can we determine whether a variable is NULL is quite simple? call is_null () and set a variable to NULL? one way is to call unset ().

Let's look at Resource, which is called a Resource. It saves a reference to an external resource. Resources are created and used through specialized functions. Because resource type variables are saved as special handles for opening files, database connections, and graphic canvas areas, other types of values cannot be converted to resources. Because the PHP4 Zend Engine introduces a resource counting system, it can automatically detect that a resource is no longer referenced (the same as Java ). In this case, all external resources used by the resource are released by the garbage collection system. For this reason, some free-result functions are rarely used to manually release the memory. Garbage collection?
Are there other things besides this? Certainly. There are also some pseudo types:
Mixed
Mixed indicates that a parameter can accept multiple (but not all) types.

For example, gettype can accept all PHP types, and str_replace can accept strings and arrays.

Number
Number indicates that a parameter can be int or float.

Callback
Some functions such as call_user_function () or usort () use user-defined functions as a parameter. The Callback function can be not only a simple function, but also an object method, including a static class method.

2. Which of the following is a language that divides data types into strong and weak types? what is PHP?

PHP is weak. In PHP, variable types are determined by the values assigned to them. If the value is an integer, the variable is an integer. if the value is a string, it is a string. See the following code:

$ Name = "your name"; // name string type.
$ Total = 12; // total is an integer.
?>

So how should we transform it? I think it's similar to C. I will not go into details.

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.