php 常量、變量用法詳細介紹
Jun 13, 2016 am 10:14 AM以前很少這么詳細的給大家介紹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)境相關聯(liá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");
?>
名稱 | 說明 |
---|---|
__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 新增) |

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

User voice input is captured and sent to the PHP backend through the MediaRecorder API of the front-end JavaScript; 2. PHP saves the audio as a temporary file and calls STTAPI (such as Google or Baidu voice recognition) to convert it into text; 3. PHP sends the text to an AI service (such as OpenAIGPT) to obtain intelligent reply; 4. PHP then calls TTSAPI (such as Baidu or Google voice synthesis) to convert the reply to a voice file; 5. PHP streams the voice file back to the front-end to play, completing interaction. The entire process is dominated by PHP to ensure seamless connection between all links.

The core method of building social sharing functions in PHP is to dynamically generate sharing links that meet the requirements of each platform. 1. First get the current page or specified URL and article information; 2. Use urlencode to encode the parameters; 3. Splice and generate sharing links according to the protocols of each platform; 4. Display links on the front end for users to click and share; 5. Dynamically generate OG tags on the page to optimize sharing content display; 6. Be sure to escape user input to prevent XSS attacks. This method does not require complex authentication, has low maintenance costs, and is suitable for most content sharing needs.

To realize text error correction and syntax optimization with AI, you need to follow the following steps: 1. Select a suitable AI model or API, such as Baidu, Tencent API or open source NLP library; 2. Call the API through PHP's curl or Guzzle and process the return results; 3. Display error correction information in the application and allow users to choose whether to adopt it; 4. Use php-l and PHP_CodeSniffer for syntax detection and code optimization; 5. Continuously collect feedback and update the model or rules to improve the effect. When choosing AIAPI, focus on evaluating accuracy, response speed, price and support for PHP. Code optimization should follow PSR specifications, use cache reasonably, avoid circular queries, review code regularly, and use X

1. Maximizing the commercial value of the comment system requires combining native advertising precise delivery, user paid value-added services (such as uploading pictures, top-up comments), influence incentive mechanism based on comment quality, and compliance anonymous data insight monetization; 2. The audit strategy should adopt a combination of pre-audit dynamic keyword filtering and user reporting mechanisms, supplemented by comment quality rating to achieve content hierarchical exposure; 3. Anti-brushing requires the construction of multi-layer defense: reCAPTCHAv3 sensorless verification, Honeypot honeypot field recognition robot, IP and timestamp frequency limit prevents watering, and content pattern recognition marks suspicious comments, and continuously iterate to deal with attacks.

PHP does not directly perform AI image processing, but integrates through APIs, because it is good at web development rather than computing-intensive tasks. API integration can achieve professional division of labor, reduce costs, and improve efficiency; 2. Integrating key technologies include using Guzzle or cURL to send HTTP requests, JSON data encoding and decoding, API key security authentication, asynchronous queue processing time-consuming tasks, robust error handling and retry mechanism, image storage and display; 3. Common challenges include API cost out of control, uncontrollable generation results, poor user experience, security risks and difficult data management. The response strategies are setting user quotas and caches, providing propt guidance and multi-picture selection, asynchronous notifications and progress prompts, key environment variable storage and content audit, and cloud storage.

PHP ensures inventory deduction atomicity through database transactions and FORUPDATE row locks to prevent high concurrent overselling; 2. Multi-platform inventory consistency depends on centralized management and event-driven synchronization, combining API/Webhook notifications and message queues to ensure reliable data transmission; 3. The alarm mechanism should set low inventory, zero/negative inventory, unsalable sales, replenishment cycles and abnormal fluctuations strategies in different scenarios, and select DingTalk, SMS or Email Responsible Persons according to the urgency, and the alarm information must be complete and clear to achieve business adaptation and rapid response.

PHPisstillrelevantinmodernenterpriseenvironments.1.ModernPHP(7.xand8.x)offersperformancegains,stricttyping,JITcompilation,andmodernsyntax,makingitsuitableforlarge-scaleapplications.2.PHPintegrateseffectivelyinhybridarchitectures,servingasanAPIgateway

The core role of Homebrew in the construction of Mac environment is to simplify software installation and management. 1. Homebrew automatically handles dependencies and encapsulates complex compilation and installation processes into simple commands; 2. Provides a unified software package ecosystem to ensure the standardization of software installation location and configuration; 3. Integrates service management functions, and can easily start and stop services through brewservices; 4. Convenient software upgrade and maintenance, and improves system security and functionality.
