|
🐘 PHP සින්හලෙන් – 4වැනි එපිසෝඩ් එක 🔄

🐘 PHP සින්හලෙන් – 4වැනි එපිසෝඩ් එක 🔄

technology web development programming
By Rasanjana 2025-05-07 19:58:08

ඔන්න macho! 🤓

අද අපේ PHP සින්හලෙන් series එකේ Lesson 4 එකට awa! 🌀

මේකේ අපි Loops, Arrays, සහ ඒවට practical use case එකක් — HTML Table එකක් හදා balamu! 😍

🔁 Loop එකෙන් Array එකක් dance කරමු!


🧠 Arrays කියන්නේ මොකක්ද?

Array එකක් කියන්නේ 👉 එකම variable එකක name එක යටතේ multiple values තියෙන "container" එකක්.

$fruits = ["Apple", "Banana", "Mango"];


📚 Array වර්ග 2ක් තියෙනව:

  1. Indexed Arrays – position-based
  2. ["Apple", "Banana"]
  3. Associative Arrays – key => value
  4. ["name" => "Chamika", "age" => 25]


🔁 Loop කියන්නේ මොකක්ද?

Loop එක කියන්නේ 👉 එකම task එක repeat කරන හැටි.

PHP වල තියෙන loops:

  • for
  • while
  • foreach


🌀 for Loop – Index-based Array එක loop කරන්න

$fruits = ["🍎 Apple", "🍌 Banana", "🥭 Mango"];

for ($i = 0; $i < count($fruits); $i++) {
  echo $fruits[$i] . "<br>";
}


🔁 while Loop – repeat while condition is true

$counter = 0;

while ($counter < 3) {
  echo "Count: $counter <br>";
  $counter++;
}


🔄 foreach Loop – Arrays වලට best friend ❤️

$colors = ["Red", "Green", "Blue"];

foreach ($colors as $color) {
  echo $color . "<br>";
}


🔑 Associative Array එකක් foreach වලින් පාරමු

$user = [
  "name" => "Chamika",
  "age" => 25,
  "country" => "Sri Lanka"
];

foreach ($user as $key => $value) {
  echo "$key: $value <br>";
}


📋 Practical: Array එකක් Table එකක් වගේ print කරන්න

$students = [
  ["name" => "Kasun", "marks" => 85],
  ["name" => "Nimali", "marks" => 92],
  ["name" => "Amal", "marks" => 78]
];
?>

<table border="1" cellpadding="8">
  <tr>
    <th>👤 Name</th>
    <th>📝 Marks</th>
  </tr>

<?php
foreach ($students as $student) {
  echo "<tr>";
  echo "<td>{$student['name']}</td>";
  echo "<td>{$student['marks']}</td>";
  echo "</tr>";
}
?>
</table>

🔁 Output එකට ලස්සන Table එකක් ගිහිං දිස්වෙයි!

🧾 අද ඉගෙනගත්තෙ:

🔮 Next Lesson එකේ දේවල්:

👉 Functions – function keyword 🧠

👉 Parameters, Return values 🔁

👉 Reusable logic එකක් හදන්න

👉 Form validate කරන real example එකක් 💡


Lesson 5 ekata "function + validation" setup එකක් ready karannada?

🔥 Comment ekak dagena yamu

Rasanjana

Rasanjana

Member since 2025-04-09 13:55:06

Comments

Please login to post a comment.

No comments yet. Be the first to comment!