back || home || next

Tutorial 2 : Variables

 

Variables

A variable is a special container that you can define to "hold" a value. A variable consists of a name that you can choose, preceded by a dollar ($) sign. The variable name can include letters, numbers, and the underscore character (_). Variable names cannot include spaces or characters that are not alphanumeric. The following code defines some legal variables:

$a;

$a_longish_variable_name;

$2453;

$sleepyZZZZ;

 

Remember that a semicolon (;) is used to end a PHP statement. The semicolons in the previous fragment of code are not part of the variable names.

 

NOTE:

PHP do not have data types for the variables, that’s because it is a loosly typed language and it correctly assign a data type for the variables according to the use of the variable.

<?php

$num1 = 10;  //this variable is Integer

$num2 = 20;  //this is same too

Print $num1+$num2;

?>

 

<?php

$name = “PHP 4”;    //this variable is String

Print $name;

?>

 

<?php

$num = 123.456;     //this variable is double

Print $num/12;

?>

 

Data Types

Different types of data take up different amounts of memory and may be treated differently when they are manipulated in a script. Some programming languages therefore demand that the programmer declare in advance which type of data a variable will contain. PHP4 is loosely typed, which means that it will calculate data types as data is assigned to each variable. This is a mixed blessing. On the one hand,

it means that variables can be used flexibly, holding a string at one point and an integer at another. On the other hand, this can lead to confusion in larger scripts if you expect a variable to hold one data type when in fact it holds something completely different.

 

The gettype() function

You can use PHP4's built-in function gettype() to test the type of any variable. If you place a variable between the parentheses of the function call, gettype() returns a string representing the relevant type.

<?php

$number = 123;

$num = 123.456;

$name = "PHP 4";

Print "Using gettype()<br>";

Print gettype($number)."<br>";

Print gettype($num)."<br>";

Print gettype($name);

?>

 

This will print as follows,

 

Using gettype()

Intrger

Double

String

 

Changing Type with settype()

PHP provides the function settype() to change the type of a variable. To use settype(), you must place the variable to change (and the type to change it to) between the parentheses and separated by commas.

 

<?php

$var = 12;     //integer

Print gettype($var).”<br>”; //integer

Settype($var , double);

Print gettype($var) .”<br>”;          //double

Settype($var , string);

Print gettype($var) .”<br>”;          //string

Settype($var , boolean);

Print gettype($var);  //boolean

?>

Changing Type by Casting

By placing the name of a data type in brackets in front of a variable, you create a copy of that variable's value converted to the data type specified. The principal difference between settype() and a cast is the fact that casting produces a copy, leaving the original variable untouched.

 

<?php

$var = 10;

Print gettype($var);  //integer

$var1 = (double)$var;

Print gettype($var1); //double

$var2 = (string)$var;

Print gettype($var2); //string

?>

 

The Assignment Operator

You have met the assignment operator each time we have initialized a variable. It consists of the single character =. The assignment operator takes the value of its right-hand operand and assigns it to its left-hand operand:

 

$name ="matt";

 

The variable $name now contains the string "matt". Interestingly, this construct is an expression. It might look at first glance that the assignment operator simply changes the variable $name without producing a value, but in fact, a statement that uses the assignment operator always resolves to a copy of the value of the right operand. Thus

 

print ( $name = "matt" );

prints the string "matt" to the browser in addition to assigning "matt" to $name.

 

Arithmetic Operators

The arithmetic operators do exactly what you would expect. The addition operator adds the right operand to the left operand. Thesubtraction operator subtracts the right-hand operand from the left. The divisionoperator divides the left-hand operand by the right. The multiplication operator multiplies the left-hand operand by the right. The modulus operator returns the remainder of the left operand divided by the right.

Operator

Name

Example

Example Result

+

Addition

2+3

5

-

Subtraction

3-2

1

/

Division

3/2

1.5

*

Multiplication

2*3

6

%

Modulus

3%2

1

The Concatenation Operator

The concatenation operator is a single dot. Treating both operands as strings, it appends the right-hand operand to the left. So "hello"." world" returns

 

"hello world"

 

Regardless of the data types of the operands, they are treated as strings, and the result always is a string.

 

More Assignment Operators

Although there is really only one assignment operator, PHP4 provides a number of combination operators that transform the left-hand operand as well as return a result. As a rule, operators use their operands without changing their values. Assignment operators break this rule. A combined assignment operator consists of a standard operator symbol followed by an equals sign. Combination assignment

operators save you the trouble of using two operators yourself. For example,

 

$x = 4;

$x += 4; // $x now equals 8

is equivalent to

$x = 4;

$x = $x + 4; // $x now equals 8

 

There is an assignment operator for each of the arithmetic operators and one for the concatenation operator.

Operator

Example

Equivelent to

+=

$v += 2

$v = $v + 2

-=

$v -= 2

$v = $v -2

/=

$v /= 2

$v = $v / 2

*=

$v *= 2

$v = $v * 2

%=

$v %= 2

$v = $v % 2

.=

$v .= “Concatenation”

$v = $v” Concatenation”

continued to next page...