I currently have 3 files, 1 return array, 2 is command file that manipulate all other and 3 is function file,i include array and function file in command file. How to make array visible in function file, should i use second include or there another way?
file 1
return $arr = array(...);
file 2
function FuncName(){ $arr[1] = '1111';}
file 3
include_once 'file 1';
include_once 'file 2';
$arr[2] = '2222'
It's simple. You can use return inside included file.
//main.php
include_once 'functions.php';
$array = include 'array.php';
goGoArray($array);
// functions.php
function goGoArray($array){
var_dump($array);
}
// array.php
return array(1,23,456,7890);
But, if you what global, use global.
//main.php
include_once 'functions.php';
$array = include 'array.php';
goGoArray();
// functions.php
function goGoArray(){
global $array;
var_dump($array);
}
// array.php
return array(1,23,456,7890);
P.S. But globals are very, very bad.
Related
I have two files:
I need to inclide a.php in b.php then call the a function to return a string variable and print it.
a.php:
function CheckExist($file) {
$response;
if (file_exists($file) {
$response = "File Extisted";
} else {
$response = "File Not Existed";
}
return $response;
}
b.php
// Some code to include a.php and call the function to get the string variable
print($response);
I understand you want to include file a.php in b.php and be able to use the function in a.php.
In PHP, you can include files using require_once, require, include and include_once statements which you should have probably known about before asking this question.
To suggest a solution while you read from the links I have provided, you can use any of the above functions to include your file a.php, call the function and save the return value in a variable and print it out:
b.php
require("a.php");
$response = CheckExists("a-sample-file.txt");
print($response);
I have a file called test1.php with lots of variable and some function definitions. I am trying to include this file to one another file called test2.php and use the variables and the functions.
test1.php
$i = "a";
$ii = "b";
$iii = "c";
function test1a(){ return "lol"; }
test2.php
function test2a(){ include 'test1.php'; return $i; }
function test2b() { include 'test1.php'; return test2c(); }
function test2c(){ include 'test1.php'; return $iii; }
function test2d() { include 'test1.php'; return test1a(); }
index.php
include 'test2.php'
echo test2a();
echo test2b();
echo test2c();
echo test2d();
Reason:
I have the same code base in two different servers. Only the test2.php file will be different.Each server will have different values inside the test2.php but with same variable name. test2.php will act somewhat like a localization file.
My problem is some of the variables or not showing up. Is there a better way to do this. I don't want to include the file in every function.
Thanks.
Just do it the other way round:
put all the different variables into one file in to an array, without any functions:
//config.php
$config['setting1'] = "val1";
$config['setting2'] = "val2";
$config['setting3'] = 42;
...
and further:
//index.php
include_once("config.php");
echo $config['setting1'];
....
now you may have different configs on different servers w/o need to change any functions.
You are trying to include file with one function several times. Functions are always global. So second include gives you an fatal error Fatal error: Cannot redeclare test1a() (previously declared in [..]).
You should put this function to separate file and use include_once.
I'm a newbie to php, and I'm in the process of hacking through some moodle code for a prototype.
Anyhows I have some data Id like passed from file1.php to file2.php, for which Im using global variables. The values are intialized in file2 and I need them for use in file1. Here's how I go about it
file1.php
<?php// top of file 1
global $content; // discussion content // line 3
file2.php
global $content;// line 3379
$content=$post->subject;
Back in file1.php
echo 'global scope'.$content; // this is always empty// line 168
Am I missing something here?
Attached files file1.php and file2.php
You use global to get a global variable
//file 1
$a = "im a global variable";
function foo(){
global $a;
echo $a;
}
//file 2
require "file1.php";
function foo2(){
global $a;
echo $a;
}
Just sharing a thought,
suppose in file1, you have $content = "myID";
and in file2, if you include("file1.php"), you can access by doing,
global $content;
var_dump($content) will output string 'myId' (length=4)
Good day.
I have problems when i use globals element in some files which include in one file.
Structure my files you can see down:
Files:
-index.php
--function.php
--globals.php
--lang.php
--allfunction.php
Code all pages you can see down:
Code index.php:
<?
session_start();
require_once("./function.php");
select();
?>
Code function.php:
<?php
require_once("./globals.php");
require_once(dirname(__FILE__)."/lang.php");
include_once Language(3);
require_once(dirname(__FILE__)."/allfunction.php");
?>
Code globals.php:
<?
$dirang = './';
$langfile = 'lang.php';
$test = 'hello';
}
?>
Code lang.php:
<?
Language($rem){
return $GLOBALS["langfile"]; //ex.
}
?>
Code allfunction.php:
<?
echo $GLOBALS["test"]; //ex.
}
?>
I get problrem when i use $GLOBALS["test"] in allfunction.php.
I get error Undefened index test in allfunction.php on line ....
Tell me please why i get it error and how aright use global element in allfunction.php ?
you should write globals inside file where you use theys, for ex. if globals you use in allfunction.php you must write global variables in this file(in which you use them)
Not use a separate file with global variables.
Don't use globals. Store your variables in static Config or Registry class instead. Also have a read about singleton design pattern.
Example:
config.php
<?php
class Config
{
static $var1 = '...';
static $var2 = '...';
public static function init()
{
self::$var2 = 1+1; //expressions go here
}
}
Config::init();
usage.php
<?php
require_once 'config.php';
function x()
{
$someKindOfSetting = Config::$var1;
}
Just a quick question, but I've been working on a small MVC framework and noticed something.
For example:
--PHP file--
class loadFiles{
function __construct($file = NULL){
include $file . '.php';
}
}
$loadfiles = new $loadFiles('crucialsettings');
echo $randomstring; //Throws an error
--crucialsettings.php--
<?php
$randomstring = 'hello';
?>
I only just realised that files included inside an objects scope are inaccessable from the global scope. What is the best way to include a file inside an object so it can be accessed globally?
I would like to be able to:
$loadfiles->settings();
$loadfiles->classes();
$loadfiles->passwords();
I want to build a class that handles global file includes.
It doesn't matter where you include or require code from in PHP. The interpreter is pretty linear in it's first definition pass, that is to say that it will basically compress all of the included / required files into one large file in the exact order in how it was read.
One thing to note about this is that scope does change. but everything is applied to the "global" scope. You can always import something from the global scope into your current scope using the "global" keyword to declare a variable prior to using it. So when you want to use a "global" variable from another script just ask for it.
A little example...
a.php
include('b.php');
global $myVar;
echo $myVar;
b.php
include('c.php');
c.php
$myVar = 'Hello World';
What the interpreter see's this code as after it's first pass
// In global scope
$myVar = 'Hello World'
// In a.php scope
global $myVar;
echo $myVar;
In short from your php file simply add the line
global $randomstring;
After you include the crucialsettings.php file and your echo will work.
Appears that your framework here is too reliant on non-OOP for its innards. Not a preferable way to build up, but you can do what you want by cycling through list of variables and making them part of your class/instance scope. A rather helpful function here is get_defined_vars();
Lets say you have files a.php, b.php and c.php. Each looks like this:
a.php: <?php $a = "AAAAAA";
b.php: <?php $b = "BBBBBBBBBB";
c.php: <?php $c = "CCCCCCCCCCCCCCCCCCCCCCCCCCC";
class mystuff {
function include_with_vars( $____file ) {
// grab snapshot of variables, exclude knowns
$____before = get_defined_vars();
unset( $____before['____file'] );
// include file which presumably will add vars
include( $____file );
// grab snapshot of variables, exclude knowns again
$____after = get_defined_vars();
unset( $____after['____file'] );
unset( $____after['____before'] );
// generate a list of variables that appear to be new
$____diff = array_diff( $____after, $____before );
// put all local vars in instance scope
foreach( $____diff as $variable_name => $variable_value ) {
$this->$variable_name = $variable_value;
}
}
function __construct($file = NULL){
$this->include_with_vars( "a.php" );
$this->include_with_vars( "b.php" );
$this->include_with_vars( "c.php" );
}
}
$t = new mystuff();
echo "<PRE>";
print_r( $t );
This program will now take local variables from your include() directives and put them in the class scope:
mystuff Object
(
[a] => AAAAAA
[b] => BBBBBBBBBB
[c] => CCCCCCCCCCCCCCCCCCCCCCCCCCC
)
In other words, your local variables from file a.php ($a) are now $t->a.