View Single Post
  #18 (permalink)  
Old 03-14-2009, 11:53 AM
grace55 grace55 is offline
Administrator
Site Admin
 
Join Date: Jun 2008
Posts: 342
Default A journey leads to php....

What’s your FUNCTION?

It feels like everything in PHP is a function, yes? I mean, it functions, right? Well, this definition of “functions” is a little bit different.

A FUNCTION in PHP is really quite easy to understand -
It is a chunk of code (like a “snippet”, if you are familiar with that term) that can be named and reused at any time. Remember earlier when we were talking about repetitive tasks? Functions can help us further reduce these time-killers by writing a block of code once, then defining it as a function. Kinda like a great big variable. Here’s how it works:

<?php
function MyFunctionName(){
//define your function here
}
?>


Functions are quite cool, because they can be considered building blocks of a web application. Let’s say you want every page of a website to say your company name.
Let’s create a function to do so. Let’s call it myCompanyName.

<?php
function myCompanyName(){
}
?>


Now, let’s add the code we want to execute between the curly brackets:

<?php
function myCompanyName(){
echo "Welcome to The Creamy Bagel Company!<br />";
}
?>


So now whenever we want our company name to appear, you can call the function within a PHP tag anywhere!

<?php
function myCompanyName(){
echo "Welcome to The Creamy Bagel Company!<br />";
}
echo “Isn’t it about time you experienced a better bagel?<br />”;
myCompanyName();
?>


Output looks like this...

A Function can contain just about any type of PHP code, so I hope you can see how useful this can be - it can definitely be a timesaver when working with a lot of code.
__________________
Christian Prayer Forum
Reply With Quote