Monday, July 15, 2013

Lesson 3 - Conditional Statements


Hello people, now we are going to talk about more basic stuff with PHP. Until now we know about setting up a server , how to print a statement , and assign variables and manipulate it. Today we are going to discuss about conditional statements. Most of the time conditional statements outputs Boolean value. (Either true or false). To accomplish this task we need a operand to complete the condition. Below mentioned are , common operands that we use in development.

Example
Name
Result
$a == $b
Equal
TRUE if $a is equal to $b after type juggling.
$a === $b
Identical
TRUE if $a is equal to $b, and they are of the same type.
$a != $b
Not equal
TRUE if $a is not equal to $b after type juggling.
$a <> $b
Not equal
TRUE if $a is not equal to $b after type juggling.
$a !== $b
Not identical
TRUE if $a is not equal to $b, or they are not of the same type.
$a < $b
Less than
TRUE if $a is strictly less than $b.
$a > $b
Greater than
TRUE if $a is strictly greater than $b.
$a <= $b
Less than or equal to
TRUE if $a is less than or equal to $b.
$a >= $b
Greater than or equal to
TRUE if $a is greater than or equal to $b.

So lets look at the conditional statements that we use to make our logic to work in real world.

IF...ELSE Statement

The structure of if..else statement goes as follows
if(condition){
   statement 1;
}elseif(condition){
   statement 2;
}else{
   statement 3;
}
If else statement work as same logic that we use in our everyday life. If our first condition is met true then we'll execute first statement. Else if we met the second condition true , then we execute the second statement. And if we don't met any of our conditions then third statement will be executed. Let's get this logic with real life scenario with Pseudo Code below.

/* I am hungry right now. I wonder around the city. */
if(I met a Burger King){
   Have a Burger;
}elseif(I met Pizza Hut){
   Have a Pizza;
}else{
   Have a Rice and Curry from Mathara Food Corner;
}
Now, let me show you PHP coding for above mentioned scenario.
/* I am hungry right now. I wonder around the city. */
if($shopImet == 'BurgerKing'){
   echo 'Have a Burger';
}elseif($shopImet == 'PizzaHut'){
   echo 'Have a Pizza';
}else{
   echo 'Have a Rice and Curry from Mathara Food Corner';
}
Also , if I wan't to have Pizza today and if I don't find Pizza shop have anything else scenario is converted to PHP like following
/* I am hungry right now. I wonder around the city. */
if($shopImet == 'PizzaHut'){
   echo 'Have a Pizza';
}else{
   echo 'Have a Rice and Curry from Mathara Food Corner';
}

IF.. ELSE short hand expression

As we getting progress on conditional statements , PHP gives us some shorthand if....else usage technique. The format is as follows.
 Condition ? Statement on True : Statement on false )
 Example:
echo ($result >=75 ? 'pass' : 'fail'); //if result is 85 this will print pass
echo ($result >= 75 ? ($age < 15 ? 'genius' : 'good'): 'fail'); // bit complicated ha! Naah this is easy if result is 80 and age is 10 then this will print genius. 

SWITCH  Statement

Now we know about If ....Else statements. In production we may need heavy nested  IF....ELSE statements. Experts suggested that using switch statements rather than nested  IF...ELSE statement is less complicated and highly readable. Let's checkout syntax of  
SWITCH(Expression){
  CASE scenario_1:
     statement_1;
  BREAK;
  CASE scenario_2:
     statement_2;
  BREAK;
  CASE scenario_3:
     statement_3;
  BREAK;
  DEFAULT:
     default_statement;
} 
When you pass an expression to switch statement it will check for cases that will match its cases. If there is a match that case will be executed. If there is no match for given cases and DEFAULT is defined then default statement will be executed. But there is a tweak. What happens when you do not mention BREAK statement like following.

SWITCH(Expression){
  CASE scenario_1:
     statement_1; 
  CASE scenario_2:
     statement_2; 
  CASE scenario_3:
     statement_3; 
  DEFAULT:
     default_statement;
}  // if all three scenarios matches then statement_1,statement_2,statement_3 will be executed


OR 

SWITCH(Expression){
  CASE scenario_1:      
  CASE scenario_2: 
  CASE scenario_3:
     statement_if_1_or_2_or_3_are_true; 
  DEFAULT:
     default_statement;
} 
 We will discuss more about Loops in our next lesson.

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.

Friday, July 5, 2013

Lesson 1: Hello world !

Let's step into the pool and start to swim........ 


  1. First you need to install apache 2 server and PHP in your operating system . There are many executable files that can install this in your workstation.
  2. Make sure you are running apache server .
    • try 'localhost' (without quotes ) in your browser. If it shows a page then you are fine. Else there is something wrong with the installation.
  3. Make sure you have a text editor. For windows I would suggest Notepad++ .
  4. Go into the servers public_html folder. For Wamp/linux systems this can be www folder.
      • Ex : C:/wamp/www , C:/ xampp/htdocs
      • Ex:  /srv/www/public_html
  5. Open a new file in the servers default folder you just found in step 4 , and save it as hello_world.php.
  6. Type the following text into it.
  7. <?php  echo 'Hello World !' ?>
  8. And go to your browser and type' localhost/hello_world.php '. And this will show Hello World in the browser.

Explanation:

  1. '<?php ' is opening tag and '?>' is closing tab of php. Compiler will only look into the codes between these tags.
  2. 'echo' means print this .