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

Heim php教程 php手冊 PHP-Lernprotokoll (2) – PHP-Variablen

PHP-Lernprotokoll (2) – PHP-Variablen

Aug 18, 2016 am 08:57 AM

Variablen sind Container, die zum Speichern von Daten verwendet werden. ?hnlich wie in der Algebra k?nnen Variablen ein bestimmter Wert (zum Beispiel: $x=3) oder andere Variablen (zum Beispiel: $x=$y $z) zugewiesen werden. Für die Definition von Variablen gelten haupts?chlich die folgenden Regeln:

  • Variablen beginnen mit $, gefolgt vom Namen der Variablen.
  • Der Variablenname besteht aus Zahlen, Buchstaben und Unterstrichen Das Zeichen darf keine Zahl sein.
  • Variablennamen dürfen keine Leerzeichen enthalten.
  • Bei Variablennamen muss die Gro?-/Kleinschreibung beachtet werden.

Variablen in PHP werden erstellt, wenn ihr zum ersten Mal ein Wert zugewiesen wird. Wenn der Variablen kein Wert zugewiesen wird, wird bei der Ausgabe ein Fehler angezeigt, wie gezeigt unten:

<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>?>

Zu diesem Zeitpunkt zeigt der Browser eine Fehlermeldung an:

PHP-Lernprotokoll (2) – PHP-Variablen

Dieser Teil unterscheidet sich von Python. Wenn der Variablen in Python kein Wert zugewiesen wird, meldet der Compiler direkt einen Fehler.

PHP-Lernprotokoll (2) – PHP-VariablenBild, Fehler ?Python-Variable nicht zugewiesener Wert“ aufgetreten?????

PHP ist eine schwach typisierte Sprache. Beim Definieren von Variablen müssen wir den Typ der Variablen nicht definieren. PHP konvertiert die Variable automatisch in den richtigen Datentyp. Wie im folgenden Beispiel gezeigt:

<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-Lernprotokoll (2) – PHP-Variablen,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>
?>

Das Ergebnis ist:

PHP-Lernprotokoll (2) – PHP-Variablen

Als n?chstes werden wir die vier Variablenbereiche von PHP besprechen. Der Variablenbereich definiert den Gültigkeitsbereich der Variablen. PHP hat haupts?chlich die folgenden vier Variablenbereiche:

  • lokal
  • global
  • statisch
  • Parameter(Parameter)

(PHP-Lernprotokoll (2) – PHP-Variablen) Lokaler und globaler Geltungsbereich

Au?erhalb einer Funktion definierte Variablen haben einen globalen Gültigkeitsbereich. Mit Ausnahme von Funktionen kann auf den globalen Gültigkeitsbereich von jedem Teil des Skripts zugegriffen werden. Um auf globale Variablen in einer Funktion zuzugreifen, müssen Sie

globales Schlüsselwort.

Beispiel:

<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-Lernprotokoll (2) – PHP-Variablen0<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)定義,在引用時出現(xiàn)錯誤.</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)定義,在引用時出現(xiàn)警告.</span>
?>

結(jié)果如下:

PHP-Lernprotokoll (2) – PHP-Variablen

圖、局部變量與全局變量

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

(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)用并不會改變該變量在內(nèi)存中的值,而global調(diào)用是直接調(diào)用內(nèi)存中的該值,直接對它進(jìn)行操作,故會改變其值。

<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)用會改變變量在內(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-Lernprotokoll (2) – PHP-VariablenPHP-Lernprotokoll (2) – PHP-Variablen</span>
?>

?

*注意:超級全局變量 $GLOBALS[index]

????? php將所有全局變量存儲在一個名為:$GLOBALS[index]的數(shù)組中,這個數(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-Lernprotokoll (2) – PHP-Variablen()
{
    </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不用寫$字符,否則報錯:$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-Lernprotokoll (2) – PHP-Variablen5<span style="color: #000000">;
}

</span><span style="color: #0000ff">echo</span> "執(zhí)行myTest_PHP-Lernprotokoll (2) – PHP-Variablen:<br>"<span style="color: #000000">;
myTest_PHP-Lernprotokoll (2) – PHP-Variablen();
</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-Lernprotokoll (2) – PHP-Variablen2;     <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)鍵字

????? 在一個函數(shù)執(zhí)行完成之后,它的變量通常都會刪除,有時我們希望函數(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-Lernprotokoll (2) – PHP-Variablen<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-Lernprotokoll (2) – PHP-Variablen</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新手,有不對的地方希望各位博友提醒,萬分感謝==

Technorati 標(biāo)簽: php,變量,local,global,static,參數(shù)傳遞,GLOBALS[index]
Erkl?rung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn

Hei?e KI -Werkzeuge

Undress AI Tool

Undress AI Tool

Ausziehbilder kostenlos

Undresser.AI Undress

Undresser.AI Undress

KI-gestützte App zum Erstellen realistischer Aktfotos

AI Clothes Remover

AI Clothes Remover

Online-KI-Tool zum Entfernen von Kleidung aus Fotos.

Clothoff.io

Clothoff.io

KI-Kleiderentferner

Video Face Swap

Video Face Swap

Tauschen Sie Gesichter in jedem Video mühelos mit unserem v?llig kostenlosen KI-Gesichtstausch-Tool aus!

Hei?e Werkzeuge

Notepad++7.3.1

Notepad++7.3.1

Einfach zu bedienender und kostenloser Code-Editor

SublimeText3 chinesische Version

SublimeText3 chinesische Version

Chinesische Version, sehr einfach zu bedienen

Senden Sie Studio 13.0.1

Senden Sie Studio 13.0.1

Leistungsstarke integrierte PHP-Entwicklungsumgebung

Dreamweaver CS6

Dreamweaver CS6

Visuelle Webentwicklungstools

SublimeText3 Mac-Version

SublimeText3 Mac-Version

Codebearbeitungssoftware auf Gottesniveau (SublimeText3)