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.