1

If you include multiple PHP scripts in a script are those variables accessible by the script? For example, I have one file called post.php. Within this I have...

include(email.php);
include(input.php);

Are the variables within each of these scripts self contained even if they are "included" on the same page? If they are, how can I access them within each of the scripts. I ask because I can't call variables in "post.php" that I have defined in the other scripts. Thanks

EDIT WITH COMPLETE CODE

Here is my first page where I gather data from my user:

<form action="postinput.php" method="post">
<input type="text" placeholder="Name"name="Name"><br>
<input type="text" placeholder="Email"name="Email"><br>

<input type="text" placeholder="Title"name="Title"><br>
<textarea placeholder="Post" rows="4" cols="22" placeholder="Post"name="Post"></textarea>
</form>

Here is the second page where I take this data and use fopen to create a random "post" page out of the supplied data:

$getname = $_POST['Name'];
$getemail = $_POST['Email']; //Here is the email variable I am trying to pass
$gettitle = $_POST['Title'];
$myfile = fopen("$random" . ".php", "w");
$txt = "<?php  include('post.php'); \$email = \"$getemail\";?>" //pass email variable and include post.php
fwrite($myfile, $txt);
print("you can see your post here:");
echo ('http://localhost/' . $random . '.php');

Here is post.php. This should include the $email variable I passed above but I cannot even echo the variable. If I pull up the php page that was generated I can see that the variable is declared but I still cannot access it for some reason.

<?php
include('Header.php');
?>

<div id = "center">
    <form action="" name="emailform" method="post">
        <input type="text" name="name">
        <input type="text" name="email">
        <input type="text" name="message">
        <input type="submit" name="Send" value="Send Email">
    </form>
</div>

<?php

echo $email;

if (isset($_POST['Send'])) {

    $name = $_POST['name'];
    $visitor_email = $_POST['email'];
    $message = $_POST['message'];

    $email_from = '[email protected]';
    $email_subject = "You have a reply from better barter";
    $email_body = "Message from: $visitor_email \n \n Message:$message";

    $to = $email;
    $headers = "from:adam\r\n";

    mail($to,$email_subject,$email_body,$headers);

} else {
    echo 'You have not hit the submit button yet';
}
?>
6
  • 1
    Insufficient info. Including files works like copy pasting their contents. There's nothing mystic about it. If you're wondering about specific code not working after including it, then provide the code, not just the "general" idea of the thing you think is not working. Commented Oct 20, 2014 at 19:22
  • functions and variables defined in one script and included are available in any other script included, also the parent script, at any point after the file has been included. Commented Oct 20, 2014 at 19:26
  • There is no boundary as such between those different files, you can access the same set of resources regardless in which file you do that. However there may be other scopes that may cause your issue: separate namespaces, or the variables are not defined globally but inside functions, classes or the like. In general it is a very good idea not to define global variables, but here this might be the issue. Commented Oct 20, 2014 at 19:27
  • Yes you can use them if they are not defined privately inside classes. but if the variables are to be used inside function, you must define them as globalbefore using them. like: global $myvariable; Commented Oct 20, 2014 at 19:31
  • Edited with full code Commented Oct 20, 2014 at 22:50

3 Answers 3

2

You don't need braces: include 'email.php';.

Always use a path for includes/require include __DIR__ . '/email.php'.

After a include/require the variables become available.

Be aware, that this might overwrite existing variables.

PHP Manual - include()

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.


you have post.php and want to access a variable defined in email.php.

email.php

<?php
$var = 'Hello';

post.php

<?php 
include __DIR__ . '/email.php';
echo $var;

You are creating a random php file.

$txt = "<?php  include('post.php'); \$email = \"$getemail\";?>"

This part \$email = \"$getemail\" looks strange to me. I think this line should be:

$txt = "<?php include 'post.php'; $email = '" . $getemail . "'; ?>";

You could append echo $email; and call random.php to test it, like so:

$txt = "<?php include 'post.php'; $email = '" . $getemail . "'; echo $email; ?>";

By the way: this is a template approach. You are inserting content to generate the new file.

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

5 Comments

Why are the backslashes there?
Because they are for directory separation. But you might use the constant DIRECTORY_SEPARATOR - which is probably more clean and cross platform.
My point is that only windows understands these backslashes. Beter to indeed use DIRECTORY_SEPARATOR or just the good old /
Edited with full code. YOur post is helpful but it seems that everything is being included properly as I can see the other elements in the included php script. The problem is I cannot access the variables
I will take a look and extend my answer.
1

If you have set the variables and they are unique, you can access them from the point on where the script is included!

Comments

0

Variables operate in 3 scopes: Global, Functions, and Objects/Classes. No matter which scope a variable is in, it is not effected by include, include_once, require, or require_once.

Globals

Global variables are only accessible outside of functions and classes OR the $GLOBALS superglobal. However, variables are not effected by namespaces.

Functions

Variables used inside functions are only available to that function, and are reset after the function has run. Keep in mind that static variables are not reset after the function has run. Also, while variables are not effected by includes or requires, if you use an include/require inside of a function, the included file and its variables are under that function's scope.

Objects/Classes

Variables used in side of instanced objects or classes (such as stdClass) are unique to that instance. Whereas static classes have only one global instance of the variable. It is important to note that while they are used in an object, you must access the object (and the variable by proxy) by the correct namespace, like so:

Instanced

$helloWorld = \myNamespace\helloWorld();
echo $helloWorld->myVar

Static

echo \myNamespace\helloWorld::$myVar

2 Comments

The fourth scope would be global.
@JonathanKuhn I know, technically the "global" is a namespace though. Give me a moment to write the rest in.

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.