ඔන්න යාලුවනේ අදත් මම ඔයාලට තවත් අලුත් පොස්ටුවකුත් අරගෙන අවා අද මම කියල දෙන්න යන්නේ php වල අපිට තියන loops ගැන
php ගත්තහම අපිට loops වර්ග 4ක් හම්බවෙනවා ඒවා තමයි,
- while
- do while
- for
- for each
මුලින් ම එහෙනම් අපි බලමු while loop එක කොහොමද වැඩ කරන්නේ කියල
මේකෙදි වෙන්නේ while loop condition එක true නම් code එක execute වෙනවා,
while loop Syntax එක බලමු අපි එහෙනම්,
while (condition is true) {//code to be executed;
}
එහෙනම් ඉතින් මේකත් කරලම බලන්නකෝ
<!DOCTYPE html>
<html>
<body>
<?php
$x = 1;
while($x <= 10) {
echo "The number is: $x <br>";
$x++;
}
?>
</body>
</html>
Result එක එන්නේ,
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
කියල මේකේ වෙන්නේ while loop condition එක true වෙනකම් x ට එකක් එකතු වෙවී ප්රින්ට් කරන එකයි.
ඊට පස්සේ ඉතින් තියෙන්නේ do while loop එක,
මේකෙදි වෙන්නේ code එක එක පාරක් run වෙලා තමයි condition එක true වෙනකම් run වෙන්නේ
Syntax එක එන්නේ මෙන්න මෙහෙමයි,
මේකෙදි වෙන්නේ code එක එක පාරක් run වෙලා තමයි condition එක true වෙනකම් run වෙන්නේ
Syntax එක එන්නේ මෙන්න මෙහෙමයි,
do {//code to be executed;} while (condition is true);
මේකත් කරලම බලන්නකෝ,
<!DOCTYPE html>
<html>
<body>
<?php
$x = 1;
do {
echo "The number is: $x <br>";
$x++;
} while ($x <= 10);
?>
</body>
</html>
Result එක එන්නේ,
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
ඊටපස්සේ එන්නේ for loop එක මේකනම් ගොඩක් ලේසි එකක්
Syntax ,
Syntax ,
for (init counter; test counter; increment counter) {//code to be executed;
}
Parameters:
- init counter : Initialize the loop counter value
- test counter : Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.
- increment counter : Increases the loop counter value
මේක කරලා බලන්නකෝ එහෙනම්,
<!DOCTYPE html>
<html>
<body>
<?php
for ($x = 1; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
?>
</body>
</html>
Result එක එන්නේ,
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
The number is: 1
The number is: 2
The number is: 3
The number is: 4
The number is: 5
The number is: 6
The number is: 7
The number is: 8
The number is: 9
The number is: 10
අන්තිමට අපිට බලන්න තියෙන්නේ for each loop එක ගැන
මේloop එක වැඩ කරන්නේ arrays වලට විතරයි අපි arrays ගැන ඉස්සරහට කරනවා එක නිසා බය වෙන්නෙපා හික් හික් :D
syntax එක බලන්නකෝ එචර අමාරු නෑ ඔලුවට දාගන්න
Result එක එන්නේ,
BMW
AUDI
CAMARO
MUSTANG
arrays කරනකොට මේකේ වෙන දේ තේරෙයි හොදට දැන් තේරුනේ නැත්තම් වැඩිය හිතන්න යන්නෙපා හික් හික් ....
එහෙනම් ඉතින් මම යනවෝ
අයත් අලුත් පොස්ටුවක් අරගෙන එන්නම් එහෙනම් හැමෝටම ජයවේවා!!!
ටටා බායි හික්ස් :p
මේloop එක වැඩ කරන්නේ arrays වලට විතරයි අපි arrays ගැන ඉස්සරහට කරනවා එක නිසා බය වෙන්නෙපා හික් හික් :D
syntax එක බලන්නකෝ එචර අමාරු නෑ ඔලුවට දාගන්න
<!DOCTYPE html>
<html>
<body>
<?php
$cars = array("BMW", "AUDI", "CAMARO", "MUSTANG");
foreach ($cars as $value) {
echo "$value <br>";
}
?>
</body>
</html>
Result එක එන්නේ,
BMW
AUDI
CAMARO
MUSTANG
arrays කරනකොට මේකේ වෙන දේ තේරෙයි හොදට දැන් තේරුනේ නැත්තම් වැඩිය හිතන්න යන්නෙපා හික් හික් ....
එහෙනම් ඉතින් මම යනවෝ
අයත් අලුත් පොස්ටුවක් අරගෙන එන්නම් එහෙනම් හැමෝටම ජයවේවා!!!
ටටා බායි හික්ස් :p
No comments:
Post a Comment