开发者

How would I call a function in another file?

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;
开发者_JS百科    }
}

...

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

require_once('error-status.php');


Include the file before you call the function.

include 'error-status.php';
validateHostName('myhostname'); 


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

$var = new otherClass;
$getString = $var->getString();


include or require the file before you call the function.


Building on what Chris said, if your function is inside a class in error-status.php, you'll need to initialise the class and call the function through that.

http://php.net/manual/en/keyword.class.php


See an example below,

first_file.php :

<?php
    function calling_function(){ 
       $string = "Something";
       return $string; 
    }
?>

In your Second file

second_file.php :

<?php
     include 'first_file.php';
     $return_value = calling_function();
     echo $return_value;
?>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜