Reputation: 129
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';
}
?>
Upvotes: 1
Views: 3329
Reputation: 41756
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.
Upvotes: 2
Reputation: 4756
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
.
Global variables are only accessible outside of functions and classes OR the $GLOBALS
superglobal. However, variables are not effected by namespaces.
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.
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
Upvotes: 0
Reputation: 73241
If you have set the variables and they are unique, you can access them from the point on where the script is included!
Upvotes: 1