Definition and scope of PHP Variables

Source: Internet
Author: User
Tags array definition variable scope

I. Definition and scope of PHP Variables

Variable:When calling properties in a method, you cannot directly use echo. You must use the $ this object in the class.

1. php variable definition and usage: starting with an underscore ^ [A-Za-Z _] \ W *

2. php variable scope:

Local scope: variables defined within the function can only work within the function.

Global scope: global $ VaR

$ Goobals ['var']

3. assign values to variables (pass values; pass references): 1. Pass values:

$ Num1 = 10; $ num2 = $ num1; $ num1 = 20; echo $ num2;

// Pass the default value assignment of PHP variables without changing the original value // output 10

2. transfer a reference:

// The reference is passed through &. The reference is to save the value in the address.

4. Super global variables (pre-defined variables ):

① $ Globals save global variables

② $ _ Server saves Server Information

③ $ _ Get save get data

④ $ _ Post save Post Data

⑤ $ _ FILES: Save the submitted file

⑥ $ _ COOKIE: the cookie saved

7$ _ Session: Save session data

Save $ _ Request to save request data

Environment $ _ env save environment

2. Constants

The define () function can define constants.

Defined () determines whether a constant is defined

Features:

(1) do not use the definition of constants $

(2) constants can be used anywhere, regardless of the scope.

(3) Once a constant is defined, it cannot be re-copied.

 

Magic constants (7 ):

_ File _ Get the absolute path and file name of the current file

_ Line _ current row number of the file

Iii. Control Structure

1, return;

(1) Use return in the function to immediately stop the function execution and return the function value;

(2) Use it in a PHP script to stop the script fileCodeExecution

 

2, require_once ()

Similar to require (), it is used to introduce external files. The difference is that require_once (). If the file is already included, it will not be included again. This avoids Function Definition and variable re-assignment.

 

3. The difference between include () and require () is that when include () contains files repeatedly, an error at the warning level is generated; require () produces a fatal error.

Iv. Functions

Parameter passing is also a value passing by default. Even if the parameter value is modified inside the function, the value outside the function is not affected.

If you want to affect the transfer through reference, the value of this variable is referenced in the memory space.

 

V. PHP Data Types

1. scalar type

String

1. String Definition

Single quotation marks: single quotation marks will output the content intact-fast execution speed

Double quotation marks (comparison): Double quotation marks parse variables-slow execution speed

2. Common string functions (important)

Search:

// Strpos (haystack, needle); Find the position where the string appears for the first time

$ Str3='You are beauul ul! ';

$='A';

$ B= Strpos ($ Str3,$);//Position where the string appears for the first time

// Strrchr () Returns the substring starting from the last occurrence of the string.

Replace:

// Trim ()Removes the white space at the beginning and end of the string.

EchoTrim ($ Str4);//Remove leading and trailing Spaces

// Str_replace (String to be searched,Replaced string,In which string);

$ Str5= _ File __;

$ Str6= Str_replace ('String. php','',$ Str5);

// Substr_replace ()Replace part of the string with another string.

EchoSubstr_replace ($ Str8,'Mathbook',11);//Replace

// Substr (string, start, length); Returns the substring of a string.

EchoSubstr ($ Str8,8);//SlaveMStart intercepting

 

Comparison:

// Strcasecmp () compares strings in case-insensitive mode. If 0 is returned, the strings are equal.

// Convert all strings to lower-case strtolower ($ str1 );

// Convert all strings to uppercase strtoupper ($ str2 );

 

Split:

// ExplodeSplits a string into an array based on a delimiter.

$ Str_1='Php, 3G,. net, Java';

$ Str_2= Explode (',',$ Str_1);//Splits a string into an array based on a delimiter.

Var_dump ($ Str_2);

//Implode ()Concatenates an array element into a string using a delimiter.

$ arr = array ( 'php' , '3g ' , '.. Net ' , 'java' );

Echo$ Str_3= Implode ($ Arr,',');//Concatenate arrays based on delimiters

3. Regular Expression matching:

Preg_match ()Regular Expression matching

// PHPRegular Expressions are supported. Regular Expressions can be used as a sieve,JSYou can use it,PHPYou can also use

EchoPreg_match ('/(Http: \/W {3} \.) (. +)/I','Http: // www.sina.com',$ Match);

//If the third parameter is provided, all results matching the entire regular expression will be placed in the array.

Echo $ Domain=$ Match[2];

Preg_replace (Regular Expression,Result of replacement,String to be searched)

 //SetSina.comReplace360. com

EchoPreg_replace ('/Sina \. com /','2014. com','Http: // www.sina.com');

// Replace the matching result according to the Regular Expression

 

Integer

Floating Point Type

Boolean

2. Composite Type

Array:

1. array definition array () or []

2. traverse the Array (Focus)

(1) Use for () to traverse the array. It is suitable for the continuous integer index array with subscript starting from 0.

$ Arr=Array('Php','3g');

// Count ()Count the number of array elementsThat is, the length of the array.

For($ I=0;$ I<Count ($ Arr);$ I++ ){

Echo$ Arr[$ I];

}

(2) foreach () traverses the Array

$ Array=Array('Php','. Net','Subobject'=>'Java');

 Foreach($ ArrayAs$ Key =>$ Value){

Echo$ Key.':'.$ Value;

Echo'<Br/>';

}

3. array pointer

(1) Current () gets the array element pointed to by the current array pointer

(2) Next () points the array pointer to the next

(3) Prev () moves the array Pointer Forward by

(4) reset () reset the array pointer

(5) end () Move the array pointer to the end

4. Common array Functions

Array_push () is used to import one or more elements to the end of an array.

Array_pop () outputs the stack and pops up the last element of the array.

Array_shift () Removes elements starting with an Array

Array_unshift () inserts an element at the beginning of the array.

 

Object

3, Special Type

Null

Resource

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.