PHP: About variables, types, scopes

Source: Internet
Author: User
Judging the existence of variables

isset($var);//只判断是否有这个变量,变量存在就是 TURE;

The judging variable is empty

!empty($var);//“”,0,“0”,NULL,FALSE$var; 和没有任何属性的对象在用 empty判断时,都是空的,返回TURE;

To determine if False

if(0false){echo"true";}    //trueif(0.0false){echo"true";}  //trueif(arrayfalse){echo"true";}  //trueif("0"false){echo"true";}  //trueif(''false){echo"true";}   //trueif(NULLfalse){echo"true"//true

Static variables: Variables that reside in memory

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

Constants: Read-only variables

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

mutable variables: PHP is the language of 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!

Splitting a assoc array into a variable

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();

A reference to a variable

$a="ABC"$b =&$aecho$a;//这里输出:ABC echo$b;//这里输出:ABC $b="EFG"echo$a;//这里$a的值变为EFG 所以输出EFG echo$b;//这里输出EFG 

References and functions are used together

functiontest(&$a) {$a=$a+100$b=1; test($b);//这里$b传递给函数的其实是$b的变量内容所处的内存地址,通过在函数里改变$a的值 就可以改变$b的值了 echo$b;//输出101 //要注意的是,在这里test(1);的话就会出错,原因自己去想:标量不能按引用传递

Global scope of a variable

(1)现在的程序(web程序,或者其他)都是只有一个入口的(你以 url/test1.php 和 url/test2.php 去访问的其实是两个程序)。(2)现在的web程序都是只能访问 index.php这个文件,然后index.php去调度资源,返回相应的页面!如果使用了框架的话,index.php 里面会先加载这个框架(资源,类库,函数库),然后由这个框架来调度资源。(3)ci就是一个url对应一个类里面的方法,每一次url请求,在服务器端都是执行一个类(控制器)的一个方法而已。而这个方法要如何写,就是我们要实现的程序部分了。换句话说,框架又将调度资源的规则交到了你的手里。(4)如果想在框架里面使用一些自己定义的函数或者类库,或者变量和常量,可以在index.php 引入框架之前,将你的类库/函数库/常量变量配置文件引入。然后在程序的任意处都可以使用了(除变量)。注意,你的命名不能和php函数以及框架里面的函数重名,否则会被覆盖。(5)全局变量在函数内部不可以直接访问,要用global 声明一下才可以访问。全局变量在函数外部是可以随意访问的。

Copyright Notice: Knowledge is taken to the people, to the people! Welcome reprint, Reprint please attach this article link, not regularly updated articles!

The above introduces PHP: about variables, types, scopes, including aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.