Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I call a function in another file?

Tags:

php

For example, I have a file error-status.php, which includes a function:

function validateHostName($hostName)
{
    if ((strpbrk($hostName,'`~!@#$^&*()=+.[ ]{}\\|;:\'",<>/?')==FALSE) && !ctype_digit($hostName) && eregi("^([a-z0-9-]+)$",$hostName) && ereg("^[^-]",$hostName) && ereg("[^-]$",$hostName))
    {
        return true;
    }
    else
    {
        return false;
    }
}

...

How do I call that function from a different PHP file after invoking require_once?

require_once('error-status.php');
like image 686
Harsh Avatar asked Apr 27 '11 04:04

Harsh


People also ask

How do you call a function in another file?

To use the functions written in one file inside another file include the import line, from filename import function_name . Note that although the file name must contain a . py extension, . py is not used as part of the filename during import.

How do you call a function in another function?

To call a function inside another function, define the inner function inside the outer function and invoke it. When using the function keyword, the function gets hoisted to the top of the scope and can be called from anywhere inside of the outer function.

Can we call one function in another?

In Python, any written function can be called by another function. Note that this could be the most elegant way of breaking a problem into chunks of small problems.


2 Answers

Include the file before you call the function.

include 'error-status.php';
validateHostName('myhostname'); 
like image 137
Chris Avatar answered Sep 30 '22 20:09

Chris


I would simply extend the class or use the require /include, then:

$var = new otherClass;
$getString = $var->getString();
like image 44
Portu Avatar answered Sep 30 '22 20:09

Portu