2

I'm trying to make an easy function for including file, but for some reason, the variables aren't being passed to.

In the file test.php:

<?php
function parse($template) {
    require $template;
}

$test = "test";
parse("stuff.php");
?>

And the file stuff.php:

<?php
echo $test;
?>

And I'll get the error:

Notice: Undefined variable: test in /opt/lampp/htdocs/testinggrounds/stuff.php on line 2

However, if I replace this statement in test.php

parse("stuff.php");

with

require "stuff.php";

And it'll work just fine.

1 Answer 1

1

Why make a function when she exist ?

You have just to call require 'stuff.php'; and that's all

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

1 Comment

I know. I wanted to put $path and $ext inside the function so all I need was to call parse("script") and it'll include path/script.php. If there's really no other way, then I'll do what you said earlier. But thanks anyway! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.