PHP how to call included file functions

Moo*_*oon 3 php function 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;
}

?>
Run Code Online (Sandbox Code Playgroud)

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

include "Services.php";
Run Code Online (Sandbox Code Playgroud)

OR

require "Services.php";
Run Code Online (Sandbox Code Playgroud)
  • How can i call its functions in myFile.php

jor*_*ens 24

您可以调用任何这些功能.包含文件后,函数将放在全局范围内.

你有没有试过:

include "Services.php";

$num = 123;

serv1($num);
Run Code Online (Sandbox Code Playgroud)

来自doc:

包含文件时,它包含的代码将继承发生包含的行的变量范围.从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用.但是,包含文件中定义的所有函数和类都具有全局范围.

查看这两个doc资源,了解有关您理解的内容的更多信息:


Tha*_*tos 5

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

serv1(42);
Run Code Online (Sandbox Code Playgroud)


Chi*_*ins 5

你应该只是能够打电话serv2(3)或任何需要.