5

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.

2 Answers 2

7

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)

Sign up to request clarification or add additional context in comments.

Comments

6

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.