Rabu, 11 September 2013

Condition, Array and Looping at PHP

1. Condition
There's 3 way to make Condition at PHP: if, else, elseif. Every Condition
starting with if :

if(codition) {
//do something
}

if included else:

if(codition) {
//do something
}
else {
//do something else
}

else can be used to add more condition:

if(codition1) {
//do activity1
}
elseif(condition2) {
//do activity2
}
else {
//do other activity
}

*if the condition correct or true values of the existing code in the curly
braces {} will be executed.

2. Array
not seems like string and number only can hold a single value, array can
store/save more than one value. Values ​​can be accommodated, like string,
number, or other arrays. To make arrays in PHP as follows:

$country=array("Indonesia","Japan", "Singapore", "Australia");

or

$country[0]="Indonesia"; 
$country[1]="Japan"; 
$country[2]="Singapore";


-> to create an empty array:

$country=array();

-> to access the value inside array:

$var=$country[1];
echo $var; //the output is Japan
echo $country[0]; //the output is Indonesia

-> sorting array can using with function sort():


sort($country); //array sorted ascending
$var=$country[1]; 
echo $var; //the output is Japan
echo $country[0]; //the output is Indonesia


-> to knowing a lot of elements / or stored values array, using function
   count():

$Amount = count($country);
echo $Amount; //the output is 3


3. Looping
In PHP there are two forms of repetition that is often used, for and while,

-> Looping using for:

for(initial expression; condition; final expression){
//do something
}

Example:
for($i=1; $i<=10; $i++){
echo "Output >,<" ;
}
*this looping will outputing word "Output >,<" ten times.

-> Looping using while:

while(condition){
//do something
}

Example:
$i=1;
while($i<=10){
echo "Output >,,<";
$i++;
}
*this looping will outputing word "Output >,,<" ten times.

"as long as the conditions TRUE, the loop will continue to do"

2 komentar:

  1. When I used to code there is one error that always comes and irritates me, misplaced if-else structure. Finally I have learned it by reading this article.

    Thank you,
    Prasant
    Software Development Company

    BalasHapus