|
| Register now to interact with over 11,000 members! Registered users have Posting Privileges, free access to Private Messaging, Email Notifications and more. |
|
|||||||
![]() |
|
|
Thread Tools |
|
|
#1 |
|
Guest
Posts: n/a
|
Use of functions
I wonder if anyone can give a short, and hopefully, a helpfull description of how the
PHP Code:
I have read the php.net sites... but for a newbi it is hard to follow... and the nice examples are in many cases only codes for the function, and no description how they work and what parameters to fill... I have timescript that I would like to make a function so that I can call it in when needed... Questions are: "Can I just wrap the PHP Code:
How to call it. Can I use just a include /my_function.php and later on call it by my_function() Will a function used twice in a document cause errors? or can you end a function? Last edited by Storms; 5-16-03 at 04:42 AM.. |
|
|
#2 |
|
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 7,266
Reputation: 340
|
You define a function like this
function foo ($arg_1, $arg_2, ..., $arg_n) { echo "Example function.\n"; return $retval; } If you want to use it in other scripts save it as a php file and use require( functions.php ) at the start of your scripts to include it in their code. When you use the function just type foo( "hi" ). Hope this helps a little bit. You may want to get the php documentation from php.net HalfaBee
__________________
I don't suffer from laziness, I enjoy every minute! Edit your php.ini here http://members.powweb.com/member/cgi...nt/PHPplus.bml |
|
|
|
|
#3 |
|
Guest
Posts: n/a
|
Here is the script that I would like to put into a function and call it up later...
------------------- PHP Code:
Can I put all the code inside { } tags?.. Will the use of functions inside the functions create conflicts? function timescript($arg_1, $arg_2, ..., $arg_n) { echo "Example function.\n"; return $retval; } and how can i call it.. to echo out the results? |
|
|
#4 |
|
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 7,266
Reputation: 340
|
If you just want to ouptut what happens in the above code, just include( script.php ) where you want it to happen.
If you need to do it more than once per page a little rewrite will be needed. HalfaBee
__________________
I don't suffer from laziness, I enjoy every minute! Edit your php.ini here http://members.powweb.com/member/cgi...nt/PHPplus.bml |
|
|
|
|
#5 |
|
Guest
Posts: n/a
|
more than once
Yes.. I want to use it more than once in a some pages and often use the output at bit different... (different combinations... etc..)
I know I'll manage to rewrite the code to suit my needs by changing variables etc.... but that will be a pain to do all the time...( You know copy the code into a document.. alter it etc..) If I can use the $var defined in the script by include(script.php) then it will help a lot, but this is all new territory... Actually I wouldn't wrap all of the script in as I like to use different $vars in the script different places... ex... In one doc i would use the script to close a url fetch at a certain hour and on certain days.... the script has all these features... I'll only need to put it infront of the script fetching and echoing... I'll then just put the fetch part within the if setting for open... This also need to be done for some other options at the same page... That's why I thought perhaps look if it was possible to use a "function" or something.. $newvars = "22.00"; my_script($newvars1...2..3 etc...) This would be a better way than pasting the script all the time in the page. If I use include(script.php) Can I then put the $vars outside the script so that the readings are different each time? Hope the questions are understandable.... |
|
|
#6 |
|
foo
Join Date: Jan 2003
Location: Seattle-ish
Posts: 2,597
Reputation: 106
|
You could go either way. Either way, you just have to have your key that runs the whole thing be a variable. If you want to do it as an include, you would simply defie the variable before you include it.
eg: if your script is all based around $my_var, then you would do: $my_var = "tacos"; include("my_script.php"); //buncho stuff here... //then you need to access it again... $my_var = "burritos"; include("my_script.php"); Anyway, someone get me a taco. I'm hungry.
__________________
webhead.cc |
|
|
|
|
#7 |
|
Guest
Posts: n/a
|
Think I'll try out the include(script.php)
Thx pig and halfaBee... I'll fill you in as soon as I have some progress to report ![]() |
![]() |
| Thread Tools | |
|
|