Php: variables, types, and scopes

Source: Internet
Author: User
: This article mainly introduces php. For more information about variables, types, and scopes, see PHP tutorials. Determine whether a variable exists

Isset ($ var); // only determines whether the variable exists. the variable existence is true;

The judgment variable is empty.

! Empty ($ var); // "", 0, "0", NULL, FALSE, array (), var $ var; and objects without any attributes are empty when they are determined by empty, and true is returned;

Determines whether it is false.

if(0 == false){echo"true";}    //trueif(0.0 == false){echo"true";}  //trueif(array() == false){echo"true";}  //trueif("0" == false){echo"true";}  //trueif('' == false){echo"true";}   //trueif(NULL == false){echo"true";} //true

Static variable: Memory resident variable

functiona(){static$a = 1;    echo$a;    $a++;}a();//1a();//2

Constant: Read-only variable

define("TEST",'codekissyoung');echo TEST;//codekissyoung

Variable: php is a language for dynamic real-time parsing.

$a = "test";$test = "i am the test";functiontest(){echo"i am function test!";}   echo$a; //testecho$$a; // i am the test$a(); //i am function test!

Split the assoc array into variables

foreach ($_POSTas$key => $value) {            $$key = $value;        }

Dynamic new object and call function

//example.com?class=person&func=run$class=$_GET['class'];$func=$_GET['func'];$obj=new$class();$obj->$func();

Variable reference

$ A = "ABC"; $ B = & $ a; echo $ a; // output here: ABC echo $ B; // output here: ABC $ B = "EFG"; echo $ a; // Here, the value of $ a is changed to EFG, so the EFG echo $ B is output. // EFG is output here.

Combined use of references and functions

Functiontest (& $ a) {$ a = $ a + 100 ;}$ B = 1; test ($ B ); // Here $ B is actually the memory address of the variable content of $ B, by changing the value of $ a in the function, you can change the value of $ B by echo $ B; // output 101 // note that test (1) here ); the cause is as follows: the scalar cannot be passed by reference.

Global range of variables

(1) the current program (web program, or other) has only one portal (you access two programs with url/test1.php and url/test2.php ). (2) currently, all web programs can only access the index. php file, and then index. php will schedule resources and return the corresponding page! If the framework is used, the index. php will first load the framework (resources, class libraries, and function libraries), and then the framework will schedule resources. (3) ci is a url that corresponds to the method in a class. every url request is a method that executes a class (controller) on the server side. How to write this method is part of the program we want to implement. In other words, the framework puts the scheduling resource rules into your hands. (4) If you want to use some defined functions or class libraries, or variables and constants in the framework, you can go to index. before php introduces the framework, introduce your class library/function library/constant variable configuration file. Then, you can use (except variables) any part of the program ). Note: Your name cannot be the same as that of a php function or a function in the framework. Otherwise, it will be overwritten. (5) global variables cannot be directly accessed within the function. they can be accessed only after being declared by global. Global variables can be freely accessed outside the function.

Copyright disclaimer: knowledge is used by the people! You are welcome to repost this article. For more information, see the link at the beginning!

The above section introduces php: variables, types, scopes, and other related content. I hope to help anyone who is interested in PHP tutorials.

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.