国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊(cè) PHP learning log (2)-php variables

PHP learning log (2)-php variables

Aug 18, 2016 am 08:57 AM

Variables are containers used to store data. Similar to algebra, variables can be assigned a certain value (for example: $x=3) or other variables (for example: $x=$y+$z). The definition of variables mainly has the following rules:

  • Variables start with $, followed by the name of the variable;
  • The variable name consists of numbers, letters, and underscores, and the first character cannot be a number;
  • The variable name cannot contain spaces;
  • Variable names are case-sensitive.

? A variable in php is created when a value is assigned to it for the first time. If the variable is not assigned a value, an error will appear during output, as shown below:

<span style="color: #000000">php
</span><span style="color: #800080">$x</span><span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> <span style="color: #800080">$x</span><span style="color: #000000">;
</span>?>

At this time, the browser will display an error message:

PHP learning log (2)-php variables

This part is different from Python. If the variable is not assigned a value in Python, the compiler will directly prompt an error. We need to pay attention to it.

PHP learning log (2)-php variablesFigure, Python variable is not assigned an error and an error occurs???????????

PHP is a weakly typed language. When defining variables, we do not need to define the type of the variable. PHP will automatically convert the variable into the correct data type based on the value of the variable. As shown in the following example:

<span style="color: #000000">php
</span><span style="color: #800080">$x</span>=3<span style="color: #000000">;
</span><span style="color: #800080">$y</span>=3.0<span style="color: #000000">;
</span><span style="color: #800080">$str</span>="hello"<span style="color: #000000">;
</span><span style="color: #800080">$bool</span>=<span style="color: #0000ff">false</span><span style="color: #000000">;
</span><span style="color: #800080">$arr</span>=<span style="color: #0000ff">array</span>(PHP learning log (2)-php variables,2,3<span style="color: #000000">);
</span><span style="color: #800080">$_null</span>=<span style="color: #0000ff">NULL</span><span style="color: #000000">;

</span><span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$x</span>),"<br>";     <span style="color: #008000">//</span><span style="color: #008000">輸出類型為integ</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$y</span>),"<br>";     <span style="color: #008000">//</span><span style="color: #008000">輸出類型為double</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$str</span>),"<br>";   <span style="color: #008000">//</span><span style="color: #008000">輸出類型為string</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$bool</span>),"<br>";  <span style="color: #008000">//</span><span style="color: #008000">輸出類型為boolean</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$arr</span>),"<br>";   <span style="color: #008000">//</span><span style="color: #008000">輸出類型為array</span>
<span style="color: #0000ff">echo</span> <span style="color: #008080">gettype</span>(<span style="color: #800080">$_null</span>),"<br>";  <span style="color: #008000">//</span><span style="color: #008000">輸出類型為NULL</span>
?>
The result is:

PHP learning log (2)-php variables

Next we will discuss the four variable scopes of PHP. The variable scope defines the scope of the variable. PHP mainly has the following four variable scopes:

    local
  • global
  • static
  • parameter
(PHP learning log (2)-php variables) Local and global scope

Variables defined outside a function have a global scope. Except for functions, the global scope can be accessed by any part of the script. To access global variables in a function, you need to add

before the variable in the function.

global keyword.

Example:

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">局部作用域與全局作用域</span><span style="color: #008000">*/</span>
<span style="color: #800080">$a</span>=5<span style="color: #000000">;

</span><span style="color: #0000ff">function</span><span style="color: #000000"> test()
{
    </span><span style="color: #800080">$b</span>=PHP learning log (2)-php variables0<span style="color: #000000">;
    </span><span style="color: #0000ff">echo</span> "測試函數(shù)內(nèi)變量<br>"<span style="color: #000000">;
    </span><span style="color: #0000ff">echo</span> "變量a的值為:<span style="color: #800080">$a</span> <br>";  <span style="color: #008000">//</span><span style="color: #008000">變量$a未在函數(shù)內(nèi)定義,在引用時(shí)出現(xiàn)錯(cuò)誤.</span>
    <span style="color: #0000ff">echo</span> "變量b的值為:<span style="color: #800080">$b</span><br>"<span style="color: #000000">;
}

test();

</span><span style="color: #0000ff">echo</span> "測試函數(shù)外變量<br>"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "變量a的值為:<span style="color: #800080">$a</span> <br>"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "變量b的值為:<span style="color: #800080">$b</span><br>";    <span style="color: #008000">//</span><span style="color: #008000">變量$a未在函數(shù)內(nèi)定義,在引用時(shí)出現(xiàn)警告.</span>
?>

結(jié)果如下:

PHP learning log (2)-php variables

圖、局部變量與全局變量

可見,在局部函數(shù)里面,是不能直接訪問全局變量的,如果要訪問全局變量,須在函數(shù)里面的變量前加上global關(guān)鍵字。同樣,在函數(shù)外也不能直接訪問函數(shù)里面的變量,函數(shù)執(zhí)行結(jié)束內(nèi)存會(huì)自動(dòng)回收,故我們無法訪問到。

(2)global關(guān)鍵字

????? global 關(guān)鍵字用于函數(shù)內(nèi)訪問全局變量,在函數(shù)內(nèi)調(diào)用函數(shù)外定義的全局變量,需要用global關(guān)鍵字。值得一提的是,這和參數(shù)的調(diào)用不同,參數(shù)的調(diào)用并不會(huì)改變?cè)撟兞吭趦?nèi)存中的值,而global調(diào)用是直接調(diào)用內(nèi)存中的該值,直接對(duì)它進(jìn)行操作,故會(huì)改變其值。

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">在函數(shù)內(nèi)調(diào)用函數(shù)外的變量,需要用到global關(guān)鍵字</span><span style="color: #008000">*/</span>
<span style="color: #008000">/*重要:</span><span style="color: #008000">這種調(diào)用會(huì)改變變量在內(nèi)存中的值</span><span style="color: #008000">*/</span>
<span style="color: #800080">$x</span>=5<span style="color: #000000">;
</span><span style="color: #800080">$y</span>=6<span style="color: #000000">;
</span><span style="color: #008000">/*</span><span style="color: #008000">
 * 以下這種方式是不被允許的,只有在函數(shù)內(nèi)調(diào)用函數(shù)外的變量才能使用global
golbal $z=7;
</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span><span style="color: #000000"> myTest()
{
    </span><span style="color: #0000ff">global</span> <span style="color: #800080">$x</span>,<span style="color: #800080">$y</span><span style="color: #000000">;
    </span><span style="color: #800080">$y</span>=<span style="color: #800080">$x</span>+<span style="color: #800080">$y</span><span style="color: #000000">;
}

myTest();
</span><span style="color: #0000ff">echo</span> "y=<span style="color: #800080">$y</span>"  <span style="color: #008000">//</span><span style="color: #008000">輸出y=PHP learning log (2)-php variablesPHP learning log (2)-php variables</span>
?>

?

*注意:超級(jí)全局變量 $GLOBALS[index]

????? php將所有全局變量存儲(chǔ)在一個(gè)名為:$GLOBALS[index]的數(shù)組中,這個(gè)數(shù)組可以在函數(shù)內(nèi)訪問,也可以用來直接更新全局變量(只能在函數(shù)內(nèi)進(jìn)行)。

實(shí)例:

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000"> GLOBALS[index]的用法講解 </span><span style="color: #008000">*/</span>
<span style="color: #800080">$x</span>=5<span style="color: #000000">;
</span><span style="color: #800080">$y</span>=6<span style="color: #000000">;

</span><span style="color: #0000ff">function</span><span style="color: #000000"> myTest_PHP learning log (2)-php variables()
{
    </span><span style="color: #800080">$GLOBALS</span>['y']=<span style="color: #800080">$GLOBALS</span>['x']+<span style="color: #800080">$GLOBALS</span>['y'<span style="color: #000000">];
    </span><span style="color: #008000">/*</span><span style="color: #008000">index不用寫$字符,否則報(bào)錯(cuò):$x,$y undefined
    $GLOBALS['$y']=$GLOBALS['$x']+$GLOBALS['$y'];
    </span><span style="color: #008000">*/</span><span style="color: #000000">
}

</span><span style="color: #0000ff">function</span><span style="color: #000000"> myTest_2()
{
    </span><span style="color: #800080">$GLOBALS</span>['x']=PHP learning log (2)-php variables5<span style="color: #000000">;
}

</span><span style="color: #0000ff">echo</span> "執(zhí)行myTest_PHP learning log (2)-php variables:<br>"<span style="color: #000000">;
myTest_PHP learning log (2)-php variables();
</span><span style="color: #0000ff">echo</span> "y=<span style="color: #800080">$y</span>","<br>"<span style="color: #000000">;
</span><span style="color: #0000ff">echo</span> "x=<span style="color: #800080">$x</span>","<br>"<span style="color: #000000">;
</span><span style="color: #800080">$GLOBALS</span>['$x']=PHP learning log (2)-php variables2;     <span style="color: #008000">//</span><span style="color: #008000">未將GLOBALS[index]放在函數(shù)里面,無效</span>
<span style="color: #0000ff">echo</span> "x=<span style="color: #800080">$x</span>","<br>"<span style="color: #000000">;

</span><span style="color: #0000ff">echo</span> "執(zhí)行myTest_2:<br>"<span style="color: #000000">;
myTest_2();
</span><span style="color: #0000ff">echo</span> "x=<span style="color: #800080">$x</span>","<br>"<span style="color: #000000">;
</span>?>

結(jié)果為:

GLOBALS[index] introduce

圖、GLOBALS[index]的用法示例

(3)static關(guān)鍵字

????? 在一個(gè)函數(shù)執(zhí)行完成之后,它的變量通常都會(huì)刪除,有時(shí)我們希望函數(shù)中的某個(gè)變量保留,這時(shí)我們可以在申明變量時(shí)使用static關(guān)鍵字:

實(shí)例:

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">靜態(tài)變量static的用法</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span><span style="color: #000000"> myTest()
{
    </span><span style="color: #0000ff">static</span> <span style="color: #800080">$x</span>=0,<span style="color: #800080">$y</span>=PHP learning log (2)-php variables<span style="color: #000000">;
    </span><span style="color: #0000ff">echo</span> <span style="color: #800080">$x</span><span style="color: #000000">;
    </span><span style="color: #800080">$x</span>++<span style="color: #000000">;
}

myTest(); </span><span style="color: #008000">//</span><span style="color: #008000">輸出為:0</span>
myTest(); <span style="color: #008000">//</span><span style="color: #008000">輸出為:PHP learning log (2)-php variables</span>
myTest(); <span style="color: #008000">//</span><span style="color: #008000">輸出為:2</span>

<span style="color: #008000">/*</span><span style="color: #008000">函數(shù)里面的靜態(tài)變量不能直接被訪問</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">echo</span> <span style="color: #800080">$y</span>;  <span style="color: #008000">//</span><span style="color: #008000">輸出為:Notice: Undefined variable: y</span>
?>

(4)參數(shù)作用域

????? 參數(shù)(parameter)的作用是將值傳遞給函數(shù)的局部變量。

實(shí)例:

<span style="color: #000000">php
</span><span style="color: #008000">/*</span><span style="color: #008000">參數(shù)(parameter)傳遞</span><span style="color: #008000">*/</span>
<span style="color: #0000ff">function</span> myTest(<span style="color: #800080">$x</span><span style="color: #000000">)
{
    </span><span style="color: #0000ff">echo</span> "傳遞的值為:<span style="color: #800080">$x</span>.<br>"<span style="color: #000000">;
}

myTest(</span>5);  <span style="color: #008000">//</span><span style="color: #008000">結(jié)果為:傳遞的值為5.</span>
myTest("string") <span style="color: #008000">//</span><span style="color: #008000">結(jié)果為:傳遞的值為string.</span>
?>

?

?

==php新手,有不對(duì)的地方希望各位博友提醒,萬分感謝==

Technorati 標(biāo)簽: php,變量,local,global,static,參數(shù)傳遞,GLOBALS[index]
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)