Custom Functions for the win

atomichomeboy's picture

Hello all

Today I would like to share some information on how custom PHP functions will help you boost development speed by allowing you to quickly replicate your website code.

As a "coder" (:P) the one thing that you don't want to be doing is writing your code twice or more on the same page. you can avoid this by creating custom functions in your PHP code.

Lets say you have a table in your site that contains an image and text in it and you want this table replicated 3 or 4 or 6 times in the body of your web page

you could write
echo "<table><TR><TD><img src=x><TD>TEXT</TR></table><br>";
Four times into your code

or you could create a function that will write it for you when called
eg.
function BuildTable($img, $txt) {
echo "<table><TR><TD><img src=".$img."><TD>".$txt."</TR></table><br>";
}

note that the image source and text values have been changed to variables and correspond to the values in between the brackets() at the beginning of the function

now to call your function (build your table) you simply need to type the following php code and repeat as necessary to get the desired number of tables.

BuildTable('x', 'TEXT'); * replacing the $values with the values you want in the table.

examples::
table with image named monkey and text that says Monkeys rule.. Then a table with starwars image and vader text

BuildTable('monkey', 'Monkeys rule');
BuildTable('starwars', 'vader');

** functions can only be called if they are created -- you must create the table once to have it replicated.
this was an easy demonstration but functions can be used for large elaborate tables that have similar formatting but different values in them or even for building the headers of your website

I hope this helps

Very interesting

DerrickH's picture

Very interesting information, although I have to admit, it's a little above me! I'm sure there are plenty of others who can apply the info though.

Thanks for sharing your knowledge.

Derrick.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The way to get started is to quit talking and begin doing. - Walt Disney

http://twitter.com/derrickh

Syndicate content