PHP variables and PHP Variables

Source: Internet
Author: User

PHP variables and PHP Variables
* Directory [1] variable definition [2] keyword [3] variable assignment [4] variable [5] before the Variable Function

A variable is a container used to temporarily store values. These values can be numbers, text, or more complex combinations. Variables are at the core of any programming language. Understanding them is the key to using php. The following describes the variables in php.

[Note] Move the Variable Section in javascript to this point

 

Variable definition

One of the features of php is that it does not need to declare a variable before using the variable. This variable is created only when a variable is assigned a value for the first time.

A variable starts with $, followed by the variable name. The variable name must start with a letter or underline and is case sensitive.

<? Php $ x = 5; echo $ x; // 5 echo $ X; // error?>

[Note] the built-in structure, keywords, and user-defined class names and function names are case-insensitive, such as echo, while, and function names.

<? Php // output 123 echo 1; Echo 2; eCho 3;?>

 

Keywords

Php is a system-defined keyword that is an integral part of the php language and therefore cannot use any of them as constants, function names, or class names. However, unlike other languages, the system keyword can be used as the variable name in php, but this is easy to confuse, so it is best not to use the php keyword as the variable name.

<? Php // output 123 $ echo = 123; echo $ echo;?>

Below is a list of commonly used php keywords

abstract  and  array  as  break  callable case  catch class  cloneconst continue  declare
default diedo echo else elseif emptyenddeclare endfor endforeach endif
endswitchendwhile eval exit extends final finally for foreach function global
goto if implements include instanceof insteadof interface isset list namespace
new or print private protectedpublic require return static switchthrow trait
try unset usevar while xor yield

 

Variable assignment

Generally, a value is always assigned to a variable. That is to say, when the 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 a variable is assigned to another variable, changing the value of one variable will not affect the value of another variable.

[Note] Although you do not need to initialize variables in PHP, it is a good habit to initialize variables. Uninitialized variables have default values of their types. The default values of Boolean variables are FALSE, Integer Variables and floating point variables are 0, and string variables are empty strings by default, the default value of the array variable is an empty array.

<? Php $ a1 = 123; $ a2 = $ a1; $ a1 = 234; // output 234 echo $ a1; echo '<br> '; // output 123 echo $ a2;?>

Php provides another way to assign values to variables: assign values to references. This means that the new variable simply references the original variable. Changing new variables will affect the original variables, and vice versa.

Assign values by reference. Simply add a '&' symbol to the source variable.

<? Php $ a1 = 123; $ a2 = & $ a1; $ a1 = 234; // output 234 echo $ a1; echo '<br> '; // output 234 echo $ a2;?>

 

Variable

A variable name can be dynamically set and used. A common variable is set through declaration, and a variable obtains the value of a common variable as the variable name of this variable.

<?php  $hi = 'hello';  $$hi = 'world';  echo "$hi $hello";//'hello world'  echo "$hi ${$hi}";//'hello world'?>

 

Variable Functions

There are a large number of variable functions. Some functions will be introduced later in the blog. Now we will mainly introduce the isset (), unset (), and var_dump () functions.

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 specified variables.

<? Php $ p = 'abc'; echo $ p; // 'abc' unset ($ p); echo $ p; // error?>

The isset () function is used to check whether a variable is set. If a variable is set to NULL or released, true is returned; otherwise, false is returned.

<?php$p = 'abc';var_dump(isset($p));//boolean true$p = NULL;var_dump(isset($p));//boolean falseunset($p);var_dump(isset($p));//boolean false?>

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.