[Basics] PHP variables and variable scopes, php variable scopes

Source: Internet
Author: User

[Basics] PHP variables and variable scopes, php variable scopes

New PHP, more interesting syntax, record.

1. Scope of Variables

The scope can only be divided into two Global and Local. Compared with the entire. php file, Global is the smallest Local range and the closest range to the variable. For example, in a function, the class is medium.

2. Variable Declaration

It may be because the PHP variable has a unique $ symbol, so it does not need keywords to declare the PHP variable (except in the class). It is automatically created when the first value is assigned.

The class is quite unique. Because the class has the member attributes private, public, and protected, you need to modify keywords when declaring variables in the class. The preceding keyword or var is used, but the two cannot be shared.

3. Example

1 <? Php 2 3 $ VarFile = "Var_File"; // variable declaration 4 5 $ nr = array ("\ n", "\ r", "\ n \ r ", "\ r \ n"); 6 7 8 class TestClass 9 {10/* 11 * declare 12 * $ Var = value with the var and private keywords respectively; this type cannot be 13 * var private $ var; this type cannot be 14 * var private $ var = value; this type cannot be 15 * var $ var; this type cannot be 16 */17 18 var $ VarClass = 'varclass declared by keyword var'; 19 private $ VarClass2 = 'varclass2 declared by keyword private '; 20 21/* 22 * Add the global keyword, use g The variable declaration method in the variable 23 * function in the lobal scope is the same as that in the global 24 */25 26 Function GetAllVar () {27 global $ VarFile; 28 $ VarFunction = 'var _ function '; 29 30 $ Temp = "$ VarFile = ". $ VarFile. "\ r \ n ". 31 "VarClass = ". $ this-> VarClass. "\ r \ n ". 32 "VarClass2 = ". $ this-> VarClass2. "\ r \ n ". 33 "VarFunction = ". $ VarFunction. "\ r \ n"; 34 35 36 return str_replace ($ GLOBALS ['nr '], "<br/>", $ Temp ); 37} 38 39 40 41} 42 43 $ MyClass = new TestClass ("inline"); 44 echo $ mycia Ss-> GetAllVar (); 45 46?>

 

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.