Saturday, July 18, 2009

PHP Arrays

In PHP, there are three kind of arrays:

* Numeric array - An array with a numeric index
* Associative array - An array where each ID key is associated with a value
* Multidimensional array - An array containing one or more arrays

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

Numeric Arrays


A numeric array stores each array element with a numeric index.

<html>
<body>
<?php
$carII=array("Saab","Volvo","BMW","Toyota");

$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";

//echo $carll[0] . " and " . $carll[1] . " are Swedish cars.;
echo $cars[0] . " and " . $cars[1] . " are Swedish cars.";
?>

</body>
</html>

-------------------------------------------
Associative Arrays

An associative array, each ID key is associated with a value.

When storing data about specific named values, a numerical array is not always the best way to do it.

With associative arrays we can use the values as keys and assign values to them.


<html>
<body>

<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";

echo "Peter is " . $ages['Peter'] . " years old.";
?>

</body>
</html>

---------------------------------------------------------
Multidimensional Arrays

In a multidimensional array, each element in the main array can also be an array. And each element in the sub-array can be an array, and so on.


<html>
<body>
<?php
$families = array
(
"Griffin"=>array
(
"Peter",
"Lois",
"Megan"
),
"Quagmire"=>array
(
"Glenn"
),
"Brown"=>array
(
"Cleveland",
"Loretta",
"Junior"
)
);
echo "Is " . $families['Griffin'][2] .
" a part of the Griffin family?";
?>

</body>
</html>


The array above would look like this if written to the output:
Array
(
[Griffin] => Array
(
[0] => Peter
[1] => Lois
[2] => Megan
)
[Quagmire] => Array
(
[0] => Glenn
)
[Brown] => Array
(
[0] => Cleveland
[1] => Loretta
[2] => Junior
)
)

---------------------------------------------------
For Loop

<html>
<body>

<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "< br /> ";
}
?>

</body>
</html>
--------------------------------------
The foreach Loop

The foreach loop is used to loop through arrays.

<html>
<body>

<?php
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br /> ";
}
?>

</body>
</html>

-----------------------------------------------------
Create a PHP Function

A function will be executed by a call to the function.

<html>
<body>

<?php
function writeName()
{
echo "Kai Jim Refsnes";
}

echo "My name is ";
writeName();
?>

</body>
</html>

------------------------------------------------------
PHP Functions - Adding parameters

To add more functionality to a function, we can add parameters

<html>
<body>

<?php
function writeName($fname)
{
echo $fname . " Refsnes.<br />";
}

echo "My name is ";
writeName("Kai Jim");
echo "My sister's name is ";
writeName("Hege");
echo "My brother's name is ";
writeName("Stale");
?>

<body>
<html>

-------------------------------------------------
PHP Functions - Return values

To let a function return a value, use the return statement.

<html>
<body>
<?php
function add($x,$y)
{
$total=$x+$y;
return $total;
}

echo "1 + 16 = " . add(1,16);
?>

</body>
</html>

PHP Basic

PHP stands for Hypertext Preprocessor and is a server-side language. This means that the script is run on your web server, not on the user's browser, so you do not need to worry about compatibility issues. PHP is relatively new (compared to languages such as Perl (CGI) and Java)

PHP syntax

<?php
?>
---------------------------------------------------------
First PHP script

<html>
<body>

<?php
echo "Hello World";
?>

</body>
</html>

For display: Hello World using the PHP echo() statement.
-----------------------------------------------------------------------------
Comments in PHP
<?php
//This is a comment

/*
This is
a comment
block
*/
?>
----------------------------------------------------------------------------
PHP is a Loosely Typed Language

In PHP, a variable does not need to be declared before adding a value to it.

All variables in PHP start with a $ sign symbol.

$var_name = value;

<html>
<body>
<?php
$txt="Hello World! PHP";
$x=16;
echo $txt ;
?>
</body>
</html>

------------------------------------------------------------------------
* A variable name must start with a letter or an underscore "_"
* A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ )
* A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)


<html>
<body>

<?php
$txt1= "Hello World!";
$txt2="What a nice day!";
$txt3=strlen("Hello World!");
$txt4=strpos("Hello world!","world");
echo $txt1 . " " . $txt2 . " " . $txt3. " " . $txt4 ;

?>

</body>
</html>
----------------------------------
PHP Operators

+ Addition x=2 x+2
- Subtraction x=2
* Multiplication x=4
/ Division 15/5
% Modulus (division remainder) 5%2
++ Increment x=5 x++ x=6
-- Decrement x=5 x-- x=4

Assignment Operators
= x=y x=y
+= x+=y x=x+y
-= x-=y x=x-y
*= x*=y x=x*y
/= x/=y x=x/y
.= x.=y x=x.y
%= x%=y x=x%y

Comparison Operators
== is equal to 5==8 returns false
!= is not equal 5!=8 returns true
> is greater than 5>8 returns false
< is less than 5<8 returns true
>= is greater than or equal to 5>=8 returns false
<= is less than or equal to 5<=8 returns true

Logical Operators
&& and x=6 y=3 (x < 10 && y > 1) returns true
|| or x=6 y=3 (x==5 || y==5) returns false
! not x=6 y=3 !(x==y) returns true

---------------------------------------------------------
IF

<html>
<body>

<?php
$d=date("D");
if ($d=="Fri")
{
echo "Hello!<br />";
echo "Have a nice weekend!";
}
elseif ($d=="Sun")
echo "Have a nice Sunday!";
else
{
echo "Hello!<br />";
echo "Have a nice day!";
}
?>

</body>
</html>

------------------------------------------------------------------
SWITCH

<html>
<body>

<?php
switch ($x=1)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>

</body>
</html>