search
Homephp教程php手册php 变量定义方法

php 变量定义方法

Jun 13, 2016 pm 12:23 PM
definephpIncludevariabledefinitionconstantdatamethod

1.定义常量define("CONSTANT", "Hello world.");
常量只能包含标量数据(boolean,integer,float 和 string)。
调用常量时,只需要简单的用名称取得常量的值,而不能加“$”符号,如:echo CONSTANT;
注: 常量和(全局)变量在不同的名字空间中。这意味着例如 TRUE 和 $TRUE 是不同的。
2.普通变量$a = "hello";
3.可变变量(使用两个美元符号($))
$$a = "world";
两个变量都被定义了:
$a 的内容是“hello”并且 $hello 的内容是“world”。
因此,可以表述为:
echo "$a ${$a}";或者 echo "$a $hello";它们都会输出:hello world
要将可变变量用于数组,必须解决一个模棱两可的问题。这就是当写下 $$a[1] 时,解析器需要知道是想要 $a[1] 作为一个变量呢,还是想要 $$a 作为一个变量并取出该变量中索引为 [1] 的值。解决此问题的语法是,对第一种情况用 ${$a[1]},对第二种情况用 ${$a}[1]。
4.静态变量
在函数内部static $a = 0;
注意:声明中用表达式的结果对其赋值会导致解析错误如static $a =3+3;(error)
静态变量仅在局部函数域中存在(函数内部),函数执行完之后,变量值不会丢失,可用于递归调用
5.全局变量
在函数体内定义的global变量,函数体外可以使用,在函数体外定义的global变量不能在函数体内使用,在全局范围内访问变量可以用特殊的 PHP 自定义 $GLOBALS 数组:
如:$GLOBALS["b"] = $GLOBALS["a"] + $GLOBALS["b"];
在一个函数域内用 global 语句导入的一个真正的全局变量实际上是建立了一个到全局变量的引用
global $obj;
注:对于变量的 static 和 global 定义是以 应用 的方式实现的
6.给变量赋值:传地址赋值(简单引用):
$bar = &$foo; //加&符号到将要赋值的变量前
改动新的变量将影响到原始变量,这种赋值操作更加快速
注意:只有命名变量才可以传地址赋值
注意:如果
$bar = &$a;
$bar = &$foo;
改变$bar的值只能改变变量foo的值,而不改变a的值(引用改变了)
7.PHP 超全局变量$GLOBALS : 包含一个引用指向每个当前脚本的全局范围内有效的变量。该数组的键标为全局变量的 名称。从 PHP 3 开始存在 $GLOBALS 数组。
$_SERVER : 变量由 Web 服务器设定或者直接与当前脚本的执行环境相关联。类似于旧数组 $HTTP_SERVER_VARS 数组(依然有效,但反对使用)。
$_GET : 经由 HTTP GET 方法提交至脚本的变量。
$_POST : 经由 HTTP POST 方法提交至脚本的变量。
$_COOKIE : 经由 HTTP Cookies 方法提交至脚本的变量。
$_FILES : 经由 HTTP POST 文件上传而提交至脚本的变量。
文件上传表单中要有 enctype="multipart/form-data"
$_ENV : 执行环境提交至脚本的变量。
$_REQUEST :经由 GET,POST 和 COOKIE 机制提交至脚本的变量,因此该数组并不值得信任。所有包含在该数组中的变量的存在与否以及变量的顺序均按照 php.ini 中的 variables_order 配置指示来定义。该数组没有直接模拟 PHP 4.1.0 的早期版本。参见 import_request_variables()。
注意: 自 PHP 4.3.0 起,$_FILES 中的文件信息不再存在于 $_REQUEST 中。
$_SESSION :当前注册给脚本会话的变量。
如何禁用phpinfo():
php.ini中
disable_functions = phpinfo()
重启web服务器。
php中的常量
常量只能用define(常量名,常量值);
常量只能包含标量数据(boolean,integer,float 和 string)。
可以简单的通过指定其名字来取得常量的值,不要在常量前面加上 $ 符号。如果常量名是动态的,也可以用函数
constant() 来读取常量的值。用 get_defined_constants() 可以获得所有已定义的常量列表。
注: 常量和(全局)变量在不同的名字空间中。这意味着例如 TRUE 和 $TRUE 是不同的。
如果使用了一个未定义的常量,PHP 假定想要的是该常量本身的名字,如同用字符串调用它一样(CONSTANT 对应 "CONSTANT")。此时将发出一个 E_NOTICE 级的错误。参见手册中为什么 $w3sky[bar] 是错误的(除非事先用 define() 将 bar 定义为一个常量)。如果只想检查是否定义了某常量,用 defined() 函数。
常量和变量不同:
* 常量前面没有美元符号($);
* 常量只能用 define() 函数定义,而不能通过赋值语句;
* 常量可以不用理会变量范围的规则而在任何地方定义和访问;
* 常量一旦定义就不能被重新定义或者取消定义;
* 常量的值只能是标量。
定义常量
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."
echo Constant; // outputs "Constant" and issues a notice.
?>

Statement
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

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.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.