Forgot Password / Register
Site Statistics
Total Members: 510
Total Tutorials: 242
Newsest User: Hp
Todays Unique Hits: 129
0 Users 2 Guests Online

How to use functions

What are functions?
Well, functions are little snippets of codes and checks that can be run in php scripts. They are also commonly used to process values or strings.

Example: ucwords
The ucwords function gets a string and makes the first letter of every string capital (obviously depending on whether that value is alphabetical or numeric).

How are functions used?
Here is a small and simple function...
PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?
function hello($value){
if (
$value == 5){ //if value var is equal to 5
return true//return true to the function
}else{ //otherwise
return false//return false to the function
//end of check
//end of function

$numberrand(4,6); //a random number from 4 to 6

if (hello($number) == true){ //if the function returns true
echo "Number was 5.";
}else{ 
//function returned false so...
echo "Number was not equal to 5.<br>Instead it was $number";
}
?>


The above script shows both what a function looks like and how it could be used.

Where are functions used?
Functions are used ALOT. A simple is_int($string) function will tell the user if a string is a number or not by returning either false or true.

But does the function NEED to return true and false? Can it not return something else?
Yes of course it can. You can also return other stuff. Heres an example...

PHP Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?
function lengthcheck($string){
if (
strlen($string) > 5){ //if the strings length is more than 5
$var="more than 5";
}else{ 
//if its less than or equal to 5...
$var="less than or equal to 5";
}
return 
$var;
}

//now time to use the function...
$randrand(0,10); //random number from 0 to 10
if (lengthcheck($rand) == "more than 5"){
echo 
"number is more than 5";
}else{
echo 
"number is less than 5";
}
?>


You can also echo stuff with functions...

PHP Code
1
2
3
4
5
6
7
8
9
<?
function makebold($string){
echo 
"<b>$string</b>";
}

echo 
makebold("hello every body its the big blue bear!");
//we are echoing the function so that the output is printed to us
?>


Theres almost limitless stuff u can do with functions. whole websites can consist of just functions and a few lines of code to execute those functions believe it or not.

Why would i use functions?
Why would you use functions? Well, PHP can't offer a developer EVERYTHING. It already gives us some cool functions like mysql_real_escape_string or htmlentities which are really helpful security wise. But developers sometimes want to check for other stuff and find that PHP doesnt give them the right functions.

For example, I want a function which lets me uppercase a whole sentence and at the same time make it bold. PHP doesnt have this kind of function. So i would have to make it myself right? Correct.

Anyway thats the end of the tutorial. If you have any questions feel free to ask.
ilyas-shezad
Views:
1320
Rating:
There are currently no comments for this tutorial.