Mada za sehemu hiiCreating PHP programsMada 4
- Applying basic PHP syntax
- Implementing conditional statements, loops and arrays
- Implementing functions
- Integrating PHP with JavaScript and event handling
Control Structures and Arrays in PHP
Control structures and arrays are essential tools in programming that allow you to make decisions, repeat actions, and store collections of data. In PHP, these structures help you write dynamic programs that can respond to different conditions and process multiple values efficiently.
Conditional statements allow your program to make decisions based on certain conditions. The program executes different code depending on whether a condition is true or false.
The if-else Statement
The if statement checks a condition. If the condition is true, the code inside the block runs. If false, the program either skips the block or executes the else part.
if (condition) {
// code runs if condition is true
} else {
// code runs if condition is false
}
The switch Statement
When you need to compare one variable against many possible values, the switch statement is cleaner than multiple if-else statements.
switch (variable) {
case value1:
// code for value1
break;
case value2:
// code for value2
break;
default:
// code if no case matches
}
Loops allow you to repeat a block of code multiple times without writing it again. This is useful when processing many items or performing an action until a condition changes.
The for Loop
Use a for loop when you know exactly how many times to repeat the code.
for (initialization; condition; increment) {
// code to repeat
}
The while Loop
Use a while loop when you don't know in advance how many times to repeat — the loop continues as long as the condition remains true.
while (condition) {
// code to repeat
}
An array is a variable that can hold multiple values at once. Instead of creating separate variables for each item, you can store related data together in one array.
Creating an Array
$items = array("book", "pen", "notebook");
// or using short syntax
$prices = [500, 200, 1000];
Accessing Array Values
Array values are accessed using an index number, starting from 0.
echo $items[0]; // outputs: book
echo $prices[2]; // outputs: 1000
Using Loops with Arrays
You can combine loops with arrays to process each item:
for ($i = 0; $i < 3; $i++) {
echo $items[$i] . "<br>";
}
Consider a simple program that calculates a student's grade based on marks scored:
$marks = 78;
if ($marks >= 90) {
$grade = "A";
} elseif ($marks >= 75) {
$grade = "B";
} elseif ($marks >= 60) {
$grade = "C";
} else {
$grade = "D";
}
echo "Grade: " . $grade;
Output:
Grade: B
Now, imagine a small shop in Dar es Salaam that tracks inventory using an array:
$products = ["Sugar", "Rice", "Oil", "Soap"];
$prices = [3000, 5500, 4500, 2000];
echo "<h3>Shop Inventory</h3>";
for ($i = 0; $i < 4; $i++) {
echo $products[$i] . " - TZS " . $prices[$i] . "<br>";
}
This displays all products and their prices in one loop, making it easy to manage and display inventory data.
A market vendor in Kariakoo or a small shop owner in Arusha can use PHP scripts with conditional statements, loops, and arrays to manage their daily sales. For example, a vendor can store product names and prices in arrays, use a loop to display all items on a simple webpage, and apply conditional logic to apply discounts — such as giving a 10% discount if a customer buys more than five items of the same product, helping the business run more efficiently and serve customers faster.
Jinsi utakavyotathminiwaTathmini ya mchakato · Tathmini ya matokeo · Tathmini ya maarifa
Tathmini ya mchakato
- Write scripts using conditional logic.
- Create loops for repetitive tasks.
- Use arrays to store and access multidimensional data.
Tathmini ya matokeo
Programs performing decision-making (e.g., grading system), loops (e.g., summing numbers), and array manipulation.
Tathmini ya maarifa
Detailed knowledge of:
Method used: The student should explain how
- PHP handles logic, iteration, and data storage in memory.
Principles: The student should explain the principles of:
- control structures for decision-making and data iteration.
Theories: The student should explain:
- algorithmic flow control (decision trees, loop invariants) in PHP
- memory management for arrays in PHP.
Circumstantial knowledge:
Detailed knowledge about: Safety precautions in electronic work.
- Debugging and handling errors in scripts with complex logic.
Swali
What is the main purpose of a conditional statement in PHP?
Ingia ili kuwasilisha jibu lako na lihesabiwe katika umahiri wako.
Ingia ili kufanya mazoeziMwalimu
Umekwama? Niulize chochote kuhusu mada hii.
Ingia ili kumuuliza Mwalimu wa AI wa Sonza kuhusu swali hili.
Ingia ili kuulizaMajadiliano
Hakuna maswali bado