PHP basic type

Source: Internet
Author: User
PHP basic type
Basic type         
 Back to homepage
          

1. Boolean type:

The value can be true or false. To convert a value to a boolean value explicitly, use (bool) or (boolean) to forcibly convert the value. "; Var_dump ($ a_bool); echo"
"; Var_dump (bool)" "); // bool (false) echo"
"; Var_dump (bool) 1); // bool (true) echo"
"; Var_dump (bool)-2); // bool (true) echo"
"; Var_dump (bool)" foo "); // bool (true) echo"
"; Var_dump (bool) 2.3e5); // bool (true) echo"
"; Var_dump (bool) array (12); // bool (true) echo"
"; Var_dump (bool) array (); // bool (false) echo"
"; Var_dump (bool)" false "); // bool (true)?>

2. integer:

Integer is out of the range and will be interpreted as float. If the calculation result exceeds the integer range, float is returned.
"; Var_dump ($ a1); echo"
"; Var_dump ($ a2); echo"
"; Var_dump ($ a3); echo"
"; Var_dump ($ a4); $ large_number = 2147483647; echo"
"; Var_dump ($ large_number); // The output is: int (2147483647) $ large_number = 2147483648; echo"
"; Var_dump ($ large_number); // The output is: float (2147483648) // It is also applicable to hexadecimal integers: from 2 ^ 31 to 2 ^ 32-1: echo"
"; Var_dump (0 xffffffff); // output: float (4294967295) // It is not applicable to the number of hexadecimal representations greater than 2 ^ 32-1: echo"
"; Var_dump (0x100000000); // output: int (2147483647) $ million = 1000000; $ large_number = 50000 * $ million; echo"
"; Var_dump ($ large_number); // output: float (50000000000)?>
PHP does not have the division operator. 1/2 generates float 0.5. You can always discard the fractional part or use the round () function.
Convert from boolean value to FALSE to 0, and TRUE to 1. "; Var_dump (25/7); // float (3.5714285714286) echo"
"; Var_dump (int) (25/7); // int (3) echo"
& Quot; var_dump (integer) (25/7); // int (3) echo & quot"
"; Var_dump (intval (25/7); // int (3) echo"
"; Var_dump (round (25/7); // float (4)?>

3. Floating point

Floating-point numbers (also called floating-point numbers, double-precision numbers or real numbers) can be defined using any of the following syntax:
Never believe that the result of a floating point number is accurate to the last digit, or compare whether two floating point numbers are equal. If higher precision is required, use any precision mathematical function or gmp function.
"; Var_dump ($ af); echo"
"; Var_dump ($ bf); echo"
"; Var_dump ($ cf);?>

4. Strings

Strings can be defined using single quotes, double quotes, and delimiters.
If double quotation marks ("") are used to enclose strings, PHP understands the escape sequences of more special characters. \ N Newline \ r carriage return \ t horizontal tab \ backslash \ $ \ "double quotation marks
\ [0-7] {} the regular expression sequence matches a character represented by the octal symbol
\ X [0-9A-Fa-f] {} this regular expression matches a sequence of characters represented by a Hexadecimal Symbol
"; Echo 'This is a simple string'; echo"
"; Echo 'You can also have embedded newlines in strings this way as it isokay to do '; echo"
"; Echo 'Arnold once said:" I \'ll be back "'; // Outputs: Arnold once said:" I'll be back "echo"
"; Echo 'You deleted C :\\*.*? '; // Outputs: You deleted C :\*.*? Echo"
"; Echo" \ n Newline \ r carriage return \ t horizontal tab \ backslash \ $ \ "double quotation marks"; echo"
"; Echo 'You deleted C :\*.*? '; // Outputs: You deleted C :\*.*? Echo"
"; Echo 'This will not expand: \ n a newline'; // Outputs: This will not expand: \ n a newlineecho"
"; Echo 'variables do not $ expand $ either '; // Outputs: Variables do not $ expand $ either?>
Delimiters: Another method used to define the string using the delimiter syntax ("<"). < <之后提供一个标识符,然后是字符串,然后是同样的标识符结束字符串。
Use echo to output large html and javascript scripts
1. PHP delimiters act as they are, including the line feed format or something, and output something inside them;
2. do not escape any special characters in the PHP delimiters;
3. the PHP variable in the PHP delimiter will be replaced with its value normally.

1. in < <之后的字符eof是自己定义的,随便什么都是可以的,但是结尾处的字符一定要和他一样,他们是成对出现的,就像{}这样的??这是最基本的;
2. the end of a line (Eof; in the preceding example) must start with another line, and change the line except Eof; the end of the separator cannot contain any other characters, including spaces. If there are spaces and tabs at the beginning or end of the line, you will receive the following error message: Parse error: parse error, unexpected $ end in ......, A syntax error is prompted;
3. if PHP variables appear in the middle of the delimiters, you only need to write them as if they were output in other strings. the variable $ name should be included in {} to tell the PHP parser that this is a PHP variable. In fact, this variable is not needed, but may be ambiguous.

5. array

Arrays in PHP are actually an ordered ING. Valuing is a type that associates values with keys. This type has been optimized in many aspects, so it can be considered as a real array, or a list (vector), a hash list (an implementation of ING), a dictionary, a set, stack, queue, and more possibilities. The value of an array element can also be another array. Tree structure and multi-dimensional array are also allowed.
Value pair. $ Arr = array ("foo" => "bar", 12 => true); echo"
"; Echo $ arr [" foo "]; // barecho"
"; Echo $ arr [12]; // 1?>
The key can be an integer or string. If the key is a standard expression of an integer, it is interpreted as an integer (for example, "8" will be interpreted as 8, and "08" will be interpreted as "08 "). The floating point number in the key is rounded to integer. In PHP, the index array and the associated array are the same. they can both contain the subscript of the integer and string. The value can be of any PHP type.
Array (6 => 5, 13 => 9, "a" => 42); echo"
"; Echo $ arr [" somearray "] [6]; // 5 echo"
"; Echo $ arr [" somearray "] [13]; // 9 echo"
"; Echo $ arr [" somearray "] [" a "]; // 42?>
Create/modify $ arr [key] = value; $ arr [] = value;/key can be integer or string; value can be any type of value
If $ arr does not exist, a new one is created. This is also a replacement method for defining arrays. To change a value, just assign it a new value. If you want to delete a key/value pair, you need to use unset () for it ().
1, 12 => 2); $ arr [] = 56; // This is the same as $ arr [13] = 56; // at this point of the script $ arr ["x"] = 42; // This adds a new element to // the array with key "x" unset ($ arr [5]); // This removes the element from the arrayunset ($ arr ); // This deletes the whole arrayecho"
"; Print_r ($ arr); // Create a simple array echo"
Create an array "; $ array = array (1, 2, 3, 4, 5); print_r ($ array); // delete all the elements, but keep the array unchanged: foreach ($ array as $ I => $ value) {unset ($ array [$ I]);} echo"
Delete the element "; print_r ($ array); // add a unit (note that the new key name is 5 instead of the value 0 you may think) $ array [] = 6; echo"
Add a unit "; print_r ($ array); // re-index: echo"
Re-index "; $ array = array_values ($ array); $ array [] = 7; print_r ($ array);?> The unset () function allows you to delete a key from an array. However, note that the array will not rebuild the index.
Array_values (); re-create an index
The foreach control structure is specially used for arrays. It provides a simple method to traverse arrays.
"; Echo" \ nChecking $ I: \ n "; echo"
"; Echo" Bad: ". $ array ['$ I']." \ n "; echo"
"; Echo" Good: ". $ array [$ I]." \ n "; echo"
"; Echo" Bad: {$ array ['$ I']} \ n "; echo"
"; Echo" Good: {$ array [$ I]} \ n ";}?>
1 Use array ()
'Red', 'taste '=> 'sweet', 'shape' => 'round ', 'name' => 'apple', 4 // key d be 0 ); echo"
"; Print_r ($ a); $ B = array ('A', 'B', 'C'); echo"
"; Print_r ($ B );//... is completely equivalent with this: $ a = array (); $ a ['color'] = 'red'; $ a ['taste '] = 'sweet '; $ a ['shape'] = 'round '; $ a ['name'] = 'apple'; $ a [] = 4; // key will be 0 echo"
"; Print_r ($ a); $ B = array (); $ B [] = 'a'; $ B [] = 'B '; $ B [] = 'C'; echo"
"; Print_r ($ B);?>
2 sets
4, 'OS' => 'Linux ', 'Lang' => 'INC', 'short _ tags' => true); echo"
"; Print_r ($ map); // strictly numerical keys $ array = array (0,156,-10); // this is the same as array (0 => 7, 1 => 8 ,...) echo"
"; Print_r ($ array); $ switching = array (10, // key = 0 5 => 6, 3 => 7, 'a' => 4, 11, // key = 6 (maximum of integer-indices was 5) '8' => 2, // key = 8 (integer !) '02' => 77, // key = '02' 0 => 12 // the value 10 will be overwritten by 12); echo"
"; Print_r ($ switching); // empty array $ empty = array (); echo"
"; Print_r ($ empty);?>
3 sets
"; // Workaround for older versionsforeach ($ colors as $ key => $ color) {$ colors [$ key] = strtoupper ($ color);} echo"
"; Print_r ($ colors);?>
4 subscript array starting from 1
'Manual', 'February ', 'march'); echo"
"; Print_r ($ firstquarter);?>
5 fill the array

6. sort arrays

7. recursive and multi-dimensional arrays
Array ("a" => "orange", "B" => "banana", "c" => "apple"), "numbers" => array (1, 2, 3, 4, 5, 6), "holes" => array ("first", 5 => "second", "third ")); // Some examples to address values in the array above echo"
"; Echo $ fruits [" holes "] [5]; // prints" second "echo"
"; Echo $ fruits [" fruits "] [" a "]; // prints" orange "unset ($ fruits [" holes "] [0]); // remove "first" // Create a new multi-dimen1_array $ juices ["apple"] ["green"] = "good";?>
8. use the & reference operator to copy the array through reference.
"; Print_r ($ arr2); // echo"
"; Print_r ($ arr3); //?>

6. object

To create a new object for object initialization, use the new statement to instantiate a class: "; Echo" Doing foo. ";}}$ bar = new foo; $ bar-> do_foo () ;?>
"; Echo $ obj-> scalar; // outputs 'ciao'?>

7. NULL

A special NULL value indicates that a variable has no value. The only possible value of the NULL type is NULL.
The value is NULL. Not assigned. Unset ().
"; # Echo $ var; // outputs 'ciao' var _ dump ($ var)?>

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.