简体   繁体   中英

PHP how to call included file functions

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

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:

只需像调用其他函数一样调用它们:

serv1(42);

你应该能够调用serv2(3)或任何需要的东西。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM