Networking, Programming and Graphics - Tutorials
ONLINEHOWTO.net Tutorials Category

Working with PHP functions

Type: Code Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Level: Beginner Networking, Programming and Graphics - Tutorials 
Networking, Programming and Graphics - Tutorials
Date: 2006-Dec-04
Networking, Programming and Graphics - Tutorials
Visited: 3770 times
Networking, Programming and Graphics - Tutorials
Rating: Networking, Programming and Graphics - Tutorials
Networking, Programming and Graphics - Tutorials
Published: Ivory Morhuld

Many times we need to use code to do one thing with different input values. If put this code at each place where we need this funtionality files in our PHP project will grow up with more lines of code. On this we say code mess.

For this we have to use PHP Functions.
First of all we need to declarate this function. Once this function is declarated we can use it many times. This function its good to be placed outside the main code, in external PHP file. You can name it â€" functions.php or something similar.

Declaration of functions is announce of his name, the count of input values, and their mandatory. In our example $var and $var2 is mandatory, $varN is not. This is because at the time of declaration we announced this var with $varN="".

The end resut of each function we can see by making return statemane. This can be true or false, the sum of ($var + $var2) or something other what we need.
<?php
function my_func ($var1$var2$varN="") {
    if ((
$var1>$var2) or (strlen($varN)<10)) {
        return 
true;
    } else {
        return 
false;
    }
}
?>
You have to know that next in your main PHP code files you should use include(“functions.php”) to use functions declarated in it.
<?php
include("functions.php");

$min_age=13;
$age=$_POST['age'];

$is_ok=my_func($age$min_age);

echo 
$is_ok;
?>
Rate this tutorial:                    
Post Comment

Need a specific tutorial? Do not hesitate and submit a request!
Your e-mail: