Monday, October 24, 2011

php control structures

some php control structures
====================

while
------

$i=0;
while ($i<=3) {

echo $i . "
";
$i++;

}

for
----

for ($i=1;$i<=3;$i++) {

echo $i . "
";

}

if
--

for ($i=1;$i<=3;$i++) {

if ($i==2) {

echo "One loop left
";

}
echo $i . "
";

}



Sunday, October 23, 2011

php variables

variables
==========

any variable starts with the dollar ($) sign,
variables are used to store values like strings, numbers
or arrays.

$i=0;
$str="hello world";

to display a variable, php used the function echo:

echo $i;
echo $str;

arrays
======

an array is a set of values, grouped in a variable.

$array()=('Frank', 'Kafka', '10.09.1929');

to retrieve each array value, one method is as follows:

$array_value_1=$array[0];
$array_value-2=$array[1];
$array_value_3=$array[2];

echo $array_value_1;
echo $array_value_2;
echo $array_value_3;

to concastinate the values with strings, it works as follows:

echo "the array values are :" .$array_value_1 . " and " . $array_value_2 . " and " .$array_value_3;

-> associative arrays are used to associate each value with a key.

$notes=("Wilson"=>"A+", "Sam"=>"B");

we can echo out the values:

$note_wilson=$notes['Wilson'];

$note_sam=$notes['Sam'];


some useful array functions
=============================

function count

$i=count($array);

echo $i;

$i is then 3.

In this case, we keep it simple, and count can be used as such, because
we don't have a multidimensional array.