Variables

Variables can be used for many things. They can be used to help reduce typing, or they can be used to limit looping in an error condition too long. When using the value of a variable, you denote it with a dollar sign and the variable name surrounded by curly braces.

There are two types of variables, namely global variables and call variables. As their names imply, global variables apply to all extensions, while call variables only apply to the current call in progress.

Note

The terms arguments and variables are often interchanged to mean the same thing. For our purposes we are going to make a firm distinction between the two. Arguments are values passed to the built in applications to tell them what to do. Variables are named places where data that may change is held. They can either be used by the user or, much more frequently, by Asterisk itself.

Global Variables

Global variables are denoted using the form VARIABLE_NAME=value. Global variables are useful as they allow us to use them within our dialplan to make it more readable, along with only requiring us to change the the value in a single place instead of multiple places, helping to reduce the number of possible errors.

Call Variables

It is also possible to do simple expressions. These expressions are evaluated when placed within square brackets. Expressions are used to either combine values together via addition, subtraction, multiplication, or division.

[example of some simple variable expressions]

For more information on using expressions with variables, see the README.variables file that comes with the Asterisk source code.

Adding Variables

[Take our Widgets Inc. example and add variables for ${RECEPTIONIST}, etc.]


			[global]
			RECEPTIONIST=Zap/2 ; Assign 'Zap/2' to the global variable ${RECEPTIONIST}
			JOHN=Zap/3         ; Assign 'Zap/3' to the global variable ${JOHN}

			[incoming]
			exten => s,1,Answer()
			exten => s,2,Playback(welcome)
			exten => s,3,Background(menu)
			exten => 1,1,Dial(${RECEPTIONIST}) ; Dial the channel assigned to ${RECEPTIONIST}
			exten => 2,1,Dial(${JOHN}) ; Dial channel assigned to ${JOHN}

			[internal]
			exten => 1001,1,Dial(Zap/2) ; Dialing extension 1001 calls channel Zap/2
			exten => 1002,1,Dial(Zap/3) ; Dialing extension 1002 calls channel Zap/3