Saturday, November 6, 2010

Basic PHP knowledge for Drupal - Part 2

As per the previous section, five key part of PHP knowledge os necessary to start drupal development.

1. PHP variables: These are represented as $variablename , Also there are certain conventions about variable names like you cannot put "-" in it or you cannot start  with a number like $34abc etc.
Allowable syntaxes are: $var, $var123, $var_12, $var_abc etc. Generally variables are written in lower cases, in certain cases they can be written in Camel Cased ($VarName etc). It is good to practice meaningful variable names while writing code as it prevents confusion among the different programmers.

These variables hold the value required for the programming during php execution. There are also centain types of variable depending upon the content of the variable.

Integer type variable: $var = 1;
Float type variable: $var = 2.33;
String type variable: $var = 'Hello World';
Boolean type variable: $var = true;

These are the four major variable types used widely in drupal coding. You can also assign one variable's value to another variable.

e.g $var1 is a Integer type variable.

$var1 = 5;
$var2 = $var1;

Here $var2 will get the value of $var1 which is 5.

The most exciting feature of PHP variable is automatic type casting. Data types can be converted into any types without type casting.
e.g.
$var1 = "5";
$var2 = 5;

These two variables will be treated as same while code execution, though one is integer and another is string.

In the next chapter I will be telling about functions and their usages.