Category: PHP

  • PHP FUNCTIONS

    PHP FUNCTIONS

    Here is an easy-to-follow way to learn php functions. you always start the function by typing function followed by the name you want to give the function e.g. function =MyFunction() and then you enter the code you want the function to execute inside the curleybrackets{ Enter function code here}

    <?php 
    
    // FIRST YOU DEFINE A FUNCTION 
    function MyFunction() {
    	echo "Hello World!";
    }
    
    // THEN YOU "CALL" THE FUNCTION TO RUN IT 
    MyFunction();
    
    // CREATE A FUNCTION THAT TAKES IN AN ARGUMENT
    function MyFunction2($product) { //$products is a argument. 
    	echo "The price for $product is $1"; // use the argument inside echo.
    }
    //CALL THE FUNCTION 
    MyFunction2('Advocado'); // When you run the function pass in a value of your choice e.g "advocado" as argument. The function then Echoes "The price for Advocado is $1";
    # MyFunction2(); // echoes Error, beucase it expects an Argument, but you can create a default for the argument 
    
    
    //CREATE A FUNCTION THAT TAKES 2 ARGUMENTS 
    function TwoArguments1($option1,$option2) { //Two arguments
    	echo "Option1: $option1 and Option2: $option2"; 
    }
    
    //CALL THE FUNCTION
    TwoArguments1('Blue',"red"); //This wil echo Option 1: Blue and Option 2: red
    
    
    
    //CREATE DEFAULT VALUE FOR ARGUMENT $PRODICTS - IN CASE NO ARGUMENT IS PASSED INTO FUNCTION
    
    function MyFunction3($products = "Advocado") {
    	echo "The price for $products is $1" ;
    }
    
    MyFunction3(); // It echoes "The price for Advoacdo is $1" ; 
    // Advocado is the default value. If you didn't have a default value, it would give an "error message". 
    
    
    // WRITE FUNCTION THAT TAKES IN ASSOCIATIVE ARRAY
    function ChangeItem($item) {
    	echo "The {$item['name']} is free"; // if a variable is an array it must be placed {} curley brackets when echoing it. 
    	echo "The {$item['name']} is {$item['color']} <br />"; // you need to pt {$variable['name']} inside curley brackets for the echo function if you want to pull out an index [0] or value ['name'] from a $variable - the value represents an item in an array. 
    	}
    
    // CALL THE FUNCTION: PASS IN AN ASSOCIATIVE ARRAY TO THE ABOVE FUNCTION. 
    ChangeItem(['name' => 'Balloon','color'=> 'blue']);
    
    
    
    
    // WRITE A FUNCTION THAT TAKES IN 2 ASSOCIATIVE ARRAYS AS ARGUMENTS 
    function TwoArrays($itemX = 0, $properties = "no property selected") {
        echo "<br /> Item name: {$itemX['name']} and Item Price: $ {$itemX['price']}";
        echo "<br /> Color: {$properties['color']} and Weight: {$properties['weight']}";
    }
    // CALL THE FUNCTION. 
    TwoArrays(
        ['name' => 'Kiwi', 'price' => '20'],
        ['color' => 'Blue', 'weight' => '20']
    // EACH ARRAY MUST BE PLACED IN [] e.g. function([array1],[array2]);
    );
    
    
    
    
    // USING RETURN IN FUNCTIONS
    function ChangeItem2($item2) {
    return "The {$item2['name']} is {$item2['color']} <br />";
    // This returns the value so you can store it in a variable when calling the function.  
    } 
    $Returned_value = ChangeItem2(['name' => 'Ballon','color'=> 'red']); 
    echo $Returned_value; 
    
    
    
    
    
    //FUNCTION WITH 2 ARGUMENTS ( 2 ARRAYS as arguments in this example)
    function TwoArguments($height = 0, $width = 0) {
    return "Height: {$height['height']} and Width: {$width['width']}";
    }
    
    $Returned_value2 = TwoArguments(['height'=> '10'],['width'=> '12']);
    echo $Returned_value2;
    
    
    ?>
    
    
  • 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)
    }
    ?>