Parijat Kalia
Parijat Kalia

Reputation: 5095

How to execute an included/required PHP file without having to make calls to it's functions or use auto_prepend_file ?

This might be a total NOOB question but I am having difficulty including and executing a file. I understand I can require_once('file.php') but then I am going to have to manually call the functions within this file instead of having the file run by itself (it's procedural in nature).

I can use php_value auto_prepend_file 'file.php' in my .htaccess or apache conf folder, but it already has auto prepended files which I cannot remove.

Can anyone suggest something ?

Upvotes: 0

Views: 2127

Answers (3)

Tyler Carter
Tyler Carter

Reputation: 61587

If you have file.php that looks something like:

<?php
echo "Hello World!";
?>

PHP will automatically parse and execute the file. For instance, all I would have to do in index.php is

<?php
include 'file.php';
?>

Upvotes: 1

Gusman
Gusman

Reputation: 15161

If you do a require_once and you put your code in the body of the required document it will be executed when you do the require_once.

If you have your code in functions then you can call them in the required document:

<?php

    function DoSomething()
    {
        //Do something :D
    }

    DoSomething();

?>

Upvotes: 2

Marc B
Marc B

Reputation: 360872

somefile.php:

<?php

   include('somefile.txt');

somefile.txt:

hello, <?php echo 'world!'; ?>

Then at your shell prompt (or via web browser):

$ php somefile.php
hello, world!

Nothing need to be done. If you include or require a file, it's parsed and any <?php ... ?> code blocks are executed. If you have "executable" code that's not buried inside a function(...) { .... } definition, it'll be executed automatically.

Upvotes: 2

Related Questions