Web Hosting Deals Holiday Logo Design Webcam Chat Website Header Templates Register domain Search Engine Optimisation Web Hosting
Go Back   Talk Mania Forum > Tutorials > PHP / Perl / Java / JavaScript / CGI Tutorials

PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials.

 Image
Buy Sell Downloads

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-14-2007, 07:42 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
PHP Functions tutorial.

Ok, so i have been a memeber of this forum for a very short time, but what i can't find are any full length tutorials in PHP (forgive me if i am wrong)

So i thought i might write one..

This is my first tut here on talk mania, so be kind!

Right then.

Today we are going to learn about a thing called a function in the scripting language of PHP.

Here are a few things about functions:

A function is a block of code that can be executed whenever we need it.

Creating PHP functions:

All functions start with the word "function()"
Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
Add a "{" - The function code starts after the opening curly brace
Insert the function code
Add a "}" - The function is finished by a closing curly brace

ok!


So lets start now with a function for echoing todays date!

PHP Code:
<?php
function thedate()
{
echo 
date("d/m/Y");
}
?>
So there we go!

That is our first ever function...

Let's just explain that a little..

function thedatedate() - tells the server that your creating a function called thedate

{ -Tells the server to start accepting the code for the function.

echo date("d/m/Y"); - Using the PHP date function just to get todays date seperated by a forward slash.

} - Tells the script that the function is finished.

OK!

So there we go thats a function for us..

So now we want to echo our date somwhere on our page..

Here is an example of a HTML page, with todays date in it.

Code:
<table width="500" border="2" cellspacing="1" cellpadding="0">
  <tr>
    <td>News added by: Base </td>
  </tr>
  <tr>
    <td><p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus sapien   nunc, varius ut, bibendum ac, porttitor eu, magna. Vivamus elit mauris, gravida   id, congue non, elementum eleifend, augue. Integer iaculis mauris non felis.   Morbi mi magna, ullamcorper nec, aliquam id, pellentesque sed, lorem. Curabitur   convallis fermentum augue. Donec semper iaculis est. Donec nibh. Suspendisse   semper odio id lorem pulvinar vestibulum. Suspendisse ut leo. Integer iaculis.   Sed sagittis lorem eget massa. Suspendisse vulputate mattis sem. Maecenas   elementum, libero ut consectetuer pellentesque, nibh nisl varius turpis, sit   amet accumsan libero neque in sem. </p></td>
  </tr>
  <tr>
    <td><div align="right">Date Added: <?php thedate(); ?>
</div></td>
  </tr>
</table>
The only thing you really need to look @ in this code is this line:
PHP Code:
<?php thedate(); ?>
That line just parses the contents of the function called thedate()

Well hope you learned somthing about functions today fokes!

my apologies for any code faults...

Will be using functions in my next tut to do with some really simple mysql stuff!


Summing up todays lesson:


To start off a funtion in php you do this:

function TheNameOfYourFunctionHere()
{ - This thing tells the server your starting off your function
Your function code goes here
} - This thing tells the server your finishing off your function

To display your function at any point you just do this:

TheNameOfYourFunctionHere();

THINGS TO REMEMBER.

You must use { & } to tell the server your starting and ending your function.

If you save your function in a seperate file (e.g. functions.php) then you HAVE to remmeber to do this tag:

<?php include("your-file.php"); ?>

More on that later!


Chow peeps

Base


Last edited by Base; 03-28-2007 at 07:02 AM.
Reply With Quote
  #2 (permalink)  
Old 03-28-2007, 01:13 AM
Junior Member
 
Join Date: Mar 2007
Posts: 25
Willyke is on a distinguished road
Please note that you need to pick the name of the function wisely because in your example you will probably get an error (or warning) because date() already exists in PHP itself.
---
Quote:
A function is a block of code that can be executed whenever we need it.

Creating PHP functions:
All functions start with the word "function()"
Name the function - It should be possible to understand what the function does by its name. The name can start with a letter or underscore (not a number)
Add a "{" - The function code starts after the opening curly brace
Insert the function code
Add a "}" - The function is finished by a closing curly brace
Please refer to your source for copied parts next time ^^ [source]
---
Maybe you can extend this howto by talking about functions with arguments?
---
And one more thing is that you actually tell the same thing 3 times...But then again it is well written
Reply With Quote
  #3 (permalink)  
Old 03-28-2007, 07:00 AM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
Quote:
Originally Posted by Willyke View Post
Please note that you need to pick the name of the function wisely because in your example you will probably get an error (or warning) because date() already exists in PHP itself.
---

Please refer to your source for copied parts next time ^^ [source]
---
Maybe you can extend this howto by talking about functions with arguments?
---
And one more thing is that you actually tell the same thing 3 times...But then again it is well written
Indeed, i realise that now.

My original tutorial involved using the names of functions with _name so this problem never arised, it was only since i stopped using the under score infront of the name in the function in late 2006 (Because a client got confused while eding the script, and it took me 3 days to recode it) so my apologies for this, and anyone who may have been misled

The source was indeed coped from w3schools i respect their website & their tutorials.

I use code from them wherever possible, and i refer all those who wish to learn to code, so when they see my tutorial, and then see the same code on W3, they can remember the fact that i wrote said tutorial, and return to see if there are any more (my logic is undeniable)...

I would like to point out here, that it was only since i started posting tutorials at talk mania, that i started using source from other websites.

I have no wish to extend this tutorial to cover functions with arguments at the moment, this is because i am doing a step by step tutorial now, which starts off at the very baisic level, and ends with writing full blown scripts.

In these tutorials we will effectively be building a CMS, and as i use new functions (or write them) they learn what they need to know.

So if i use functions with arugments, i will explain how to use them, and what they are.


You point out that i said somthing 3 times, i belive you are refering to the line "You must use { & } to tell the server your starting and ending your function."

Yes, your right i do use this line 3 times, this is only because when i started learning PHP, syntaxes were the main thing i had a problem with, so you will see me repeating the syntaxes in most if not all of the tutorials i write.

By the tone of your reply, i can only assume that you are under the impression that i don't understand the language, or don't know it.

I can assure you that this is not the case. Having written 1500 seperate scripts in the last 2 years, my skills are not to be doubted, and certainly should not by you.

I do hope that you did not mean your reply in a spitefull way.

Yours most sincerely

Thomas Gray

Customer Services, General Coding & Sales manager - Pure Host UK
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.

Last edited by Base; 03-28-2007 at 07:10 AM. Reason: Spotted spelling errors
Reply With Quote
  #4 (permalink)  
Old 03-28-2007, 11:47 AM
Junior Member
 
Join Date: Mar 2007
Posts: 25
Willyke is on a distinguished road
Quote:
Originally Posted by Base View Post
By the tone of your reply, i can only assume that you are under the impression that i don't understand the language, or don't know it.

I can assure you that this is not the case. Having written 1500 seperate scripts in the last 2 years, my skills are not to be doubted, and certainly should not by you.
Sorry that you thought that of my reply, my main language is dutch and I sometimes say things wrong in English.
Reply With Quote
  #5 (permalink)  
Old 03-28-2007, 02:23 PM
Base's Avatar
Senior Member
 
Join Date: Mar 2007
Location: North Yorkshire, UK
Posts: 121
Base is on a distinguished road
Send a message via MSN to Base Send a message via Skype™ to Base
It's all OK, no harm done, i was a little offended you see

Hope you liked my tutorial tho!

Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png
PointServ.co.uk - One of the UK's cheapest webhosting companies.
Reply With Quote
  #6 (permalink)  
Old 03-29-2007, 03:16 AM
Junior Member
 
Join Date: Mar 2007
Posts: 15
daoner is on a distinguished road
This helped me alot. Thank you.
Reply With Quote
  #7 (permalink)  
Old 03-29-2007, 06:26 AM
Junior Member
 
Join Date: Mar 2007
Posts: 10
ArKiVe is on a distinguished road
Looks good
Reply With Quote
  #8 (permalink)  
Old 04-04-2007, 07:03 PM
Junior Member
 
Join Date: Mar 2007
Posts: 20
kraze101 is on a distinguished road
time to write some functions. thank you
Reply With Quote
  #9 (permalink)  
Old 04-06-2007, 04:11 PM
Junior Member
 
Join Date: Apr 2007
Posts: 10
Zika is on a distinguished road
really nice - i needed
Reply With Quote
  #10 (permalink)  
Old 04-25-2007, 02:23 PM
Junior Member
 
Join Date: Apr 2007
Posts: 4
Ultimate` is on a distinguished road
wow.. thanks lol :P im so stupid, never knew how the heck functions worked lmao.. now i know :P made my own logging function and date one

Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT +2. The time now is 02:31 AM.


Fake ID

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC8
Forums Copyright © Talk-Mania.com