PHP Knowledge Point Summary 2

Source: Internet
Author: User
Tags array sort sorts

PHP5 Data types

String (String), Integer (integer), float (floating-point), Boolean (Boolean), Array (array), Object (objects), null (null value).

string
You can put any text in single and double quotation marks:

<?php $x = "Hello world!"; echo $x; echo "<br>"; $x = ' Hello world! '; Single quotes including string literals double quotation marks contain strings that can contain variable echo $x;? >

Integral type
In the following example we will test for different numbers. The PHP var_dump () function returns the data type and value of the variable:

? php $x = 5985;var_dump ($x); echo "<br>"; $x =-345; Negative var_dump ($x); echo "<br>"; $x = 0x8c; Hexadecimal number var_dump ($x); echo "<br>"; $x = 047; Octal number Var_dump ($x);? >

Floating point Type

<?php $x = 10.365;var_dump ($x); echo "<br>"; $x = 2.4e3;var_dump ($x); echo "<br>"; $x = 8e-5;var_dump ($x);? >

Boolean type

The Boolean type can be TRUE or FALSE.

Array

Arrays can store multiple values in a variable

<?php $cars =array ("Volvo", "BMW", "Toyota"); Var_dump ($cars);? >

Object

In PHP, the object must be declared.

First, you must declare the class object using the Class keyword. A class is a structure that can contain properties and methods.
Then we define the data type in the class and then use the data type in the instantiated class:

<?phpclass car{    var $color;    function Car ($color = "green") {      $this->color = $color;    }    function What_color () {      return $this->color;    }} function Print_vars ($obj) {   foreach (Get_object_vars ($obj) as $prop = = $val) {     echo "\t$prop = $val \ n"; 
   
    }}//Instantiate one Object$herbie = new Car ("white");//Show Herbie Propertiesecho "\herbie:properties\n";p rint_vars ($ Herbie);? >  
   

NULL Value
A NULL value indicates that the variable has no value. Null is a value with a data type of NULL.

<?php$x= "Hello world!"; $x =null;var_dump ($x);? >

Constant

A constant is an identifier for a simple value. The value cannot be changed in the script. (The constant name does not require a $ modifier).

Note: Constants can be used throughout the script.

To set constants, use the Define () function with the following function syntax:

Define (string constant_name, mixed value, case_sensitive = True)
The function has three parameters:

    • Constant_name: Required parameter, constant name, or marker.
    • Value: A required parameter, a constant.
    • Case_sensitive: Optional parameter that specifies whether case sensitive, set to True indicates insensitivity.

The following example creates a case-sensitive constant with a constant value of "Welcome to w3cschool.cc!" :

<?phpdefine ("Greeting", "Welcome to w3cschool.cc!"); echo greeting;? >

String functions and string connections

<?php$txt1= "Hello world!"; $txt 2= "What a nice day!"; echo $txt 1. " " . $txt 2; String join operator. Echo strlen ("Hello world!"); Gets the string length echo strpos ("Hello world!", "World"); Gets the substring position//The position of the first character in the string is 0?>
Operator
<?php//Other operators slightly: logical operators! && | | The and or xor//array operation conforms to and: + Comparison: = = = = = = = =!== $x = Array ("A" = "red", "b" = "green"); $y = Array ("c" = "Blue", "d" = "yellow"); $z = $x + $y;  $x and $y Arrays merge Var_dump ($z), var_dump ($x = = $y), var_dump ($x = = = $y), var_dump ($x! = $y); Var_dump ($x <> $y); Var_dump ($x !== $y);? >
Process Control statement with JS
<?php$t=date ("H"), if ($t < ") {echo" has a good day! ";} $t =date ("H"), if ($t < ") {echo" has a good day! ";} Else{echo "a good night!";} $t =date ("H"), if ($t < "ten") {echo "has a good morning!";} else if ($t < ") {echo" has a good day! ";} Else{echo "a good night!";} $favcolor = "Red"; switch ($favcolor) {case ' red ': echo "Your favorite color is red!"; Break;case "Blue": echo "Your favorite color is blue!"; Break;case "green": echo "Your favorite color is green!"; Break;default:echo "Your favorite color is neither red, blue, or green!";} Loop $i=1;while ($i <=5) {echo "the number is". $i. "<br>"; $i + +;} $i =1;do{$i ++;echo "The number is". $i. "<br>";} while ($i <=5), for ($i =1; $i <=5; $i + +) {echo "the number is". $i. "<br>";} $x =array ("One", "one", "one", "three"), and foreach ($x as $value) {echo $value. "<br>";}? >
Array
<?php$cars=array ("Volvo", "BMW", "Toyota"); Array defines the array of values echo "I like". $cars [0]. ", " . $cars [1]. "and". $cars [2]. "."; Access array elements//array length count () $cars =array ("Volvo", "BMW", "Toyota"); Echo count ($cars); The count () function is used to return the length of the array//iterate over the Valarray $cars=array ("Volvo", "BMW", "Toyota"), $arrlength =count ($cars); for ($x =0; $x <$ Arrlength, $x + +) {echo $cars [$x];echo "<br>";} $age =array ("Peter" and "+", "Ben" = "Notoginseng", "Joe" = "43");//define Associative array echo "Peter is". $age [' Peter ']. "Years old."; /Traverse Associative array $age=array ("Peter" and "+", "Ben" = "PNs", "Joe" = "+"), foreach ($age as $x = + $x _value) {echo "key=". $x . ", value=." $x _value;echo "<br>";}? >
Array sorting

PHP-Array sorting function

    • Sort ()-ascending order of arrays
    • Rsort ()-Descending order of the array
    • Asort ()-Sets the array in ascending order based on the values of the associative arrays
    • Ksort ()-an array is sorted in ascending order based on the keys of the associative arrays
    • Arsort ()-sorts arrays in descending order based on the values of the associative array
    • Krsort ()-Sorts the arrays in descending order based on the keys of the associative array
Sort () $cars =array ("Volvo", "BMW", "Toyota"), sort ($cars), $clength =count ($cars); for ($x =0; $x < $clength; $x + +) { echo $cars [$x];echo "<br>";} Rsort ()   $cars =array ("Volvo", "BMW", "Toyota"), Rsort ($cars);   Asort () Arsort () associative array sort value$age=array ("Peter" and "Arsort", "Ben" = "Panax Notoginseng", "Joe" = "a"), Asort ($age); );//ksort () Krsort () associative array sort key$age=array ("Peter" and "Krsort", "Ben" = "Panax Notoginseng", "Joe" = "a"), Ksort ($age); );? >
Super Global variables

Several super global variables (superglobals) are predefined in PHP, which means they are available in all scopes of a script.

    • $GLOBALS
    • $_server
    • $_request
    • $_post
    • $_get
    • $_cookie
    • $_files
    • $_env
    • $_session

$GLOBAL is a globally combined array that contains all the variables. The name of the variable is the key of the array.

<?php $x = 75; $y = 25; function addition () {$GLOBALS [' z '] = $GLOBALS [' x '] + $GLOBALS [' Y '];} addition (); echo $z;?>

$_server is an array of information, such as header information (header), path, and script location (scripts locations), and so on. The items in this array are created by the WEB server. There is no guarantee that all the projects are available on each server;

<?php echo $_server[' php_self ']; /try/demo_source/demo_global_server.phpecho "<br>"; Echo $_server[' server_name '); w3cschool.cc echo "<br>"; Echo $_server[' http_host ']; Www.w3cschool.ccecho "<br>"; Echo $_server[' http_referer '); echo "<br>", Echo $_server[' http_user_agent '); The browser echo "<br>"; Echo $_server[' script_name '); /try/demo_source/demo_global_server.php//also $_server objects include SERVER_ADDR, Server_protocol, REQUEST_METHOD. Properties?>

$_request is used to collect data submitted by HTML forms.

$_post is widely used to collect form data, specifying this attribute in the HTML form tag: "Method=" POST "

$_get is also widely used to collect form data, specifying this attribute in the HTML form tag: "method=" GET ".

$_get can also collect the data sent in the URL.

PHP Knowledge Point Summary 2

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.