how to set variables in a php include file? - php

I have a set of variables that are set outside the target php include file, how would i set them in the target php include file. Example:
<?php
$fname = "david";
?>
Now how would I set $fname in another php file?

In your other PHP file you would do:
<?php
$fname = "david";
?>
This answers your question directly, but I would hazard a guess that you actually want to have access to variables that are set in a file that is included into your current file or something along those lines.
So
File1.php:
<?php
$fname = "david";
?>
File2.php
<?php
require_once 'File1.php';
echo $fname;
Would result in david being printed to screen.

Just use it directly.
$fname = "your value";

The PHP docs have an article about variable scopes.
The global variable scope is shared across all included and required files. In your example, once you've defined $fname globally, all other PHP lines executed afterwards can access $fname even if they're in different files.
Example: if a.php is:
<?php
$fname = "david";
?>
and b.php is:
<?php
$fname = "sarah";
include 'a.php';
// $fname is now "david"
?>
then executing b.php would define $fname as "sarah", then redefine it as "david" (through a.php).

Related

Can't pass variable from one file to another using $GLOBALS array [duplicate]

I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>

how to read data from variables using header function in php

in the root of my project folder i have a file called index.php and a directory called views which contains a file called page_one.php ...
index.php file has the follwing code:
<?php
$action = "one";
if ($action == 'one') {
$name = "John";
//include './views/page_one.php';
header("Location: views/page_one.php");
}
?>
and page_one.php has the following code:
<?php echo 'Name = ' . $name; ?>
in the above code I have commented out the line with include because that works perfectly..I want to pass the value of $name using the header function..Is there a way of doing it WITHOUT sending the value in the URL?
I want the address in the URL to change when page_one.php is accessed that is why I am using the header function instead of include...
#Qirel as per said, try using session. Do something like this in index.php
<?php
session_start();
$_SESSION['name'] = "John";
header("Location: views/page_one.php);
exit();
?>
and inside page_one.php
<?php
session_start();
echo $_SESSION['name'];
unset($_SESSION['name']); // remove it now we have used it
?>

how to pass variable with require() in PHP

I'm stuck trying to pass variables between 2 PHP files a.php and b.php using require(). I learned that PHP variables have a single scope, which means that if a.php includes b.php, it can read its variables. However, I would like b.php to be able to read a variable from a.php with my current setup. For example, I can print out $msg if it is set in b.php but not a.php. Is there a simple way to do this?
a.php
<?php
$msg = "Welcome NEW USER ! We can't wait for you to check out our books!";
require("connect.php");
require("b.php");
$table = "SELECT * FROM `login`";
if ($query_run=mysqli_query($cnn,$table)){
$username = $_POST['username'];
$pw = $_POST['password'];
$sql = "INSERT INTO login (username,password) VALUES ('{$username}','{$pw}')";
if($cnn->query($sql)===TRUE){echo "Query ran successfully";}
else{echo "Error: ".$sql."<br>".$cnn->error;}
}
else{die("table connection failed");}
$cnn.close();
?>
b.php
<?php
echo "<h1>".$msg."</h1>
<form action='' method='post'>
Username: <input type='text' name='username'><br>
Password: <input type='text' name='password'><br>
<input type='submit' name='submit'>
</form>
"
?>
Using $_SESSION will allow you to pass data between different files.
in a.php
$_SESSION['myName'] = "John";
in b.php
echo $_SESSION['myName']
I'm not sure what your trouble is but your example should work. This most definitely works:
a.php
<?php
$from_a = 'hey';
require "b.php";
print $from_b;
b.php
print $from_a;
$from_b = 'okay';
The above is perfectly valid PHP and will work as intended with no errors. A script which is required by another script has full access to its variables and vice-versa.
If you make require an infinite cycling occurs,try require_once in each file.
But the best solution is to create a third file be it c.php and include both b.php and a.php in it
No need to use of session.
if PHP file is included in other PHP file then variables also accessible in included PHP file.
Example
<?php
$a = 1;
include 'filename.php';
?>
Then $a is accessible in filename.php.
use echo in included file and test..
Let me know if you feel any issue.
"which means that if a.php includes b.php, it can read its variables."
I think the concept is slightly different. When you call the language construct require, it will read and include the specified file as if it were written in the calling file itself.
So this:
<?php
// File a.php
$somevar = 6;
require "b.php";
?>
<?php
// File b.php
echo $somevar;
?>
Is effectively the same as
<?php
// File a.php
$somevar = 6;
// File b.php
echo $somevar;
?>

PHP using variable for require location

I believe my problem should be simple, but no matter how much looking I do I cant find a solution :(
I am trying the following....
File structure.....
-Folder 1
-Folder 2
-loader.php
-admin.php
loader.php contents...
<?php // $_GET['l'] == admin.php
$location = $_GET['l'];
require $location;
?>
admin.php contents .....
<?php echo "test"; ?>
But this doesnt work. Anything I search only brings results regarding passing the variable to the loaded file. I'm wanting to use the variable to define the location of the file to require.
Surely this is possible somehow!
<?php // $_GET['l'] == admin.php
$location = $_GET['l'];
require $location;
$location = $_GET['l'];
require '$location';
?>
i have also tried require|include|require_once|include_once

How to access a variable across two files

I have three files - global.php, test.php, test1.php
Global.php
$filename;
$filename = "test";
test.php
$filename = "myfile.jpg";
echo $filename;
test1.php
echo $filename;
I can read this variable from both test and test1 files by
include 'global.php';
Now i want to set the value of $filename in test.php and the same value i want to read in test1.php.
I tried with session variables as well but due to two different files i am not able to capture the variable.
How to achieve this........
Thanks for help in advance.....
Use:
global.php
<?php
if(!session_id()) session_start();
$filename = "test";
if(!isset($_SESSION['filename'])) {
$_SESSION['filename'] = $filename;
}
?>
test.php
<?php
if(!session_id()) session_start();
//include("global.php");
$_SESSION['filename'] = "new value";
?>
test1.php
<?php
if(!session_id()) session_start();
$filename = $_SESSION['filename'];
echo $filename; //output new value
?>
I think the best way to understand this is as following:
You have one file index.php whit some variable defined.
$indexVar = 'sometext';
This variable is visible on all index.php file. But like a function, if you include other files, the variable will not be visible unless you specify that this variable has scope global.
You should redefine the scope of this variable in your new file, like this:
global $indexVar;
Then you will be able to acess directly by calling it as you were in your main file. You could use an "echo" in both files, index.php and any other file.
First you start session at the top of the page.
Assign your variable into your session.
Check this and Try it your self
test.php
<?php
session_start(); // session start
include("global.php");
$filename = "myfile.jpg";
$_SESSION['samplename']=$filename ; // Session Set
?>
test1.php
<?php
session_start(); // session start
$getvalue = $_SESSION['samplename']; // session get
echo $getvalue;
?>

Categories