Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 27th, 2005, 11:06 PM   #1
jstephens
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 jstephens is on a distinguished road
Array and Function Help.

I am new to PHP and in need of some help. My questions are :

1) How often will I use arrays ?
2)When are good times for using Functions ?

I am using O'Reilly's Learning PHP 5 and currently on functions but I am still confused as you can see. Any help would be appreciated.
jstephens is offline   Reply With Quote
Old Oct 28th, 2005, 12:20 AM   #2
crazykid48x
Programmer
 
crazykid48x's Avatar
 
Join Date: Apr 2005
Posts: 96
Rep Power: 4 crazykid48x is on a distinguished road
I know! you'll use arrays often in not only php but in almost all other languages as well. And the best time to use functions is when you have a chunk of code that you repeat several times.

edit: oh yeah, functions are also useful when you want your code to be broken into smaller independant peices. (thats a good habit by the way)
__________________
I had my portable CD player, and took it in the bathroom with me while I went to pee. And the second I whipped my penis out, the theme song to 'Rocky' started playing. I've never felt more manly than in that moment.
crazykid48x is offline   Reply With Quote
Old Oct 28th, 2005, 8:35 AM   #3
Lich
Professional Programmer
 
Lich's Avatar
 
Join Date: May 2005
Location: Detroit
Posts: 254
Rep Power: 4 Lich is on a distinguished road
Send a message via AIM to Lich Send a message via MSN to Lich
My rule of thumb is make a function for any piece of code that you may use more than once.
__________________
--John Cruz
Web Developer
www.cruzweb.net
Lich is offline   Reply With Quote
Old Oct 28th, 2005, 9:13 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
PHP has a propensity to produce a LOT of its data in arrays. There are alternatives (objects, for instance), but arrays are extremely common. After all, that's what a row in a database table essentially IS: an associative array.

A clue to why you might want to write a function lies it it's name: function. Perhaps you never thought of it like that. Connecting to a database is a function. Emitting an HTML table of a certain structure, over and over, is a function. Why mix the two? They're not related. Scrambled eggs are for breakfast, not applications.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 28th, 2005, 3:23 PM   #5
jstephens
Newbie
 
Join Date: Oct 2005
Posts: 12
Rep Power: 0 jstephens is on a distinguished road
So can you place your functions into a seperate page and use an include? All the examples in the book show poor usage of them. I am looking for an actual real world Example of how and when to use them. One of the examples in the book is as follows:

function page_Header($color) {
print "<html><head><title>Welcome to My Site </title></head>";
print "<body bgcolor = " . ' $color ' . ">";
}

This is typed into the same web page as the call to the function. So how would I use this over again.
jstephens is offline   Reply With Quote
Old Oct 28th, 2005, 4:18 PM   #6
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Use [ P H P ] [ / P H P ] tags around PHP code in the future, please...

That's a VERY bad example of functions, since it'll only be called once (and could be simplified).

[php]
<?php
function multiply($n1, $n2) {
return $n1 * $n2;
}

echo "9 * 9 = " . multiply(9, 9) . "<br>\r\n";
?>
[/php]
__________________

tempest is offline   Reply With Quote
Old Oct 29th, 2005, 11:07 PM   #7
Mocker
Hobbyist Programmer
 
Mocker's Avatar
 
Join Date: Oct 2005
Location: Indiana
Posts: 218
Rep Power: 0 Mocker is an unknown quantity at this point
Send a message via AIM to Mocker
Quote:
Originally Posted by jstephens
So can you place your functions into a seperate page and use an include? All the examples in the book show poor usage of them. I am looking for an actual real world Example of how and when to use them. One of the examples in the book is as follows:

function page_Header($color) {
print "<html><head><title>Welcome to My Site </title></head>";
print "<body bgcolor = " . ' $color ' . ">";
}

This is typed into the same web page as the call to the function. So how would I use this over again.

It might be simplified, but say you make one php program to serve up lots of websites (ie index.php?page=home index.php?page=gallery index.php?page=login)

You are going to be duplicating a lot of html output, so you would want all the html that is going to be the same in every page to be in its own function, or set of functions. So everytime you make a webpage you can call

page_Header();
if($_GET['page']){
//decide what content to show based upon the page
}
page_Footer();

this is really basic, but the point is there is a reason the header was put in its own function. You want to keep the different parts of your program defined.
__________________
#programmingforums relay - http://thegupstudio.com/cgi-bin/pforelay.cgi
freelance scripts - http://ryanguthrie.com/index.html
Mocker is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
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
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:12 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC