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.

Monday, December 4, 2006

Access Form Variables

An aim of using php for you may be processing html forms. its easy to access form variables in php. i wont write a sample html form code here. i suppose you already have an html form where each input has a name value. There are general 3 ways of accessing form variables in php. Lets suppose that you have an input field in your form named as input_var :

$input_var //short style
$_POST['input_var'] //middle style
$HTTP_POST_VARS['input_var'] //long style

the short style is valid only if the register_globals configuration is on. since php version 4.2.0 this configuration is off as default. this style is not recommended.

middle style is the recommended style that is valid since php version 4.1.0

long style was portable in the past but now requires the register_long_arrays configuration.

i always use the middle style. it is easy, not confusing and also portable. if you dont want to access your form variable like this everywhere needed in your code then you can just assign it to another variable (with the form name generally) and use it freely as you need.

$input_var = $_POST['input_var'];
// lets use our form variable for output
echo $input_var;

Concatenate Strings

when you need printing more than one strings where there may be variables or other things between them, it would be annoying to write a new statement to echo all the parts. php has a nice way of doing this for you, string concatenating
let me show you an example:

echo "you have " . $msg_num . "messages!" ;

the . operator is the string concatenation operator and is very useful for situations like this. ( i left spaces after and before '.' operators to make them more visible. this is not needed in fact. )
you could also write the statement above like that without string concatenating :

echo "you have $msg_num messages!" ;

this will give an output like:

you have 3 messages!

depending on the variable $msg_num .
be careful that you can do this only inside double quotes. if you use a statement like that:

echo 'you have $msg_num messages! ' ;

you 'll get html output exactly as in the quotes:

you have $msg_num messages!

there should be a value instead of $msg_num here so we should use double quotes for this statement. single quotes give output without changing.