![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#2 |
|
Programmer
Join Date: Apr 2005
Posts: 96
Rep Power: 4
![]() |
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. |
|
|
|
|
|
#3 |
|
Professional Programmer
|
My rule of thumb is make a function for any piece of code that you may use more than once.
|
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2005
Posts: 12
Rep Power: 0
![]() |
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. |
|
|
|
|
|
#6 |
|
Programming Guru
![]() |
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]
__________________
|
|
|
|
|
|
#7 | |
|
Hobbyist Programmer
|
Quote:
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|