Front-End Learning PHP variables

Source: Internet
Author: User
Tags php language

xTable of Contents [1] variable definition [2] keyword [3] variable assignment [4] variable variable [5] variable function in front of the word

A variable is a container for temporarily storing values. These values can be numbers, text, or a much more complex permutation combination. Variables are at the heart of any programming language, and understanding them is the key to using PHP. The variables in PHP are described in more detail below

[note] Some of the variables in JavaScript

Variable definition

One of the features of PHP is that it does not require a variable to be declared before it is used, and the variable is created when it is assigned to a variable for the first Time.

The variable starts with the $ sign, followed by the name of the Variable. Variable names must start with a letter or underscore and are case sensitive

<? PHP $x=5; Echo $x ;//5 Echo $X ;//error ?>

[note] built-in structures and keywords, as well as user-defined class names and function names are case-insensitive, such as echo, while, function names, and so on can be arbitrarily sized

<? PHP   // Output 123    echo 1;     Echo 2;     ECho 3;? >

Key words

Some of PHP are system-defined, also known as keywords, which are part of the PHP language and therefore cannot be used as constants, function names, or class Names. however, Unlike other languages, the System keyword can be used as a variable name in php, but it is easy to confuse, so it is better not to use the PHP keyword as the variable name

<? PHP   // Output 123  $echo = 123;   Echo $echo ;? >  

Here is a list of common PHP keywords

AbstractandArray   as   breakcallable case  Catch class  CloneConst Continue  Declare
default die doEcho Else ElseIf EmptyEnddeclare endfor Endforeach endif
EndswitchEndwhile Eval Exit extends Finalfinally for foreach function Global
Gotoif Implements includeinstanceof InsteadofInterface isset Listnamespace
NewOrPrint Private protected public require return Static SwitchThrowTrait
Try unset usevar whileXOR yield

Assigning values to variables

In general, a variable is always assigned a value, that is, when a value of an expression is assigned to a variable, the value of the entire original expression is assigned to the target Variable. This means that when the value of one variable is assigned to another variable, changing the value of one of the variables will not affect the other variable

[note] 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 integer and floating-point variables is 0, the default value for a string variable is an empty string, and the default value for an array variable is an empty array

<? PHP   $a 1 = 123;   $a 2 $a 1 ;   $a 1 = 234;   // Output 234  Echo $a 1 ;   Echo ' <br> ';   // Output 123  Echo $a 2 ;? >   

PHP provides another way to assign a value to a variable: a reference assignment. This means that the new variable simply refers to the original Variable. Changing the new variable will affect the original variable and Vice Versa.

Use reference assignment to simply add a ' & ' symbol to the source variable

<? PHP   $a 1 = 123;   $a 2 = &$a 1;   $a 1 = 234;   // Output 234  Echo $a 1 ;   Echo ' <br> ';   // Output 234  Echo $a 2 ;? >  

Variable variable

Variable names for a variable can be set and used Dynamically. An ordinary variable is set by a declaration, and a mutable variable gets the value of an ordinary variable as the variable name of the variable Variable.

<? PHP   $hi = ' Hello ';  $$hi = ' world ';   Echo "$hi$hello"; // ' Hello World '  Echo "$hi ${$hi}"; // ' Hello World '?>

Variable function

The number of variable functions is numerous, and some functions are described later in the Post. now, the three functions of Isset (), unset () and Var_dump () are mainly introduced.

The Var_dump () function is used to return the type and value of a variable

<? PHP $p = 3.14; Var_dump ($p); // Float 3.14 $p = ' abc '; Var_dump ($p); // string ' abc ' (length=3)?>

The unset () function is used to release the specified variable

<? PHP $p = ' abc '; Echo $p; // ' abc ' unset ($p); Echo $p; // Error ?>

The Isset () function is used to detect if a variable is set, returns True if a variable is set to null or released, otherwise false

<?PHP$p= ' abc ';Var_dump(isset($p));//Boolean True$p=NULL;Var_dump(isset($p));//Boolean falseunset($p);Var_dump(isset($p));//Boolean false?>

Front-End Learning PHP variables

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.