Thursday, January 18, 2007


The reference operator & was added to PHP in version 4. References are useful when you want to refer to a variable by using a name other than its original one.

$x = 3 ;
$y = &$x ;

The second statement creates a reference for $x named as $y. After now we can access the variable by using its original one or the reference. As we assign a value to one of them, the other has the same value. In fact a reference has not a memory for the value. It only references the original variable.

$x = 5 ;

$x and $y are both 5 after this statement. The reference operator may remind you pointers in the C language. The function is similar but you can't assign the address of a reference manually.

Friday, December 29, 2006

Assignment Operators

The main assignment operator is = which is used to assing the right side value to the left. It doesn't mean 'equal to' as in maths. It means 'set to' . This operator returns the result of the assignment.


This statement has the value 18 . A better example:

$x= ($y=3) + ($z=$y-1) ;

This statement assigns 3 to $y, substracts 1 from $y and assigns it to $z and then adds them to assign the result to $x . There are some combined assignment operators too:

operator example equivalent

+= $x+=$y $x=$x+$y
-= $x-=$y $x=$x-$y
*= $x*=$y $x=$x*$y
/= $x/=$y $x=$x/$y
%= $x%=$y $x=$x%$y
.= $x.=$y $x=$x.$y

Wednesday, December 27, 2006

Arithmetic Operators

Arithmetic Operators in Php are just like the ones you use in mathematics.

+ addition $x + $y
- subtraction $x - $y
* multiplication $x * $y
/ division $x / $y
% modulus $x % $y

These operators are binary (they take two operands) but the subtraction (-) operator is also unary when used with negative numbers. The modulus operator returns the remainder of dividing its operands.

$mod_result=18 % 4;

This statement has the value 2 as a result. If you try to use arithmetic operators with strings php will try to convert the strings to numbers starting from the first character. If there are no digits at the beginning of the string the value of it will be zero.

String Operators

There is only one string operator in PHP (string concatenating operator). For details:

Friday, December 22, 2006


Constants are like variables but their values can't be changed after they are set. Constants are defined like that:

define( 'MY_CONSTANT' , 298 );

this statement defines a constant named as MY_CONSTANT . you don't need to write the constant names with all capital letters but i can say that this is common (or traditional) for defining constants in some other languages too.
while using a constant in the rest of your code you just write its name anywhere that you want its value to be.

echo "my constant is: ";

you see that we put no $ sign before the constant name. that's because a constant is not a variable. constants will be very useful when you have a constant value in your code that is used many times in the code and you frequently change it. by using a constant you don't need to change every statement that has the value you want to change. you only change the definition of the constant. you may also want to use constants when you want to keep some special values that you don't want to memorize or write everytime you need it (pi, speed of light, gravity constant etc.) .

Tuesday, December 19, 2006

Variable Variables

In php you can use "variable variables" to reference the name of a variable by using another variable. this may remind you pointers if you are familiar with C/C++ languages but actually its not the same. variable variables allow you to change the name of a variable. C and C++ don't support this.

$$var_name="value"; //equal to: $my_variable="value";

in this example $$var_name is equal to $my_variable . the second statement assigns the string "value" to $my_variable . (simply put 'my_variable' instead of $var_name). this way you can change the variable name dynamically and use the same expression


for accessing different variables. this feature is sometimes very useful. (for ex: when you keep variable names in an array and use them in a loop.)

Wednesday, December 13, 2006


variables are data symbols carrying a value. in php variables are defined by using a $ sign before an identifier. identifiers are the names of variables. identifiers can't begin with a digit and may include letters, numbers, underscores and the $ sign. using the $ sign has a special meaning for variables (i won't mention about it now.) the name of a variable is case sensitive and you can't use function names as variable names. unlike the c language, in php you don't have to declare a variable before using it. the variable will be created when you first use it.


this is a simple assignment to a variable named as 'myvariable'. php supports these data types:


double is used for real numbers, string is a set of characters, boolean is a logical variable for values true and false, arrays are used for multiple number of variables of the same type and objects are used to store class instances. since php 4 the types NULL, boolean and resource are valid. variables that don't have any value, unset or given the value NULL are of NULL type. some functions return the type 'resource' that you use for handling some special data like database query outputs.
notice that we haven't used any type indicator before the name of the variable in the previous example. in php the type of a variable is the same as the value it has. for example:

$myvar="hello world";

the first statement (creates if not created yet and) makes $myvar an integer, the second statement makes it a double and the last makes it a string by assigning the values in the statements. you can also make type casting when you want the variable to be in an exact type.

$myint=1 ;
$mydouble=(double) $myint ;

the first statement gives the value to $myint as an integer and the second makes an assignment from $myint to $mydouble by casting the type to double although the value is the same actually. $myint is still an integer after type casting.