简体   繁体   English

PHP如何调用包含的文件函数

[英]PHP how to call included file functions

lets say i make a file Services.php 让我说我做一个文件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 我把它包含在一些其他文件中,让我们说myFile.php

include "Services.php";

OR 要么

require "Services.php";
  • How can i call its functions in myFile.php 我怎样才能在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: 来自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: 查看这两个doc资源,了解有关您理解的内容的更多信息:

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

serv1(42);

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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