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;


?>

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *