PowWeb Forums - The Perfect Community for the Perfect Host  

Register now to interact with over 11,000 members! Registered users have Posting Privileges, free access to Private Messaging, Email Notifications and more.

Go Back   PowWeb Community Forums > The PowWeb Platform > PHP
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Closed Thread
 
Thread Tools
Old 5-16-03, 04:37 AM   #1
Storms
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:
function 
works...

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:
My function(){....} 
around it all?

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..
 
Old 5-16-03, 05:57 AM   #2
HalfaBee
 
HalfaBee's Avatar
 
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
HalfaBee is offline  
Old 5-16-03, 07:07 AM   #3
Storms
Guest
 
Posts: n/a
Here is the script that I would like to put into a function and call it up later...

-------------------
PHP Code:
----------------------------------------
<?
// Function to convert to correct timezone
function zonechange ($current$target) {
$current = -$current;
$zonedate mktime(date('G'), date('i'), date('s'), date('n'),
date('j'), date('Y'), 1) + (($current $target) * 3600);
return 
$zonedate;
}

/*****************************************
// Times, days and dates not to be counted
/*****************************************/

// Opening hours //
$openhours '00:01:00';
$closehours '23:44:00';

// Days in week that you are closed, sample to be found at php.net under "date"//
$nodays = array();
$nodays[0] = "0"//equals sunday
$nodays[1] = "3"//equals wedensday

// Dates that you are closed //
$nodates = array();
$nodates[0] = "09.5.2003";
$nodates[1] = "18.5.2003";
$nodates[2] = "25.5.2003";
$nodates[3] = "12.5.2003";

// Your messages/actions //
$closed "closed";
$open "open";

/*********************************
// Definitions
/**********************************/
$arrDay = array ("Søndag""Mandag""Tirsdag""Onsdag""Torsdag""Fredag","Lørdag");

$nowhours gmdate('H:i:s', (time() + 7200)); // presenting a 24 hour format e.g 24:00

$today getdate(zonechange(-72)); // correcting timezone
$minutes $today['minutes'];
$hours $today['hours'];
$mon $today['mon'];
$mday $today['mday'];
$year $today['year'];
$wday $today['wday'];
$nodato "$mday.$mon.$year";
$output "";

// testing against days
foreach( $nodays as $rd )
{
list( 
$nodays ) = $rd;
    if( 
$rd == $today['wday'])
    {
    
$output $closed;
     break;
// closed today don't check anything else
    
}
    }    
foreach( 
$nodates as $nd )
{
   
    
// Testing against dates

    
list( $nodates ) = $nd;
  if( (
$nodato == $nd))
  
// && ($today['mday']==$mday)
  // && ($today['year']==$year) )
    
{
        
$output $closed;
        break;
    }
}
// Testing against timesettings
if( $output=="" )
{
    list( 
$o_hr,$o_min,$o_sec ) = explode':' $openhours ); // removing the : sign
    
list( $c_hr,$c_min,$c_sec ) = explode':' $closehours );
    
$openfrom $o_hr*3600+$o_min*60+$o_sec// converting to seconds
    
$nowclosed $c_hr*3600+$c_min*60+$c_sec;
    
$now $today['hours']*3600+$today['minutes']*60+$today['seconds']; // converting to seconds
    
if( ($now>=$openfrom) && ($now<=$nowclosed) )
    {
    
$timeleft $nowclosed $now;
    
    
// converting seconds to hours and minutes
    
$dd=floor($timeleft 86400) ;
    if (
$dd<=9$dd="0".$dd ;
    
$timeleft=$timeleft 86400 ;
    
$hh=floor($timeleft/3600) ;
    if (
$hh<=9$hh="0".$hh ;
    
$timeleft=$timeleft 3600 ;
    
$mm=floor($timeleft/60) ;
    if (
$mm<=9$mm="0".$mm ;
    
$timeleft=$timeleft 60 ;
    
$ss=$timeleft ;
    if (
$ss<=9$ss="0".$ss ;

$retval="$hh:$mm:$ss" ;

/*******************************
// Output results etc..
/******************************/

        
$output .= "$open time left =".$retval;
    }

    else
        
$output "$closed" ;
}

echo 
"Today is: " .$arrDay[date(w)]."  $mday.$mon.$year - $nowhours --- <br>";
echo 
"<br><br>resultS: $output ";

?>
--------------------------------------

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?
 
Old 5-16-03, 08:20 AM   #4
HalfaBee
 
HalfaBee's Avatar
 
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
HalfaBee is offline  
Old 5-16-03, 09:00 AM   #5
Storms
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....
 
Old 5-16-03, 09:36 AM   #6
Pig
foo
 
Pig's Avatar
 
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
Pig is offline  
Old 5-16-03, 11:50 AM   #7
Storms
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
 
Closed Thread

Thread Tools

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 Off
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 07:08 AM.


Contents ©PowWeb, Inc. ~ vBulletin, Copyright © 2000-2007 Jelsoft Enterprises Limited.