简体   繁体   中英

How to call php function from another file?

I want to call my custom php function from my index.php, but the function is located in another file. Ho to do that ?

Example :

index.php :

<?php

myCustomFunction();

?>

function.php

<?php

function myCustomFunction(){
//some codes
}

?>

Thank you.

You just need to include the other file:

include('function.php');

index.php :

<?php

include('function.php');

myCustomFunction();

The include function in PHP inserts in-place the code contained in the file passed to its parameter.

For further details, and alternative, please refer to PHP manual:

http://php.net/manual/en/function.include.php

http://php.net/manual/en/function.include-once.php

http://php.net/manual/en/function.require.php

http://php.net/manual/en/function.require-once.php

Change index.php as follows:

<?php
  include "function.php";
  myCustomFunction();
?>

(this is based on the assumption that both files are in the same directory, otherwise you have to add the filepath in the include line)

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