Personal portfolio and blog
Introduction to PHP Custom Functions
This tutorial is intended to be a simple easy to understand introduction to the custom functions php allows you to create.
Please note: This function is not intended to be useful nor should it be used. It is solely for learning purposes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | <?php $var = 'Hello Im a variable.'; function makeBold( $text ) //The variable inside our makeBold fucntion $text, does not have to be a variable called //$text when we run the function. It is just showing you where a variable or string should //go so that it can be used inside the function. { $var = '<b>' . $text . '</b>'; //Here we just set a variable $var inside our function, notice we already //have a variable called var in the first line of out code? it will not overwrite it. //variables made inside the function are used only inside the function that they were. //made in. return $var; //return $var is the last part to this function and will output whatever $var may be to the page //once the function has been used within it. } $string = 'Here I have a different variable name and I would like the string it carries to be bold'; makeBold( $string ); //Now this is where you may not understand it. When we created our function the variable //inside the brackets said $text and weve just put a variable called $string inside there! //Do not fear this was intentional, I promise. The $text variable in the brackets when we created our //function was only meant to show where the variable or string would be going once we put //our function to use. echo '<br />';//dont want out text all bunched up now do we :) makeBold( 'I would like to be bold please' ); //you can also enter just a string inside the brackets of our function //the string will replace the variable $text when we created our function //if that helps you understand it better. echo '<br />';//dont want out text all bunched up now do we :) echo $var; //Finally we echo the variable we created before we created the function just to show that //a variable with the same name inside the function will not overwrite it. ?> |
Now if you read all the comments I made within the code you should hopefully have a better grasp of what a function does and how it works.
conclusion
A simple function like this would be used to repeat a simple task or piece of code over and over without having to rewrite the code over and over again.
e.g.:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <?php $var = 10; $var2 = 11; $var3 = 12; $var = $var / 5 + 1 * 2; echo $var.'<br />'; $var2 = $var2 / 5 + 1 * 2; echo $var.'<br />'; $var2 = $var2 / 5 + 1 * 2; echo $var.'<br />'; ?> |
This piece of code requires you to redo the same calculations over and over which makes your code look messy and it is inefficient.
1 2 3 4 5 6 7 8 9 | <?php function math( $var ){ $var = $var / 5 + 1 * 2; return $var.'<br />'; } math( '10' ); math( '11' ); math( '12' ); ?> |
Now on a small scale like this it may not seem like a massive difference but on a large scale it makes a massive difference and greatly reduces your code.
If you liked or found this helpful please comment or if you didn’t please comment and ask why and maybe I can help
| Print article | This entry was posted by Callum on July 6, 2010 at 2:50 PM, and is filed under PHP / MySQL, Web Development. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
