How to read variable in php

Variables are "containers" for storing information.

Creating [Declaring] PHP Variables

In PHP, a variable starts with the $ sign, followed by the name of the variable:

After the execution of the statements above, the variable $txt will hold the value Hello world!, the variable $x will hold the value 5, and the variable $y will hold the value 10.5.

Note: When you assign a text value to a variable, put quotes around the value.

Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.

Think of variables as containers for storing data.

PHP Variables

A variable can have a short name [like x and y] or a more descriptive name [age, carname, total_volume].

Rules for PHP variables:

  • A variable starts with the $ sign, followed by the name of the variable
  • A variable name must start with a letter or the underscore character
  • A variable name cannot start with a number
  • A variable name can only contain alpha-numeric characters and underscores [A-z, 0-9, and _ ]
  • Variable names are case-sensitive [$age and $AGE are two different variables]

Remember that PHP variable names are case-sensitive!

Output Variables

The PHP echo statement is often used to output data to the screen.

The following example will show how to output text and a variable:

The following example will produce the same output as the example above:

Example

Try it Yourself »

The following example will output the sum of two variables:

Note: You will learn more about the echo statement and how to output data to the screen in the next chapter.

PHP is a Loosely Typed Language

In the example above, notice that we did not have to tell PHP which data type the variable is.

PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.

In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.

You will learn more about strict and non-strict requirements, and data type declarations in the PHP Functions chapter.



Sometimes it is convenient to be able to have variable variable names. That is, a variable name which can be set and used dynamically. A normal variable is set with a statement such as:

A variable variable takes the value of a variable and treats that as the name of a variable. In the above example, hello, can be used as the name of a variable by using two dollar signs. i.e.

At this point two variables have been defined and stored in the PHP symbol tree: $a with contents "hello" and $hello with contents "world". Therefore, this statement:

produces the exact same output as:

i.e. they both produce: hello world.

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second.

Class properties may also be accessed using variable property names. The variable property name will be resolved within the scope from which the call is made. For instance, if you have an expression such as $foo->$bar, then the local scope will be examined for $bar and its value will be used as the name of the property of $foo. This is also true if $bar is an array access.

Curly braces may also be used, to clearly delimit the property name. They are most useful when accessing values within a property that contains an array, when the property name is made of multiple parts, or when the property name contains characters that are not otherwise valid [e.g. from json_decode[] or SimpleXML].

Example #1 Variable property example

The above example will output:

I am bar.
I am bar.
I am bar.
I am r.

Warning

Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.

userb at exampleb dot org

12 years ago

Anonymous

17 years ago

It may be worth specifically noting, if variable names follow some kind of "template," they can be referenced like this:



This is apparent from the notes others have left, but is not explicitly stated.

Anonymous

20 years ago

The 'dollar dereferencing' [to coin a phrase] doesn't seem to be limited to two layers, even without curly braces.  Observe:



This works for L-values as well.  So the below works the same way:



NOTE: Tested on PHP 4.2.1, Apache 2.0.36, Red Hat 7.2

Sinured

15 years ago

One interesting thing I found out: You can concatenate variables and use spaces. Concatenating constants and function calls are also possible.



This syntax doesn't work for functions:

Chủ Đề