Is there a way to reference a variable from an external document with PHP?

I wanted to use PHP do do a little thing, but I needed to know how to get a variable from an external document (say external.php) and use it in a function in a PHP document (internal.php). I was thinking maybe something like this:

Code for external.php

$variable = "true";

Code for internal.php

if ($GETFROM{'/external.php'}['variable']) 
echo "It worked";

Is there any possible way to do something like this?

Answers


If you want to include a specific value from an include file directly into a variable in your current document, you can also include a PHP file which returns the value.

For example:

inc.php

<?php
    return "Hello, world!";
?>

index.php

<?php
    $var = include "inc.php";

    if(isset($var) && !empty($var)) {
        echo "It worked!\n";
        echo "Value: {$var}";
    }
    else {
        echo "It failed!";
    }
?>

Use the include function for that. In internal.php

include 'external.php';
if($variable)
{
    echo "It worked";
}

Note: This is great for configuration files or loading of classes or helper functions, but try to avoid too many includes in random places in your sourcecode. This can make your code very hard to read.


Need Your Help

ASP.net MVC problems when model binding with sub-class

asp.net asp.net-mvc data-annotations

I've got two types of contact entity. One that just has an email address and one that drives from this and includes fields for a postal address.

Magento Multiple Authorize.net Gateways

magento payment authorize.net e-commerce

I have a CAD and USD gateway accounts for processing credit card payments, both from a payment provider with support for the Authorize.net API. I've successfully configured one using Magento's buil...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.