Dec 25, 2014

Array in PHP

Array is a data structure which can store one or more values in a single value. 
 
In here '1,2,3,4,5,6' numbers will assign to one variable. Then we can access all values using that variable. In this array for each value has an ID which is called as array index. using that ID we can access array values. 
 In Php, there are three different arrays. 

  • Numeric Array 
  • Associative array  
  •  Multidimensional array

Numeric Array 

We already know about array index. An Array which is use numeric indexing then that array is a numeric array. By default numeric array index start with zero.You will get idea from this example. 
$myArray = array("H","e","l","l","o","W","o","r","l","d"); // Array intialization
// access the values of created array
echo $myArray[0]; //first value of the array. 0 is the array index
In this example there are ten values that means that array max index is 9. Because numeric array start with 0 (zero). We can get first value using zero th index then second value from first index and so on.We can create numeric array from another way also.
$myArray = array(); // Array intialization
$myArray[0] = "H";
$myArray[1] = "e";
$myArray[2] = "l";
$myArray[3] = "l";
$myArray[4] = "o";
$myArray[5] = "W";
$myArray[6] = "o";
$myArray[7] = "r";
$myArray[8] = "l";
$myArray[9] = "d";
// print the values
echo $myArray[0];
echo $myArray[1];
echo $myArray[2];
echo $myArray[3];
echo $myArray[4];
echo $myArray[5];
echo $myArray[6];
echo $myArray[7];
echo $myArray[8];
echo $myArray[9];

Associative Arrays 

Associative array is similar to numeric array. The difference is the array indexing. In here you can put a string as the index key. In numeric array we used numeric values for indexing. But here not use numeric as an index. 
$myArray = array("fname"=>"Harshana", "lname"=>"Samaranayaka","email"=>"awharshana@gmail.com"); // Array intialization
echo "My first name is ".$myArray["fname"]."<br />";
echo "My last name is ".$myArray["lname"];

Multidimensional Arrays 

In multidimensional array we can use nested array. $myarray[0][1] this means get the first index and inside first index get the second value. Overall Image.
you can see the first index value is another array. That is the multidimensional array. Then check how to implement this kind of array in php.
$person= array("name"=>array("fname"=>"Harshana", "lname"=>"Samaranayaka"),
"position"=>"Student","email"=>"awharshana@gmail.com"); // Array intialization
echo "My first name is ".$myArray["name"]["fname"]."<br />";
echo "My last name is ".$myArray["name"]["lname"];
echo "My Position is ".$myArray["position"];
echo "My email address is ".$myArray["email"];
you can see the 'name' index has an array. 

Dec 24, 2014

Decision Making in PHP

PHP Decision Making

What is decision making ? Think you need to check a number equals to another number.
$a = 10;
$b = 20;
You know these two variables are not equal. In programming how do you use it. When you make a decission that decision can be true or false we already know that there is data type called 'boolen'. that data type hold true or false only. if the '$a ' equals to '$b' the out put should be true. but in my example '$a' not equals to '$b' so the output should be false. 

The IF..ELSE Statement 

We can use to make a decision 'if else' statements. if ( checking condition ). Lets do a example. 
$a = 10
$b = 20
if($a == $b){
echo "a equals to b";
}else{
echo "a not equals to b";
}
in here we use ' == ' sign. When we use ' = ' sign we spell it as 'equal'. so When we use ' ==' sign we spell that ;equal equal to' . Then the out put will be a boolean value (true or false) because we need to use boolean value for the if statement's condition. When we use comparison operators out put will be a boolean value(check php-operators).

ELSEIF Statement

 If we need to check multiple cases we can use 'elseif ' statement.
$a = 10;
$b = 20;
$c = 20;
if($a == $b){
echo "a equals to b";
}elseif($a == $c){
echo "a equals to c";
}else{
echo "a not equals to b and a not equals to c ";
}

The Switch Statement

We also can use switch statement for multiple case decision make. Above example using switch statement. 
$a = 10;
$b = 20;
$c = 20;
switch($a){
case $b:
echo "a equals to b"; break;
case $c:
echo "a equals to c";
default:
echo "a not equals to b and a not equals to c ";
}
in switch statement we can put the variable which need to check in side brackets. in our exaple it looks like this 'switch($a)' because we need to check variable '$a'. Then in 'case' statement can put checking values for variable '$a'. In our example 'case $b' and 'case $c'. Which means check variable $a 's value equals to variable $b if that is not equals then check variable $c if that also not equals then 'default' statement executes.
Thats it we done it. !. Lets do a example.
<!DOCTYPE >
<html>
<head>
<title>Php decision</title>
</head>

<body>
<?php
$a = 10;
$b = 20;
$c = 10;
//print the variables
echo "a is ". $a. "<br />";
echo "b is ". $b. "<br />";
echo "c is ". $c. "<br />";

//if else statement
echo "<h2>if else statement </h2> ";
if($a == 10){
echo "a is ten ! <br />";
}else{
echo "a is not ten <br />";
}
//end if else statement

//elseif statement
echo "<h2>elseif statement </h2> ";
if($a == $b){
echo "a equals to b <br />";
}elseif ($a == $c) {
echo "a equals to c <br />";
}else{
echo "a is not equals to b or c <br />";
}
//end elseif statement

//switch statement
echo "<h2>switch statement </h2> ";
switch ($a) {
case $b:
echo "a equals to b <br />";
break;

case $c:
echo "a equals to c <br />";
break;

default:
# code...
break;
}
?>
</body>
</html>

Dec 14, 2014

PHP Operators

PHP support following operators
  • Arithmetic Operators
  • Comparision Operators
  • Logical (or Relational) Operators
  • Assignment Operators
  • Conditional (or ternary) Operators

Arithmetic Operator

lets A = 10 and B = 20
OperatorDescriptionExample
+Adds two operandsA + B will give 30
-Subtracts second operand from the firstA - B will give -10
*Multiply both operandsA * B will give 200
/Divide numerator by denumeratorB / A will give 2
%Modulus Operator and remainder of after an integer divisionB % A will give 0
++Increment operator, increases integer value by oneA++ will give 11
--Decrement operator, decreases integer value by oneA-- will give 9
lets do a simple example.
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
    $a = 10;
    $b = 20;
    
    $c = $a + $b;
    echo "Addtion Operation Result: $c <br/>";
    $c = $a - $b;
    echo "Substraction Operation Result: $c <br/>";
    $c = $a * $b;
    echo "Multiplication Operation Result: $c <br/>";
    $c = $a / $b;
    echo "Division Operation Result: $c <br/>";
    $c = $a % $b;
    echo "Modulus Operation Result: $c <br/>";
    $c = $a++; 
    echo "Increment Operation Result: $c <br/>";
    $c = $a--; 
    echo "Decrement Operation Result: $c <br/>";
?>
</body>
</html>

Comparison Operators

lets A = 10 and B = 20
OperatorDescriptionExample
==Checks if the value of two operands are equal or not, if yes then condition becomes true.(A == B) is not true.
!=Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.(A != B) is true.
>Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true.(A > B) is not true.
<Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.(A < B) is true.
>=Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.(A >= B) is not true.
<=Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.(A <= B) is true.
Lets do a simple example
<html>
<head><title>Comparision Operators</title><head>
<body>
<?php
    $a = 10;
    $b = 20;

    if( $a == $b ){
       echo "TEST1 : a is equal to b<br/>";
    }else{
       echo "TEST1 : a is not equal to b<br/>";
    }

    if( $a > $b ){
       echo "TEST2 : a is greater than  b<br/>";
    }else{
       echo "TEST2 : a is not greater than b<br/>";
    }
    if( $a < $b ){
       echo "TEST3 : a is less than  b<br/>";
    }else{
       echo "TEST3 : a is not less than b<br/>";
    }
    if( $a != $b ){
       echo "TEST4 : a is not equal to b<br/>";
    }else{
       echo "TEST4 : a is equal to b<br/>";
    }
    if( $a >= $b ){
       echo "TEST5 : a is either grater than or equal to b<br/>";
    }else{
       echo "TEST5 : a is nieghter greater than nor equal to b<br/>";
    }
    if( $a <= $b ){
       echo "TEST6 : a is either less than or equal to b<br/>";
    }else{
       echo "TEST6 : a is nieghter less than nor equal to b<br/>";
    }
?>
</body>
</html>
in here if() is codition checker. You will lern more about it later.

Assignment Operators

lets A = 10 and B = 20
OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operandC = A + B will assigne value of A + B into C
+=Add AND assignment operator, It adds right operand to the left operand and assign the result to left operandC += A is equivalent to C = C + A
-=Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operandC -= A is equivalent to C = C - A
*=Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operandC *= A is equivalent to C = C * A
/=Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operandC /= A is equivalent to C = C / A
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operandC %= A is equivalent to C = C % A
Lets do a simple example
<html>
<head><title>Assignment Operators</title><head>
<body>
<?php
    $a = 10;
    $b = 20;
    
    $c = $a + $b;
    echo "Addtion Operation Result: $c <br/>";
    $c += $a;
    echo "Add AND Assigment Operation Result: $c <br/>";
    $c -= $a;
    echo "Subtract AND Assignment Operation Result: $c <br/>";
    $c *= $a;
    echo "Multiply AND Assignment Operation Result: $c <br/>";
    $c /= $a;
    echo "Division AND Assignment Operation Result: $c <br/>";
    $c %= $a;
    echo "Modulus AND Assignment Operation Result: $c <br/>";
?>
</body>
</html>

Logical Operators

lets A = 10 and B = 20
OperatorDescriptionExample
? :Conditional ExpressionIf Condition is true ? Then value X : Otherwise value Y
Lets do a simple example
<html>
<head><title>Arithmetical Operators</title><head>
<body>
<?php
    $a = 10;
    $b = 20;

    $result = ($a > $b ) ? $a :$b;
    echo "TEST1 : Value of result is $result<br/>";

    $result = ($a < $b ) ? $a :$b;
    echo "TEST2 : Value of result is $result<br/>";
?>
</body>
</html>

Basic syntax and Variable in php

Basic Syntaxt

As you know already when we write a java script in a html page we use <script></script> tags. Like that in php we use like this.
<?php

?>
ans also you must save file with extention .php for support php scripts. check with this code  
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test one</title>
</head>
<body>
<?php
print("Hello World !");
?>
</body>
</html>
save the file as test1.php inside the htdocs->Newfolder. The go to the browser and and put the url to the this php file. in my case 'localhost:8081/Newfolder/test1.php'. Note that 'print()' function use for print some to html page.

PHP Variable

In php all variables in PHP are denoted with a leading dollar sign ($). Variables in PHP do not have intrinsic types(a variable does not know in advance whether it will be used to store a number or a string of characters).PHP does a good job of automatically converting types from one to another when necessary. 

Php has eight data types (Integer, Double, Boolean, NULL,String, Array, Object, Resources). Lets do a simple example.
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test Two</title>
</head>
<body>
<?php
$num1_int = 100; // integer number
$num2_int = 50; // integer number
$num1_double = 0.5; //double number
$str1 ="Hello"; // String
$your_name = "Harshana"; // String

echo "<h1>".$str1." ".$your_name."</h1>";
echo "<p> num1_int + num2_int = ". ($num1_int+$num2_int)."</p>";
echo "<p> num1_int + num1_double = ". ($num1_int+$num1_double)."</p>";
echo "<p> num1_int - num2_int = ". ($num1_int-$num2_int)."</p>";
echo "<p> num1_int - num1_double = ". ($num1_int-$num1_double)."</p>";
echo "<p> num1_int x num2_int = ". ($num1_int*$num2_int)."</p>";
echo "<p> num1_int x num1_double = ". ($num1_int*$num1_double)."</p>";
echo "<p> num1_int / num2_int = ". ($num1_int/$num2_int)."</p>";
echo "<p> num1_int / num1_double = ". ($num1_int/$num1_double)."</p>";

?>
</body>
</html>
In here "echo" used to print some thing like print() function. I use dot (.) operator for join string. Save the file as test2.php. and run it and check the browser source code for getting more understanding.   

Nov 17, 2014

Another example with what we have learnt

Hey I'm going to do an another example for more understanding about what i told you in previous post. One more thing i need to say that the static keyword. if we create a method with static keyword that method will be comman method. Just think like that. I'll teach you more about when i teach you OOP concept.
Let's  do little exmaple.
Open the netbeans and create a new project and inside the main method, do the followings,
Output shoud be look like this.
In the above example there is "+" sign. In the java "+" sign do two things.
  • "+" with a string do the concatanation (join two strings) 
  •  "+" with numbers do the arithmatic operation "addition"
 
 

Nov 16, 2014

Data type, class, method in java

Hello this is the second tutorial with the freethemalloc. In this tutorial I'm going to teach you basic thing about the java. As i mentioned before i need to teach you what is a class in java. Basically a class is a template. I'll explain that with simple example.
Think you have a car. The car has states such as color, brand. Then you can create a class called car. Your car class define states of the car like you telling about your car to a friend. "My car is a black one".
Another thing you need to know, that is called methods. So what is a method ? I'll explain that using above example.
Your car has a speed meter. That is state of your car. Speed meter change according to your speed of driving. When you driving the car's speed meter always need to change. That changing part is perform with methods. 
If didn't get what i explain in the above example don't worry. When we do these things with coding it will be easy for you. 
Now we need to to know how we can express state of your car. When we describe about a state it can be a countable or non countable. Eg. Car's color: black (non countable)  and Car's speed meter : 60Kmph (countable) . So when you express state of the car that express types called data types in java. Basically there are two data types in java.


  • Primitive Data Types
  • Reference/Object Data Types

Primitive Data Types

There are eight primitive data types supported by Java. Primitive data types are predefined by the language and named by a keyword. Let us now look into detail about the eight primitive data types.
byte
  • Byte data type is an 8-bit signed two's complement integer.
  • Minimum value is -128 (-2^7)
  • Maximum value is 127 (inclusive)(2^7 -1)
  • Default value is 0
  • Byte data type is used to save space in large arrays, mainly in place of integers, since a byte is four times smaller than an int.
  • Example: byte a = 100 , byte b = -50
short
  • Short data type is a 16-bit signed two's complement integer.
  • Minimum value is -32,768 (-2^15)
  • Maximum value is 32,767 (inclusive) (2^15 -1)
  • Short data type can also be used to save memory as byte data type. A short is 2 times smaller than an int
  • Default value is 0.
  • Example: short s = 10000, short r = -20000
int
  • Int data type is a 32-bit signed two's complement integer.
  • Minimum value is - 2,147,483,648.(-2^31)
  • Maximum value is 2,147,483,647(inclusive).(2^31 -1)
  • Int is generally used as the default data type for integral values unless there is a concern about memory.
  • The default value is 0.
  • Example: int a = 100000, int b = -200000
long
  • Long data type is a 64-bit signed two's complement integer.
  • Minimum value is -9,223,372,036,854,775,808.(-2^63)
  • Maximum value is 9,223,372,036,854,775,807 (inclusive). (2^63 -1)
  • This type is used when a wider range than int is needed.
  • Default value is 0L.
  • Example: long a = 100000L, int b = -200000L
float
  • Float data type is a single-precision 32-bit IEEE 754 floating point.
  • Float is mainly used to save memory in large arrays of floating point numbers.
  • Default value is 0.0f.
  • Float data type is never used for precise values such as currency.
  • Example: float f1 = 234.5f
double
  • double data type is a double-precision 64-bit IEEE 754 floating point.
  • This data type is generally used as the default data type for decimal values, generally the default choice.
  • Double data type should never be used for precise values such as currency.
  • Default value is 0.0d.
  • Example: double d1 = 123.4
boolean
  • boolean data type represents one bit of information.
  • There are only two possible values: true and false.
  • This data type is used for simple flags that track true/false conditions.
  • Default value is false.
  • Example: boolean one = true
char
  • char data type is a single 16-bit Unicode character.
  • Minimum value is '\u0000' (or 0).
  • Maximum value is '\uffff' (or 65,535 inclusive).
  • Char data type is used to store any character.
  • Example: char letter  ='A'

Other type (Reference/Object Data Types) I'll teach you later.

When  we use data types how the values of the data type store? When we use "byte" data type we can use that byte a = 100 then what is "a" ? We call that as vvariables.

What is a variables?

Variable is a piece of memory that can contain a data value.When the programme run, Programme run on the main memory.
 in here there are two variables ('a' and 'b'). When the programme runs these two variables stored in the main memory. When storing values in the RAM data type comes to the role. When we use integer (int) data type has it's own size that is 32 bits. According to the above example variable 'a' store in the RAM with 32 bit size location. How about the variable 'b' ? I know now you know it. :)
There are four type of variables.

  • Non-static fields (Instance Variables)
  • Static fields (Class Variables)
  • Local variables
  • Parameters
I'll explain these thing in later examples. 

How do we define a class in java?

In java we can define class using "class" key word. See the bellow example
public class ExampleClass{

 }

Oooh ! what is the mean of  "public" . In java we called that modifiers.  

What is a modifier ?

When we create a class or method usually we use modifiers. So when we use modifiers that means, we are setting access levels. As a example,
Think about previous car example. Do you like every ones in your school use your car ?  I know you don't like .. like that you can use access levels for the class or methods. There are for modifiers in java. Those are:
  • public
  • protected
  • default
  • private
Modifier Same class Package Subclass Any one
Public
Protected
default
Private
When we create a class we need to consider about the class modifiers. These modifiers we can use in method also.

What is a method ?

Method is a collection of statements (codes). This group of code work together for do a task. Think you need to add two numbers that is a one task then you can create a method to add two numbers. When we create a method we need a class. Methods create inside a class.
When we  create a programme from where the programme start to run ? :O
The programme start with the "main()" method. Can you remember our "hello world" programme? From where the output of "hello world came ? That is came from the main method. Note that when you work with two or more classes you can have only one main method. 

Lets do a one example with Net beans

open the net beans and create a new project and do the following.

Then Run the programme. Output will look like this.


OK see you in next post ! :)

Nov 15, 2014

PHP Introduction and Environment setup

PHP Hypertext Preprocessor 


  • PHP is a web base server side scripting language and it is embedded in HTML. Using this language we can handle dynamic contents.
  • PHP syntax looks like C language.
  • characteristics of PHP
    • Simplicity
    • Efficiency
    • Security
    • Flexibility
    • Familiarity

Environment Setup

  • Download the xampp (because you need a server to run the php scripts)
  • Install the xampp and open it. It will look like this.
  • Then You can start the apache server.
  • If you can't start the apache server and getting error like this.
  • Then follow these steps (we are going to change the apache server port number)
    • click on the config button and select Apache(Httpd.conf)
    • Then it will open a config file from notepad
    • Then find  'Listen 8080'
    • Then change the 8080 to 8081 or 8082 and save the file and start the server
  • After start the just open the browser and put this URL 'localhost' or if you change the (port) 8080 to 8081 then URL should be like this 'localhost:8081'. The you'll be able to see this page.
  • That's all. Now you can develop PHP web applications, \\//

Important

Go to the xampp install directory and find a folder call 'htdocs'. This is the server root folder. So You better to create a folder and inside that folder you need to develop you php application. Because if you create php files inside the root folder some time you will not be able to use xampp default page. SO keep it in your mind. 

Java Introduction and Environment Setup

Hello this is the my first Post in this blog. This post about programming basic. This is based on java programming language. You need to setup the java environment for working with java. So these things you need to download and install.

You need to download the Java SE(Java Standard Edition) package and download the java JDK according to the your system. These instalation worked with windows OS. Now you have lot of quesions isn't it ? 

What is Java ? 

Java is a high-level programming language originally developed by Sun Microsystems and released in 1995. Java runs on a variety of platforms, such as Windows, Mac OS, and the various versions of UNIX.

 What is JDK ?

The Java Development Kit (JDK) is a software development environment used for developing Java applications and applets. It includes the Java Runtime Environment (JRE), an interpreter/loader (java), a compiler (javac), an archiver (jar), a documentation generator (javadoc) and other tools needed in Java development.
Simply JDK need for developing java program. So think like this, when you speak in english with a person who don't know english. So he can not understand what you'r tolking. So if you talking with computer you have to teach a language to computer then that teaching part done by jdk. After installed the jdk computer know what you'r talking.

What is NetBeans ?

 NetBeans IDE is an open-source integrated development environment for developing programmes. Using this IDE you can write codes easily. You also can write programmes without using IDE. But Using the IDE there are many advantages. You will feel that later. 

First Java programme 

Open the NetBeans IDE. 
Then Click the File->New Project

 Then Click Java-> Java Application-> Next
Project Name: Give an any name you want. Then Hit Finish.
Now your screen look like this. 

The you need know these things. This is a java class file. I'll explain what is a java class in next post. Now i need show you a Simple Out put from a simple java programme. I need to print "Hello World". Now you need to change the file content like this.

The Click the Run button. The Out put will look like this. 

Hope you can get this output. Any problem please comment. See you in next post . have a good day !