Variables in PHP

Source: Internet
Author: User
Overview

Variables in PHP are represented by a dollar sign followed by a variable name.

Variable names are case-sensitive.

A valid variable name begins with a letter or underscore, followed by any number of letters, numbers, or underscores.

$this is a special variable that cannot be assigned to a value.

Variables are always assigned values by default. If you need to reference an assignment, use the&
Only a variable with a name can reference an assignment

$foo = 25;$bar = &$foo;      // 合法的赋值$bar = &(24 * 7);  // 非法; 引用没有名字的表达式

Although it is not necessary to initialize variables in PHP, it is a good practice to initialize variables. Uninitialized variables have default values of their type-the default value for a variable of type Boolean is FALSE, the default value for shaping and floating-point variables is zero, the default value for string variables (for example, in Echo) is an empty string, and the default value of an array variable is an empty array.

Pre-defined variables

超全局变量 — 超全局变量是在全部作用域中始终可用的内置变量    $GLOBALS — 引用全局作用域中可用的全部变量    $_SERVER — 服务器和执行环境信息    $_GET — HTTP GET 变量    $_POST — HTTP POST 变量    $_FILES — HTTP 文件上传变量    $_REQUEST — HTTP Request 变量    $_SESSION — Session 变量    $_ENV — 环境变量    $_COOKIE — HTTP Cookies$php_errormsg — 前一个错误信息$HTTP_RAW_POST_DATA — 原生POST数据$http_response_header — HTTP 响应头$argc — 传递给脚本的参数数目$argv — 传递给脚本的参数数组

Many of the predefined variables in PHP are "hyper-global", which means they are available in all scopes of a script. There is no need to perform a global $variable in a function or method; You can access them.

Variable scope

Global variables are automatically invalidated in functions


  
   

When a global variable is used in a function in PHP, it must be declared asglobal


  
   

A second way to access variables globally is to use special PHP custom $GLOBALS arrays


  
   

static variables

Another important feature of the variable range is the static variable (variable). A static variable exists only in the local function domain, but its value is not lost when the program executes away from the scope.


  
   

Variable variable

Variable names for a variable can be set and used dynamically

$Bar = "a";$Foo = "Bar";$World = "Foo";$Hello = "World";$a = "Hello";$a; //Returns Hello$$a; //Returns World$$$a; //Returns Foo$$$$a; //Returns Bar$$$$$a; //Returns a

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above introduces the variables in PHP, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.