PHP, Getting Variable from Another PHP-File

PHP, getting variable from another php-file

You can, but the variable in your last include will overwrite the variable in your first one:

myfile.php

$var = 'test';

mysecondfile.php

$var = 'tester';

test.php

include 'myfile.php';
echo $var;

include 'mysecondfile.php';
echo $var;

Output:

test

tester

I suggest using different variable names.

How to get a PHP variable value from another PHP file?

Just use requiere

<?php require 'file2.php'; ?>

How do I use a variable from another file?

The problem is not with the include or require. The problem is that $check is not defined inside your acheckReturner function.

<?php 
require "nuclear_test2.php";

function acheckReturner() {
echo " ?" . $check; // this $check is undefined in this scope
}
acheckReturner();
?>

You can use it by passing it as an argument to the acheckReturner function.

<?php 
require "nuclear_test2.php";

function acheckReturner($check) {
echo " ?" . $check;
}

acheckReturner($check); // this is the $check from nuclear_test2.php
?>

The PHP documentation on variable scope may be helpful.

Php get variable from another file

There are many different ways you could do this, I will share 2 with you.

INCLUDE

You can in fact include() it using a 3rd file, but I don't recommend that as sessions are much easier. But this is useful if you want a config type file aswell.

top of login.php:

include('global.php');

New file: global.php

    if (session_status() == PHP_SESSION_NONE) {
session_start();
} //start sessions if it isn't already started.
if(isSet($_POST['email'])) { //Check if email variable is POSTED
$email = $_POST['email']; //Set session variable "Email" to the posted data.
}

top of candidate.php

include('global.php');

You'll want to include the global file before anything containing $email is written. The other bright side of this method is that you only really need to start sessions once (if you start them in the global file), because when you include it on other pages, it takes care of itself.


SESSIONS

At the top of any page that you want to access a global variable, you could use sessions.

Top of login.php:

if (session_status() == PHP_SESSION_NONE) {
session_start();
} //start sessions if it isn't already started.
if(isSet($_POST['email'])) { //Check if email variable is POSTED
$_SESSION['email'] = $_POST['email']; //Set session variable "Email" to the posted data.
}

on Candidate.php:

if (session_status() == PHP_SESSION_NONE) {
session_start();
} //start sessions if it isn't already started.
echo $_SESSION['email']; //You can call this variable on any page that has sessions started

Hope I helped you!

Passing a variable from one php include file to another: global vs. not


The parent file has access to variables in both included files

When including files in PHP, it acts like the code exists within the file they are being included from. Imagine copy and pasting the code from within each of your included files directly into your index.php. That is how PHP works with includes.

So, in your example, since you've set a variable called $name in your front.inc file, and then included both front.inc and end.inc in your index.php, you will be able to echo the variable $name anywhere after the include of front.inc within your index.php. Again, PHP processes your index.php as if the code from the two files you are including are part of the file.

The included file doesn't have access to the other included file

When you place an echo within an included file, to a variable that is not defined within itself, you're not going to get a result because it is treated separately then any other included file.

In other words, to do the behavior you're expecting, you will need to define it as a global.

Can't use php variable from another file

Functions have their own scope. The variable is accessible, just not from inside the function.

You could pass $DEBUG as a parameter

function echo_debug(string $message, bool $DEBUG)

Then you would call it as

echo_debug("comment that will help me debug in dev mode", $DEBUG);

Another option is to declare DEBUG as a constant,

define('DEBUG', true);
$mysqli = new mysqli("127.0.0.1", "root", "", "29185917-database");
$DEBUG_LOG_FILE = "../log";

Then, in your function you would check for that constant:

function echo_debug(string $message) {
if(DEBUG) { ... }
}

You could also use the global keyword, right above your if(), try to add global $DEBUG;.

require_once 'variables.php';
function echo_debug(string $message)
{
global $DEBUG;
if($DEBUG) { ... }
}

But generally the other two solutions are better, global variables are sometimes frowned upon.

How do I get every array/variable from another php file without knowing variables/arrays names

Despite the comments correctly suggesting to think about whether this is a preferrable approach at all (see XY problem), I still think this might be a niche use-case that warrants a solution (after all, we simply don't know all the legitimate use-cases that may exist).

So here's what you can do: Use get_defined_vars() after and before requiring the file. Then just include all the variables that didn't exist before the require (array_diff will not work in this case, as it will throw an "array to string conversion" error).

<?php
$vars_before = get_defined_vars();

require 'example.php';

$vars_after = array_filter( get_defined_vars(), function( $key ) use ( $vars_before ) {
return ! array_key_exists( $key, $vars_before ) && $key !== 'vars_before';
}, ARRAY_FILTER_USE_KEY );

var_dump( $vars_after );


Related Topics



Leave a reply



Submit