Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP how to call included file functions

Tags:

function

php

call

lets say i make a file Services.php

<?php

$myGlobalVar='ranSTR';

function serv1($num)
{
   //some processing...
   return $num;
}

function serv2($num)
{
   //some processing...
   return $num;
}

?>

and i include it in some other file lets say myFile.php by

include "Services.php";

OR

require "Services.php";
  • How can i call its functions in myFile.php
like image 535
Moon Avatar asked Aug 18 '10 02:08

Moon


3 Answers

You can just call any of these functions. After the file is included, the functions will be placed in the global scope.

have you not tried:

include "Services.php";

$num = 123;

serv1($num);

From the doc:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

Check out these two doc resources for more information on what you're having trouble understanding:

  • include statement: http://php.net/manual/en/function.include.php
  • variable scope: http://php.net/manual/en/language.variables.scope.php
like image 106
bimbom22 Avatar answered Nov 16 '22 08:11

bimbom22


Just call them the same as you would any other function:

serv1(42);
like image 25
Thanatos Avatar answered Nov 16 '22 07:11

Thanatos


You should just be able to call serv2(3) or whatever need be.

like image 40
Chiggins Avatar answered Nov 16 '22 08:11

Chiggins