mistysnake
mistysnake

Reputation: 113

PHP - how to send variable values without Global variables between php files?

Without use of cookie, session, post, get superglobals, is there a way to retrieve variables between php files?

1.php has

$value="hello";   

and

2.php wants to retrieve

$value // with value hello

Upvotes: 1

Views: 175

Answers (6)

Fabien Warniez
Fabien Warniez

Reputation: 2741

If they are run in the same call, then you can include the PHP file that defines the variable in the second PHP file and access it as if it was defined in the second one.

If these scripts are executed as part of 2 different calls, then you need to give us more information about what / why you are trying to do.

Upvotes: 0

Subie
Subie

Reputation: 383

Here's an example using a class...

1.php

<?php
  class Config {
      public static $test = "hello world!";
      public static $arrayTest = array(
         'var1'=>'hello',
         'var2'=>'world',
     );
}

?>

2.php

<?php
  include('1.php');
  echo Config::$test;
  echo Config::$arrayTest['var1'];
?>

Upvotes: 0

frosty11x
frosty11x

Reputation: 205

You could make a class, then include the class and reference the variables through that class.

Upvotes: 0

Rottingham
Rottingham

Reputation: 2604

Here is a common method I use, because you can write to it as well, making it dynamic and not hard coded values that require you to manually edit the file.

globalvalues.php

<?
return array (
    'value1' => 'Testing'
);

2.php

$globalValues = include('globalvalues.php');

echo $globalValues['value1'];

I have wrapper classes around this, but thats the basics of it.

Upvotes: 0

Brian H.
Brian H.

Reputation: 505

You will have to store the state of the variables somewhere. If you don't want to use the session, you can write them to a file or database. Or, you can store them client-side using JavaScript. You can't read between two different requests without storing the information, though.

Upvotes: 0

Awlad Liton
Awlad Liton

Reputation: 9351

TRY this:
1.php

$a="this is my 1.php";

2.php

include("1.php");
echo $a;

OUTPUT:

this is my 1.php

Upvotes: 2

Related Questions