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

Home php教程 php手冊 php 常量、變量用法詳細介紹

php 常量、變量用法詳細介紹

Jun 13, 2016 am 10:14 AM
php introduce variable constant usage detailed

以前很少這么詳細的給大家介紹php中的變量、常量以及魔術常量的用法以及參考表,這文章對于初學者有不小的幫助有需要了解的朋友可以參考一下。

變量:

變量用于存儲值,比如數字、文本字符串或數組。

一旦設置了某個變量,我們就可以在腳本中重復地使用它。

PHP 中的所有變量都是以 $ 符號開始的。

在 PHP 中設置變量的正確方法是:

?代碼如下 復制代碼
$var_name = value;

PHP 的入門者往往會忘記在變量的前面的 $ 符號。如果那樣做的話,變量將是無效的。

讓我們試著創(chuàng)建一個存有字符串的變量,和一個存有數值的變量:

?代碼如下 復制代碼
$txt = "Hello World!";
$number = 16;
?>

?

1.如何定義變量,它和C# 等語言有什么不同呢?
?? PHP 中的變量用一個美元符號后面跟變量名來表示。變量名是區(qū)分大小寫的。例如:

?代碼如下 復制代碼
?$var='Jim';
? $VAR='Kimi;
? echo "$var,$VAR";//輸出“Jim,Kimi"

??>你可能還關心變量的命名,其實和大多數語言一樣。
2. 變量區(qū)分大小寫嗎?
?? 如 1里說的,區(qū)分大小寫。
? 注意,需要說明的一點是自PHP4以來,引入了引用賦值的概念,其實和多數語言的引用類似,不過我覺得最類似的是C/C++.因為它也用到了"&"符號。例如:?

?代碼如下 復制代碼
1 2 $foo = 'Bob';????????????? // 賦值'Bob'給foo
3 $bar = &$foo;????????????? // 通過$bar引用.注意&符號
4 $bar = "My name is $bar";? // 修改 $bar
5 echo $bar;
6 echo $foo;??????????????? // $foo 也修改了.
7 ?>

和其他語言一樣,只能對有變量名的變量才可以引用。


好了現在大家對變量應該有一個大概的了解了,現在我們看看變量的間接引用和字符串連接。

①變量的間接引用: 先看個例子吧

?代碼如下 復制代碼

?$a = "b";
?$$a = "123";
?echo $b;
?>

?

上面的輸出結果是123

我們可以看到在第二行代碼中多了一個$,并通過指定的名稱訪問變量,指定的名字存儲在$a("b")中,并把這個變量$b的值更改為123。因此,這樣的$b的變量被創(chuàng)建和賦值。

通過在變量的前面增加附加的$標記,你可以任意增加引用的次數。

?②字符串連接: 先看個例子吧

?代碼如下 復制代碼

$a = "PHP 4" ;
$b = "功能強大" ;
echo $a.$b;
?>

?

需要注意的是 在PHP 4.2.0 以及后續(xù)版本中,PHP 指令 register_globals 的默認值為 off。這是 PHP 的一個主要變化。讓 register_globals 的值為 off 將影響到預定義變量集在全局范圍內的有效性。例如,為了得到 DOCUMENT_ROOT 的值,將必須使用 $_SERVER['DOCUMENT_ROOT'] 代替 $DOCUMENT_ROOT,又如,使用 $_GET['id'] 來代替 $id 從 URL http://www.example.com/test.php?id=3 中獲取 id 值,亦或使用 $_ENV['HOME'] 來代替 $HOME 獲取環(huán)境變量 HOME 的值

我們看到代碼的第三行,英文的(句)號,它可以將字符串連接起來,變成合并的新字符串。

?

超全局變量 描述
$GLOBALS 包含一個引用指向每個當前腳本的全局范圍內有效的變量。該數組的鍵名為全局變量的名稱。從 PHP 3 開始存在 $GLOBALS 數組。
$_SERVER 變量由 web 服務器設定或者直接與當前腳本的執(zhí)行環(huán)境相關聯。類似于舊數組 $HTTP_SERVER_VARS 數組(依然有效,但反對使用)。
$_GET 經由 URL 請求提交至腳本的變量。類似于舊數組 $HTTP_GET_VARS 數組(依然有效,但反對使用)。
$_POST 經由 HTTP POST 方法提交至腳本的變量。類似于舊數組 $HTTP_POST_VARS 數組(依然有效,但反對使用)。
$_COOKIE 經由 HTTP Cookies 方法提交至腳本的變量。類似于舊數組 $HTTP_COOKIE_VARS 數組(依然有效,但反對使用)。
$_FILES 經由 HTTP POST 文件上傳而提交至腳本的變量。類似于舊數組 $HTTP_POST_FILES 數組(依然有效,但反對使用)
$_ENV 執(zhí)行環(huán)境提交至腳本的變量。類似于舊數組 $HTTP_ENV_VARS 數組(依然有效,但反對使用)。
$_REQUEST ?經由 GET,POST 和 COOKIE 機制提交至腳本的變量,因此該數組并不值得信任。所有包含在該數組中的變量的存在與否以及變量的順序均按照 php.ini 中的 variables_order 配置指示來定義。此數組在 PHP 4.1.0 之前沒有直接對應的版本。參見 import_request_variables()
$_SESSION 當前注冊給腳本會話的變量。類似于舊數組 $HTTP_SESSION_VARS 數組(依然有效,但反對使用)
?

常量:

常量是一個簡單值的標識符(名字)。如同其名稱所暗示的,在腳本執(zhí)行期間該值不能改變(除了所謂的魔術常量,它們其實不是常量)。常量默認為大小寫敏感。通常常量標識符總是大寫的。

常量名和其它任何 PHP 標簽遵循同樣的命名規(guī)則。合法的常量名以字母或下劃線開始,后面跟著任何字母,數字或下劃線。用正則表達式是這樣表達的:[a-zA-Z_x7f-xff][a-zA-Z0-9_x7f-xff]*

?

①是在程序執(zhí)行期間無法改變的數據,常量的作用域是全局的。

②常量的命名與與變量相似,只是不帶美元符號“$”。一個有效的常量名由字母或者下劃線開頭,后面跟報上任意數量的字母、數字或者下劃線。

③一般在PHP中常量都為大寫字母而且又分為系統(tǒng)常量和自定義常量。

?

系統(tǒng)常量我們就大概說了 ,這個在后面的知識會介紹到。

1、__FILE__??? 默認常量,是指PHP程序文件名及路徑;
2、__LINE__??? 默認常量,是指PHP程序的行數;
3、__CLASS__??? 類的名稱;

自定義常量:通過define()函數來定義一個常量的,

其語法格式為:bool define ( string $name, mixed $value [, bool case_$insensitive] )

name:指定常量的名稱。
value:指定常量的值。
insensitive:指定常量名稱是否區(qū)分大小寫。如果設置為true則不區(qū)分大小寫;如果設置為false則區(qū)分大小寫。如果沒有設置該參數,則取默認值false。

// 合法的常量名
define("FOO",???? "something");
define("FOO2",??? "something else");
define("FOO_BAR", "something more");

// 非法的常量名
define("2FOO",??? "something");

// 下面的定義是合法的,但應該避免這樣做:(自定義常量不要以__開頭)
// 也許將來有一天PHP會定義一個__FOO__的魔術常量
// 這樣就會與你的代碼相沖突
define("__FOO__", "something");

?>

幾個 PHP 的“魔術常量”
名稱 說明
__LINE__ 文件中的當前行號。
__FILE__ 文件的完整路徑和文件名。如果用在被包含文件中,則返回被包含的文件名。自 PHP 4.0.2 起,__FILE__ 總是包含一個絕對路徑(如果是符號連接,則是解析后的絕對路徑),而在此之前的版本有時會包含一個相對路徑。
__DIR__ 文件所在的目錄。如果用在被包括文件中,則返回被包括的文件所在的目錄。它等價于 dirname(__FILE__)。除非是根目錄,否則目錄中名不包括末尾的斜杠。(PHP 5.3.0中新增) =
__FUNCTION__ 函數名稱(PHP 4.3.0 新加)。自 PHP 5 起本常量返回該函數被定義時的名字(區(qū)分大小寫)。在 PHP 4 中該值總是小寫字母的。
__CLASS__ 類的名稱(PHP 4.3.0 新加)。自 PHP 5 起本常量返回該類被定義時的名字(區(qū)分大小寫)。在 PHP 4 中該值總是小寫字母的。
__METHOD__ 類的方法名(PHP 5.0.0 新加)。返回該方法被定義時的名字(區(qū)分大小寫)。
__NAMESPACE__ 當前命名空間的名稱(大小寫敏感)。這個常量是在編譯時定義的(PHP 5.3.0 新增)
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)

How to get the current session ID in PHP? How to get the current session ID in PHP? Jul 13, 2025 am 03:02 AM

The method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

PHP get substring from a string PHP get substring from a string Jul 13, 2025 am 02:59 AM

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

How do you perform unit testing for php code? How do you perform unit testing for php code? Jul 13, 2025 am 02:54 AM

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

How to split a string into an array in PHP How to split a string into an array in PHP Jul 13, 2025 am 02:59 AM

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript Data Types: Primitive vs Reference JavaScript Data Types: Primitive vs Reference Jul 13, 2025 am 02:43 AM

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

Using std::chrono in C Using std::chrono in C Jul 15, 2025 am 01:30 AM

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

How does PHP handle Environment Variables? How does PHP handle Environment Variables? Jul 14, 2025 am 03:01 AM

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach

Why We Comment: A PHP Guide Why We Comment: A PHP Guide Jul 15, 2025 am 02:48 AM

PHPhasthreecommentstyles://,#forsingle-lineand/.../formulti-line.Usecommentstoexplainwhycodeexists,notwhatitdoes.MarkTODO/FIXMEitemsanddisablecodetemporarilyduringdebugging.Avoidover-commentingsimplelogic.Writeconcise,grammaticallycorrectcommentsandu

See all articles