0

i have two files. one file contains the functions and another that is trying to pull the data.

function.php

<?php 

    function randomString() {
    $str = "";
    $characters = array_merge(range("A","Z"),range("a","z"),range("0","9"));
    $max = count($characters) - 1;
    for ($i=0; $i < $length='8'; $i++) { 
        $rand = mt_rand(0, $max);
        $str .=$characters[$rand];
    } 
    return $str;
}
?>

fortesting.php

<?php 

    randomString(); //i'm getting this error: Fatal error: Call to undefined function randomString() "FILE NAME HERE" on line 3

?>
1
  • 2
    include('function.php'); in fortesting.php Commented Jul 10, 2013 at 4:34

4 Answers 4

1

You haven't included the file that contains the function definition, so it's not available in the scope when you try to call it. You'll need to use include, require, include_once, or require_once.

  • include basically pulls a file in, adding it's code/content at that point. This is good for importing HTML or template files. If it can't find the file, PHP will issue a warning but try to continue.
  • include_once behaves the same as include with the addition that it tracks what files have already been included. The file will not be included if it's already been pulled in before, which is useful when it contains function definitions and the like that will raise errors if PHP tries to define them twice.
  • require and require_once are like their include counterparts, but will raise errors if the file can't be pulled in (maybe the file doesn't exist, for example). These are good when you need to make sure the code is definitely imported or else things won't function correctly (class definitions, function defs, etc.)

So, your fortesting.php file should look like:

<?php
require_once "function.php";
echo randomString();

include/require et al are language constructs not functions, and you can either use parenthesis with them or not depending on your personal preference. That is, require_once "foo.php"; is the same as require_once("foo.php");.

For more information, check out the PHP Manual under the appropriate sections.

Hope this helps!

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

2 Comments

thank you everyone. the include worked and i forgot i was calling a RETURN not an ECHO so nothing was showing.
@user2556174 use echo andomString();.
1

make sure that in your fortesting.php you have the following statement.

require_once "function.php";

Comments

1

fortesting.php needs to require or include function.php

<?php 
    require('function.php');
    randomString(); 
?>

1 Comment

i thought if its a function you can call it anywhere without adding "include", as an example. Anyway, i did add include, but the return result is empty. i tried echoing it not as a function and the code works fine. am i missing something? is my code editor setting off?
1

fortesting.php page shoule be

<?php 
require_once("path_to_file/function.php");
randomString(); 

?>

You have to include the function.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.