Home > Backend Development > PHP Tutorial > php: About variables, types, scope

php: About variables, types, scope

WBOY
Release: 2016-07-30 13:30:00
Original
886 people have browsed it

Judge whether the variable exists

<code><span>isset</span>(<span>$var</span>);<span>//只判断是否有这个变量,变量存在就是 TURE;</span></code>
Copy after login

Judge whether the variable is empty

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

Judge whether it is false

<code><span>if</span>(<span>0</span> == <span>false</span>){<span>echo</span><span>"true"</span>;}    <span>//true</span><span>if</span>(<span>0.0</span> == <span>false</span>){<span>echo</span><span>"true"</span>;}  <span>//true</span><span>if</span>(<span>array</span>() == <span>false</span>){<span>echo</span><span>"true"</span>;}  <span>//true</span><span>if</span>(<span>"0"</span> == <span>false</span>){<span>echo</span><span>"true"</span>;}  <span>//true</span><span>if</span>(<span>''</span> == <span>false</span>){<span>echo</span><span>"true"</span>;}   <span>//true</span><span>if</span>(<span>NULL</span> == <span>false</span>){<span>echo</span><span>"true"</span>;} <span>//true</span></code>
Copy after login

Static variables: memory-resident variables

<code><span><span>function</span><span>a</span><span>()</span>{</span><span>static</span><span>$a</span> = <span>1</span>;
    <span>echo</span><span>$a</span>;
    <span>$a</span>++;
}
a();<span>//1</span>
a();<span>//2</span></code>
Copy after login

Constant: read-only variables

<code><span><span>define</span></span>(<span>"TEST"</span>,<span>'codekissyoung'</span>);
echo TEST;<span>//codekissyoung</span></code>
Copy after login

Variable variables: PHP is a dynamic real-time analysis The language

<code><span>$a</span> = <span>"test"</span>;
<span>$test</span> = <span>"i am the test"</span>;
<span><span>function</span><span>test</span>(){</span><span>echo</span><span>"i am function test!"</span>;}   
<span>echo</span><span>$a</span>; <span>//test</span><span>echo</span><span>$$a</span>; <span>// i am the test</span><span>$a</span>(); <span>//i am function test!</span></code>
Copy after login

disassembles the assoc array into variables

<code><span>foreach</span> (<span>$_POST</span><span>as</span><span>$key</span> => <span>$value</span>) {
            <span>$$key</span> = <span>$value</span>;
        }</code>
Copy after login

dynamic new objects and call functions

<code><span>//example.com?class=person&func=run</span><span>$class</span>=<span>$_GET</span>[<span>'class'</span>];
<span>$func</span>=<span>$_GET</span>[<span>'func'</span>];
<span>$obj</span>=<span>new</span><span>$class</span>();
<span>$obj</span>-><span>$func</span>();</code>
Copy after login

References to variables

<code><span>$a</span>=<span>"ABC"</span>; 
<span>$b</span> =&<span>$a</span>; 
<span>echo</span><span>$a</span>;<span>//这里输出:ABC </span><span>echo</span><span>$b</span>;<span>//这里输出:ABC </span><span>$b</span>=<span>"EFG"</span>; 
<span>echo</span><span>$a</span>;<span>//这里$a的值变为EFG 所以输出EFG </span><span>echo</span><span>$b</span>;<span>//这里输出EFG </span></code>
Copy after login

Use references and functions in combination

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

Global scope of variables

<code>
(<span>1</span>)现在的程序(web程序,或者其他)都是只有一个入口的(你以 url/test1<span>.php</span> 和 url/test2<span>.php</span> 去访问的其实是两个程序)。

(<span>2</span>)现在的web程序都是只能访问 index<span>.php</span>这个文件,然后index<span>.php</span>去调度资源,返回相应的页面!如果使用了框架的话,index<span>.php</span> 里面会先加载这个框架(资源,类库,函数库),然后由这个框架来调度资源。

(<span>3</span>)ci就是一个url对应一个类里面的方法,每一次url请求,在服务器端都是执行一个类(控制器)的一个方法而已。而这个方法要如何写,就是我们要实现的程序部分了。换句话说,框架又将调度资源的规则交到了你的手里。

(<span>4</span>)如果想在框架里面使用一些自己定义的函数或者类库,或者变量和常量,可以在index<span>.php</span> 引入框架之前,将你的类库/函数库/常量变量配置文件引入。然后在程序的任意处都可以使用了(除变量)。注意,你的命名不能和php函数以及框架里面的函数重名,否则会被覆盖。

(<span>5</span>)全局变量在函数内部不可以直接访问,要用global 声明一下才可以访问。全局变量在函数外部是可以随意访问的。
</code>
Copy after login

Copyright statement: Knowledge comes from the people and is used by the people! Reprinting is welcome. Please attach a link to this article at the beginning. The article will be updated from time to time!

The above introduces PHP: about variables, types, scopes, including aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template