Jump to content

functions functions and more functions


pthurmond

Recommended Posts

Ok a couple of function questions.
First, can a function you call access the $_POST array within it, without passing it?
Second, when dealing with functions that need to connect to the database or use session info do I need to do the includes and session start inside each function in a file containing multiple functions, or can I just declare those at the top of the file outside the functions and be good.

Examples:
Option 1-
[code]
require_once('connecttodb.php'); //Connect to the db
session_start();

function dosomething()
{
}

function dosomethingelse($val)
{
}
[/code]

Option 2 -
[code]

function dosomething()
{
    require_once('connecttodb.php'); //Connect to the db
    session_start();
}

function dosomethingelse($val)
{
    require_once('connecttodb.php'); //Connect to the db
    session_start();
}
[/code]


Thanks for your help,
-Patrick
Link to comment
https://forums.phpfreaks.com/topic/26960-functions-functions-and-more-functions/
Share on other sites

1) The $_POST is a superglobal array variable so you don't have to pass it to each function.

2) Issue just one session_start() at the beginning (before you send any output to the browser). So Option 1 is the correct approach, although I recommend putting it even before the require_once().


http://us3.php.net/manual/en/reserved.variables.php#reserved.variables.post

http://us3.php.net/manual/en/function.session-start.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.