Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

quick function question (newbie) 2

Status
Not open for further replies.

krappleby025

Programmer
Joined
Sep 6, 2001
Messages
347
Location
NL
I have a script that has several pages in one page, and i want to create functions for each of the page scripts so that they can be called at the end of the page...

what is the setting for a funciton

FUNCTION func1
{
DATA HERE
}

then call it by just typeing

func1

am i correct, or is this incorrect

sorry real newbie
 
basically you do the following:

function index_page () {

all data here for index page....
}


to access it:
index_page();

you can call functions inside functions so you can create a function for footer and header. for example:
function header() {
data here....
}

function footer() {
data here....
}

then you call them from your other pages:

function index_page() {
header();
other data here...
footer();
}

there is good explanation about functions at:


(-:
 
Hi,

Like stakadush said, you can use your function in many pages. However, to do that you have to use one of these statements BEFORE you call your own (external) functions:

[tt]include 'myFunc.php'; or include('myFunc.php');[/tt]
-loads the external file multiple times if the same include() statement is used or called multiple times.
-displays a warning if the external file is not found but continues the script.

[tt]include_once 'myFunc.php'; or include_once('myFunc.php');[/tt]
-loads the external file only once, even if the same include() statement is used or called multiple times.
-displays a warning if the external file is not found but continues the script.

[tt]require 'myFunc.php'; or require('myFunc.php');[/tt]
-loads the external file multiple times if the same require() statement is used or called multiple times.
-displays a fatal error if the external file is not found, hence, aborting execution of your script.

[tt]require_once 'myFunc.php'; or require_once('myFunc.php');[/tt]
-loads the external file only once, even if the same require_once() statement is used or called multiple times.
-displays a fatal error if the external file is not found, hence, aborting execution of your script.

Also, if one of the above statement is used in a conditional block - like [tt]if($something)[/tt] - you have to put the statement in a block -ie. curly brackets [tt]{}[/tt].

Here's an example I wrote to illustate how to do it:

Filename : main.php

Code:
<?
  $user = &quot;Me&quot;;
  $dbug = false;

  if($dbug) {
    include('dbug.php');
  } else {
    include('myFunc.php');
  }
  info($user);
  echo &quot;This is PHP talking ...&quot;;
?>


Filename : myFunc.php

Code:
<?
  function info($name) {
    echo &quot;Hi $name! -You're working in <B>normal</B> mode.<BR>\n&quot;;
  }
?>

Filename : dbug.php

Code:
<?
  function info($name) {
    echo &quot;Hi $name! -You're working in <B>debug mode</B>.<BR>\n&quot;;
  }
?>

Notice that the external php scripts must be wrapped in <? and ?>

Good luck with it §;O)


Jakob
 
thanks dkdude,

that info helps.. i did have a problem with scripting on external files, not working, but sussed that problem, about the <? and ?>

thanks
 
Hey krappleby025,

Thanks. You're welcome! Nice to get something working. PHP will grow on you -It did on me! The greatest SSS ever! §:O)


Jakob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top