Tag: webdesign

  • PHP LOOPS: For, Foreach & While

    PHP LOOPS: For, Foreach & While

    PHP for beginners

    For loops

    This is a for loop

    <?php
    // FOR LOOP
    for($i = 0; $i<4; $i++) {
    	echo $i;
    }
    ?> 


    Let’s break it down
    $i=0;
    start at the value 0
    $i<4; run the loop as long as $i is less than 4
    $i++ Increment $i by 1 every time you run through the loop
    This loop will run 4 times because $i starts with the value 0, then after the first loop it’s 1, after the second loop 2, then 3 then 4. When $i > 4 the loop will stop.

    This loop will echo the value of $1 for each loop so it will echo “0123” and then it stops.

    For loop: Blogpost example

    Instead of $i<5;
    Try $i <count($blogposts);
    Now it will keep running through each blogpost and stop after post4

    <?php 
    // FOR LOOP
    $blogposts=['post1','post2','post3','post4'];
    for($i = 0; $i <count($blogposts); $i++) {
    	echo $i;
    	echo " --- ";
    	echo $blogposts[$i]." --- ";
    } 
    // This will echo 0 --- post1 --- 1 --- post2 --- 2 --- post3 --- 3 --- post4 ---
    ?> 

    Foreach ($blogposts as $post): So each time we cycle through the array $blogposts, each value in that array is stored in $post (you can call $blog whatever you want, it’s just a variable that stores each value in the array $blogposts). You can then echo out $post; 

    Foreach loop

    How the foreach loop works (look at example below)
    It’s especially used used when you don’t know the number of items in an array
    (you don’t set $i in this loop) .
    Foreach ($blogposts as $post): So each time we cycle through the array $blogposts, each value in that array is stored in $post (you can call $blog whatever you want, it’s just a variable that stores each value in the array $blogposts). You can then echo out $post;

     

    <?php 
    
    $blogposts=['post1','post2','post3','post4'];
    //FOREACH LOOP 
    foreach($blogposts as $post) {
    	echo $post; 
    // This echoes out post1post2post3post4
    // Try echo $post . '<br />'; to add linebreak between each loop. 
    }
    ?> 

    A foreach loop is easier when cycling through an array than the for() loop as you need to write less code. With the for loop you can specify how many loops you want to run, you cannot do that with the foreach loop.

    <?php 
    
    // ASSOCIATIVE ARRAY 
    $products = [
    ['name'=>'Tuna','price'=>10],
    ['name'=>'grapes','price'=>14],
    ['name'=>'advocado','price'=>17],
    ['name'=>'kiwi','price'=>2],
    ['name'=>'beef','price'=>32],
    ['name'=>'salmon','price'=>34]
    ['name'=>'Apples','price'=>12]
    ]; 
    
    //FOREACH LOOP 
    foreach($products as $product) {
    echo $product['name'] . ' - '. $product['price'];
    echo '<br />';
    }
    // ECHOES OUT product name and price of each product 
    
    ?> 

    While loop 

    $i=0 This must be declared outside the while loop function
    count($products) This will be the number 7, because we have 7 products in the array.
    while($i < count($products)) is the same as writing while(0 < 7) so the loop will run 7 times.
    $i++ Means increment $i by one each time the loop runs: you must increment $i inside the loop function or else the loop will never stop (infinite loop) because 7 is always less than zero.

    <?php 
    
    // ASSOCIATIVE ARRAY 
    $products = [
    ['name'=>'Tuna','price'=>10],
    ['name'=>'grapes','price'=>14],
    ['name'=>'advocado','price'=>17],
    ['name'=>'kiwi','price'=>2],
    ['name'=>'beef','price'=>32],
    ['name'=>'salmon','price'=>34]
    ['name'=>'Apples','price'=>12]
    ]; 
    
    
    //WHILE LOOP
    $i = 0; // $i must be delared outside the loop. 
    
    while($i < count($products)) {
    	echo $products[$i]['name'];
    	echo' <br />'; 
    	$i++; // you must increment i or else it's always =0. 
    //count($products) is = 7 (there are 7 products in the array)
    }
    ?>