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. function hello($value){
  3. if ($value == 5){ //if value var is equal to 5
  4. return true; //return true to the function
  5. }else{ //otherwise
  6. return false; //return false to the function
  7. } //end of check
  8. } //end of function
  9.  
  10. $number= rand(4,6); //a random number from 4 to 6
  11.  
  12. if (hello($number) == true){ //if the function returns true
  13. echo "Number was 5.";
  14. }else{ //function returned false so...
  15. echo "Number was not equal to 5.<br>Instead it was $number";
  16. }
  17. ?>


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. function lengthcheck($string){
  3. if (strlen($string) > 5){ //if the strings length is more than 5
  4. $var="more than 5";
  5. }else{ //if its less than or equal to 5...
  6. $var="less than or equal to 5";
  7. }
  8. return $var;
  9. }
  10.  
  11. //now time to use the function...
  12. $rand= rand(0,10); //random number from 0 to 10
  13. if (lengthcheck($rand) == "more than 5"){
  14. echo "number is more than 5";
  15. }else{
  16. echo "number is less than 5";
  17. }
  18. ?>


You can also echo stuff with functions...

PHP Code
  1. <?
  2. function makebold($string){
  3. echo "<b>$string</b>";
  4. }
  5.  
  6. echo makebold("hello every body its the big blue bear!");
  7. //we are echoing the function so that the output is printed to us
  8. ?>


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's Avatar
Views:
2,218
Rating:
There are currently no comments for this tutorial, login or register to leave one.