Create your own Functions 

1. An Introduction to Functions


A function is just a piece of code,that you want to use not once but again and again. Functions save you from writing the code again and again. A function always return a value.



2. Variable scope and functions


 


function function_name( )


{


}



3. Functions and Arguments


You can pass variable to your functions by typing them inside of the round brackets of the function name.


<?


function myFunction($variable1,$variable2)


{


    echo $variable1;


}


// Using Function


$variable1="Some Text";


$variable2=100;


myFunction($variable1,$variable2);


?>



4. Getting values out of functions


Getting some calculated value from function.


Get a discout value.


<?


$spent_money = 150;
echo calculate($spent_money);



function calculate($spent_money)


{
    $discount = 0.1;


    if ($spent_money > 100)


    {
        $discount_total = $spent_money - ($spent_money * $discount);
        $discount = $discount_total;
    }
    else


    {
        $discount = "No discount";
    }
    return $discount;
}


?>



5. HTTP Header() Function


<?
header("Location:
http://www.visualbuilder.com/");
?>



6. The INCLUDE( ) Function
<? include "somefile.txt" ; ?>

                    

Copyright © 2013 VisualBuilder. All rights reserved