Jump to content

Functions from different scripts


supertrucker
 Share

Recommended Posts

How do you call a function from a different PHP script? I'm a little confused on this topic. Thanks,Supertrucker :)

Link to comment
Share on other sites

How do you call a function from a different PHP script? I'm a little confused on this topic. Thanks,Supertrucker :)
you can do something like this have two seperate files we call one page.php and another called functions.php. We will start with the functions page which as you can assume will have all your functions. and will look like this.functions.php
<?phpfunction somefunction($varaible,$varaible){code here.....code here......code here.....return $varaible}?>

Okay then on to calling the function from another php page in this case it will be called from our page.php.page.php

<?php include $_SERVER['DOCUMENT_ROOT'].'/functions.php'; <-------- this includes your functions$variable = somefunction($variable1,$variable2);?>

The include keyword allows you to include the functions.php page for use on this page. It must be on every page you want to use the functions.Next i used $_SERVER['DOCUMENT_ROOT'].'/functions.php'; as a personal prefence this makes it portable on any server i upload this script to. You can alternativly just do:include '/functions.php';Then the next line of code is calling our function passing $variable2 and $variable2 as arguments for the function to use then storing it into our a variable called $variable.And that is basicly it to calling a function from a diffrent php page.

Link to comment
Share on other sites

Cool, thanks. And can variables from the main script be accessed in the called function still, by declaring them global in the called function? In other words do the included functions act as if they were coded into the main script? Thanks again and in advance!Supertrucker :)

you can do something like this have two seperate files we call one page.php and another called functions.php. We will start with the functions page which as you can assume will have all your functions. and will look like this.functions.php
<?phpfunction somefunction($varaible,$varaible){code here.....code here......code here.....return $varaible}?>

Okay then on to calling the function from another php page in this case it will be called from our page.php.page.php

<?php include $_SERVER['DOCUMENT_ROOT'].'/functions.php'; <-------- this includes your functions$variable = somefunction($variable1,$variable2);?>

The include keyword allows you to include the functions.php page for use on this page. It must be on every page you want to use the functions.Next i used $_SERVER['DOCUMENT_ROOT'].'/functions.php'; as a personal prefence this makes it portable on any server i upload this script to. You can alternativly just do:include '/functions.php';Then the next line of code is calling our function passing $variable2 and $variable2 as arguments for the function to use then storing it into our a variable called $variable.And that is basicly it to calling a function from a diffrent php page.

Link to comment
Share on other sites

well not really variables inside your function are only avaible inside that function once you exit out of the functions your varibles used in there are destroyed hence the return at the end of the function. To clear it up a bit more basicly your passing variables from the main script in the above example ($variable1,$variable2).Those will be used inside the function to do what ever you need done inside the function. Now in order to use the results you get from inside the function you use the return $variable_name; statement to return it to the main script. and then that variable can be used through out the rest of your main script.So in esscence when you include your function it is part of the main script then, however you still need to pass it variables through the function call, so in other words the variables inside the function are not global they are known as local vararibles.Hope that clears up your question a bit more.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...