Feb 25, 2011 - Article, Training    No Comments

PHP for beginners – Part 4



Hi ………….

Loops

loops in php are same as in the C language

if….Else loop

if(condition)

{

}

else{

}

example :

<?php

$a=10;

$b=5;

if($a>$b){

echo “a is greater”;

}

else{

echo “b is greater”;

}

?>

instead of above code u can also written the in this manner

if($a>$b):

echo “a is greater”;

else:

echo “b is greater”;

endif;

for loop

for ( initialize a counter; conditional statement; increment a counter){
	do this code;
}
or
for( initialize a counter; conditional statement; increment a counter):
do this code
endfor;
example :
for($i=0;$i>10;$i++){
echo $i;
}

or

for($i=0;$i>10;$i++):
echo $i;
endfor;
while loop

while ( conditional statement is true){
	//do this code;
}
or

while ( conditional statement is true):
	//do this code;
endwhile;

example :
$brush_price = 5;
$counter = 10;

while ( $counter <= 100 ) {

	echo $counter;

	echo $brush_price * $counter;

	$counter = $counter + 10;
}
or
$brush_price = 5;
$counter = 10;

while ( $counter <= 100 ):

	echo $counter;

	echo $brush_price * $counter;

	$counter = $counter + 10;
endwhile;
foreach loop 
this will be explained after array
do.... while loop
do {
	some code;
} while (condition);
example:
$cookies = 0;
do {
	echo "Mmmmm...I love cookies! *munch munch munch*";
} while ($cookies > 1);
switchcase will be explained after method

PHP Tutorial – Part 1
PHP Tutorial – Part 2
PHP Tutorial – Part 3

Got anything to say? Go ahead and leave a comment!

*

Powered By Indic IME

Switch to our mobile site