PHP Learning Notes

Source: Internet
Author: User

Directory 1.PHP variable 2.PHP constant 3.PHP string 4.PHP array 5.PHP function

I. PHP base 1.PHP variables
Header("content-type:text/html; Charset=utf-8 ");/*PHP has three different variable scopes: local (local) global (global) static (static)*///variables declared outside the function have Global scope and can only be accessed outside of the function. Variables declared inside a function have a local scope and can only be accessed inside the function.     $x= 5;//Global Scope    functionmyTest () {$y= 10;//Local Scope    } //The Global keyword is used to access variables within the function.     $x=5; $y=10; functionmyTest () {Global $x,$y; $y=$x+$y;    } myTest (); Echo $y;//The output 15//php also stores all global variables in an array named $GLOBALS [index]. The subscript contains the variable name. $GLOBALS [' x '] = 5//$GLOBALS [' y '] = ten

2.PHP Constants
Header("content-type:text/html; Charset=utf-8 ");//valid constant names begin with a character or underscore (there is no $ symbol in front of the constant name). /*to set a constant, use the Define () function-it uses three parameters: 1. The first parameter defines the name of a constant 2. The second parameter defines the value of the constant 3. The optional third parameter specifies whether the constant name is case-sensitive. The default is False. *///constants that are case sensitiveDefine("Greeting", "Welcome");Echogreeting;//constants that are not sensitive to caseDefine("Greeting", "Welcome",true);Echogreeting;

3.PHP string
//1. The difference between single and double quotes    #String variables directly contained in double quotation marks    #the contents of a single quote string are always considered ordinary characters    $str= ' Hello '; Echo"Str is$str";//Run Result: STR is Hello    Echo' Str is $str ';//operation Result: STR is $str//2. Remove whitespace from the end of a string    #trim removes spaces at both ends of a string.     #RTrim is to remove a string to the right of the space    #LTrim is to remove the left space of a string//3. Gets the length of the string    #English character length    strlen($str); #Chinese character lengthMb_strlen ($str, "UTF8");//4. Interception of Strings    #The Intercept function of the English string substr ()    #substr (string variable, start intercept position, intercept number)    $str= ' I love You '; Echo substr($str, 2, 4); #intercept function of Chinese string mb_substr ()    #mb_substr (string variable, start intercept position, intercept number, page encoding)    $str= ' I love you, China '; EchoMB_SUBSTR ($str, 4, 2, ' UTF8 ');//5. String Lookup    #Strpos (string to be processed, string to position, starting position of position [optional])    $str= ' I want to study at IMOOC '; $pos=Strpos($str, ' Imooc ');//6. String substitution    #Str_replace (The string to find, the string to replace, the string to be searched, and the replacement to count [optional])    $str= ' I want to learn JS '; $replace=Str_replace(' JS ', ' php ',$str);//7. Merging and splitting of strings    #string merge function implode (): Combining array elements into a single string    #implode (delimiter [optional], array)    $arr=Array(' Hello ', ' world! '); $result=implode(‘‘,$arr); Print_r($result);//The results show Hello world!    #string-delimited function explode (): function returns an array consisting of strings    #explode (delimiter [optional], string)    $str= ' Apple,banana '; $result=Explode(‘,‘,$str); Print_r($result);//results show Array (' Apple ', ' banana ')

4.PHP arrays
//Create an empty array$str=Array();//indexed array: The key of an array is an array of integers, and the integer order of the keys starts at 0, and so on. $fruit=Array("Apple", "banana", "pineapple");//Indexed array Assignment://1. Assign a value with the name of the array variable followed by a bracket$arr[0]= ' Apple ';//Use the-= symbol to separate keys and values, the left for the key, and the right for the value. Array(' 0 ' = ' apple ');//count ($arr) returns the array length//for iterating through the values in the indexed array for($i= 0;$i<Count($fruit);$i++){    Echo"$fruit[$i]<br> ";}//foreach iterates through the values in an indexed arrayforeach($fruit  as $k=$v){    Echo $k." ...".$v." <br> ";}//indexed Array Sorting#sort arrays in ascending orderSort($fruit)#sort the array in descending orderRsort($fruit)

//associative array: The key of an exponential group is an array of strings$fruit=Array(' apple ' = ' apple ', ' banana ' and ' banana ', ' pineapple ' and ' pineapple ');//Associative array assignment//1. Assign a value with the name of the array variable followed by a bracket$arr[' Apple ']= ' apple ';//2. Use the-= symbol to separate the keys and values, the left for the key, and the right to indicate the valueArray(' apple ' = ' apple ');//The Foreach loop accesses the values in the associative arrayforeach($fruit  as $k=$v){    Echo' <br> Fruit's English Key name: '.$k. ', the corresponding value is: '.$v;}//Associative array sorting#sort associative arrays in ascending order according to the keyKsort($fruit) #sort associative arrays in descending order according to the keyKrsort($fruit) #sort associative arrays in ascending order based on valuesAsort($fruit) #sort associative arrays in descending order based on valuesArsort($fruit)

5.PHP function
// a user-defined function declaration begins with "function".//The function name can begin with a letter or an underscore (not a number). The function name is not sensitive to capitalization.  function  functionname () {  the code being executed;}


// determine if a function exists function func () {} if (function_exists(' func ')) { echo ' exists ';}

PHP Learning Notes

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.