Search This Blog

Thursday, April 16, 2009

arrays in PHP

To day We will look @ arrays in php


in PHP we can find two types of arrays that one is array that we usually used

with numeric indexed, and another one is associative arrays. this type of arrays

also behave as same as usuall arays we used to.. only difference is we can use alphanumeric key index.



You can understand this with following example..


normally array is key value paired .



ex1:

[0] => 456

[1] => 3456

[2] => 566

[3] => 70


-------------------------------------------


ex 2:


["name"] => "Mc.Donald"

["ID"] => 1234567889

["SSN"] => "239898K"

["Country"] => "Australia"

["age"] => 45


in first example array index keys are numeric values {0,1,2,3} but in example 2 it is an alphanumeric values {name,ID,SSN,Country,age}



How can create and access the arrays in PHP

arrays can be created and accessed in normall way how we do in other languages only difference is we dont specify the type of the values that holds by the array

see below example how we create array.


$Marks= array(0=>12,1=>34,2=>50,3=>60,4=>45); this array definition is also same as below definition..

$MarksII= array(12,34,50,60,45); // with this style index is autogenerated.



echo $Marks[0]; // output is 12

echo $MarksII[0]; // output is 12


now we see ho we can create an associative array ..

this is also same as normal array definition.



$StudentsMarks = array("kamal"=>30,"amal"=>34,"nimal"=>50,"sunil"=>20); // this holds an marks of each stdents for a particular subject

how we can print marks of a given student.



echo $StudentsMarks["amal"]; /// this wil echoes the marks of student amal. output is 34

echo $StudentsMarks["sunil"]; /// this wil echoes the marks of student sunil. output is 20



when we working with array we have to loop through the array. how we can loop associative array

is like this...



foreach($key=key($StudentsMarks))

{

echo "marks of $key : $StudentsMarks[$key]<>";


}


this will prints an out put like below...



marks of kamal : 30

marks of amal : 34

marks of nimal : 50

marks of sunil :20



i think you got some idea about arrays in PHP and im retain @ here.. we'll meet again with onother lesson

thank you..

Saturday, April 4, 2009

PHP lesson II

Hi all ,
Today we gonna have a look at data types and variable in PHP

is there any data type in php? have you ever noticed that? like other programming languages we don't mention the data type of the variable wehen it declares huh..?
ex..
in C# ig we want an integer variable we declare it like this
int i=0;
but with PHP we dont mention it so. what we do is we simply use
$i=0;

why we can do this, how php going to identify the type of the variable then.. does php has no data type ? we have to find answers fo these questions rite?

the reason for this defference is "typing" what is typing means.. while other laguages use "strong type" php is gonna use "weekly type". basically in a higher level PHP has scalar type only. but behid the screen you can find couple of data type those are integer,string,double. PHP identifies the data type depending on the variable context.. you can see this with the following simple script
$i=10; // this will be integer type
$f=250.00; //this is a double
$str="Hi i'm string"; // this is string type
$s='im too string '; // This is also a string
$c='a'; // This is also a string

/*to see the data type of each variable we can use getType(); function*/

echo( getType($i));
echo "<br/>"; ;

echo(getType($f));
echo "<br/>";

echo( getType($str));
echo "<br/>"; ;

echo( getType($s));
echo "<br/>"; ;

echo(getType($c));
echo "<br/>";

?>

The out put will be like this
integer
double
string
string
string

try this.. hope you got to know something new..
will meet with another lesson later
bye...

:-)

Friday, April 3, 2009

හිංගලෙන් ලියන්න හිතුනා

හිංගලෙන් කතා කරමු

ම‍ට අද නිකමට හිතුනා මොනවා හරි සිංහලෙන් ලියන්න ඕන කයලා. ඉතින් ඔන්න ලිව්වා. ඔයාලා කාට හෝ අදසක් තියෙනවා නම් මේ අදහසට ප්‍රතිචාර එවන්න. සුදුසු මාතෘකාවක් අරන් අපි සංවාදයක් ගොඩ නගමු..

එහෙනම් මම මෙතනින් නවතිනවා.. බොහෝම ස්තූතියි.