Tuesday, July 20, 2010

Lesson number two...wohoo programming is as easy as i wished for

So today was a hard day...
PHP is basically variables, arrays, loops: while, for, foreach. and conditions: if elseif else.
Well it's not all that there is to php, but it is kind of basic thing that makes PHP basics.
So here we go....variables.
PHP is a language where you do not need to declare variables(if you never programmed in any other language before just ignore this sentence).Well basically variable is a container for your info...
< ?php
$variable='i am variable';
echo $variable;
?>
so if you see $ before a word it means it is php variable. Here we put in that container string(sentence) "i am variable"...Those single quotes mean that text within them is string. Variable can contain a lot of different stuff...even html code...but if you want it to contain html then you must put it within " " quotes. If you want your information to be ordinary number then you should leave quotes out...like $variable=1;
Massive is what i will speak about now
Massive is a container that contains other smaller containers....
For example
< ?php
$bigcontainer[1]=1;
$bigcontainer[2]=2;
$bigcontainer[three]=3;
?>
So what did we do here? It is actually pretty easy, imagine we have one big big containter named library. Now we put 3 small containers, first one we call "1" and it containes value 1. Second container we call 2 and it contains value 2 and third one we call three and it contains value 3. If you want to output of any small container on your page just echo $array[name of the small container];
if you want to output all three containers and to have space between them then:
echo $bigcontainer[1]." ".$bigcontainer[2]." ".$bigcontainer[three];
dots there mean that " add it next to what you echo"
NOTE: things that are written within [ ] are called key's.
NOW COMES INTERESTING STUFF=) Well that's my personal opinion...
IF...conditions....
< ?php
some code
if($car==1){
echo $car;
}
elseif($car==0){
echo 'car is empty and it is on standby mode';
}
else{
echo 'car can contain no more than one person...so get the hell out';
}
?>
SOOOOOO...i bet you do not understand what the hell is happening if you are newbie=) It is easy actually... some code means that there is some code before that has the value of $car somewhere in it....== is comparing....you compare $car value with value on the right and if they are equal you get that condition true and the code inside { } is being executed. !== would mean if $car value does not equal value on the right then this condition is true. Well i believe that it is quite simple and you understand it. Just else means that if $car==1 condition is not true and $car==0 is not true then the code that is in else{ } is being executed.
Now comes loop
FOR
< ?php
$car=0;
$number_of_people_that_car_can_contain=4;
for($i=0; $i<$number_of_people_that_car_can_contain;$i++) {
$car=$car+1;
}
?>
basically it means that $i has the value of 0 when for first executes code inside { } and it will execute until the condition that $i<$number_blablabla is true. so it means that when $i is 4 it does not execute code inside. and $i++ means that after each loop $i becomes bigger number by 1. FOREACH
< ?php
$a=2;
$array[one]='one';
$array[$a]='$a';
$array[3]="3";
foreach($array as $key => value)
{
echo "$key $value"."<>";
}
?>
NOTE: also possible foreach($array as $value)
So what is the point of it all? You ask me again ..and i answer you again(wow=)) )... foreach works only with arrays and it goes through each element that means if we have an array that contains 5 elements then firstly it will go through 1 element and until the last one. Going through means giving variables $key value of the array key that it is going through right now and giving $value value of current array element value.
means moving onto new line. Output will loock lke this:
one one
2 2
3 3
WHILE
< ?php
$i=0;
while($i<=2) {
$i++;
}
?>
here it does the code until $i value is 3..it means that when $i value is 3 it wont execute code within anymore....
THAT'S it now you can do a lot in php...for real all codes consist of those things...well there are some things that are kinda advanced but i dont know about them yet and will try some stuff with those things in next lessons...and look into functions and object oriented programming maybe...

1 comment:

  1. This comment has been removed by the author.

    ReplyDelete