Monday, July 8, 2013

Lesson 2 : Variable Assignment

In my last document we discussed about how to install apache server, PHP  in a server. And how to print a statement in a web page. Let's have an overview about PHP.

PHP is an Object Oriented Programming Language. (That means it allows work with classes and its instance). We will discuss about O.O.P in great detail in future blogs. And most of the time PHP compiles line by line (for your basic understanding ). As we discussed in our last lesson PHP server reads whatever inside <?php ?> (or  if you configured your server to allow short tags then <??>) and executes it. 

Lets have an example,
<?php 
 echo 'Hello World'; // outputs Hello World
 echo 'This is second line'; //outputs This is second line
 echo 3+4; // outputs 7
?> 
But  you will see some thing like this in your web page.
Hello WorldThis is second line7
Why? because there is no line breaks.  The corrected version will be ,
<?php
  echo 'Hello World <br/>'; // outputs Hello World
 echo 'This is second line<br/>'; //outputs This is second line
 echo 3+4; // outputs 7
?> 
 Lets assign values to some variables. As the way PHP is designed , it has no casting  or variable type at all. Sure there is , but not when you are assigning values to it. You can assign any value including Class objects, Arrays , Strings , Numerics , Lists etc., etc. Lets have an example.
<?php
 $my_first_variable = 'Hello World'; // assigns  Hello World to  $my_first_variable
 $my_second_variable = 3;
 $my_third_variable = 4; 
 $my_array = array(1,2,3.1,3,'Tithira Hiranjth','I love dogs');
 $my_class_object = new myClass(); 
?> 
As you can see we can assign any type to a variable. There are two naming conventions to variables. Of course you cannot declare a variable starting with a numeric. For declaring a variable you should start with Dollar Sign '$'. Then continue the name that you can understand easily. If the variable name is one word then you can put it as what it is. As $mango . If it is more than one word, either you can separate the word with a UNDERSCORE '_' or you can continue all words together by capitalizing first character of each word except initial word. Lets have an example.
  • $this_is_first_convention
  • $thisIsSecondConvention
Clear? Good. Let's Proceed. Now we will look how we can have string concatenation and simple math with variables. First thing we need to remember is most of the time variables assign by copy , unless we explicitly say them to assign by reference (assigning class objects is always assign by reference. Indeed!) . So most of the time we do not need to worry about assign values to variables.

We concatenate strings by using dot ".". Lets have an example.
 $myCountry = 'I am '.' Sri Lankan';
echo $myCountry; //this will print I am Sri Lankan.
Now we'll do some simple math.
 $first_math= $my_first_variable + $my_second_variable;// this will assign 3+4 = 7
echo $first_math; //this will print 7.
Thank you! Let's meet again with next lesson Conditional Statements.

1 comment: