PHP require variable from php file - php

How can I make a config.php file with all environment variables and import it successfully into other scripts?
config.php:
<?php
$folder = 'SomePath';
?>
script1.php
<?php
require_once('config.php');
function doStuff(){
global $folder;
echo($folder);
}
doStuff();
?>
I get
PHP Notice: Undefined variable: folder in script1.php
It's php version 5.4.41 and I can't do anything about it (no root)

keeping it simple, if it was me:
//config.php:
<?php
$folder = 'SomePath';
?>
//script1.php
<?php
require_once('config.php');
function doStuff($folder){//parse the variable in to the function
echo($folder);
}
doStuff();
?>

Related

PHP Fatal error: Call to undefined function?

So i have a problem with my website when im hosting it on my webhost.
i get this error:
PHP Fatal error: Call to undefined function getSkillIcons()
The weird thing is that locally(Xampp) it works just fine.
This is how i included the function(in index.php):
<?php include 'http://sub.website.com/incl/Settings.php'; ?>
this is where i call it (index.php):
<div class="panel-body" style="text-align:center">
<?php getSkillIcons(explode(',', skills)); ?>
</div>
This how i defined skills (settings.php)
define("skills", "Test1,Test2,Test3,Test4");
this is the function itself: (settings.php)
function getSkillIcons($skills) {
echo '<img src="http://sub.website.com/incl/img/skill_icons/Overall-icon.png" class="skill-icon">';
for ($i = 0; $i < count($skills); $i++) {
$tooltip = 'title="'.$skills[$i].'" data-toggle="tooltip" data-placement="top"';
echo '';
}
Call to undefined function error clearly shows that its not getting your function where you have defined. Reason is you are attaching full path of settings.php file with http.
You need to include settings.php file without http path at the top of 'index.php' and make sure settings.php file is located in your project only. If it is located at the same folder with index.php, then simply include like below.
<?php include __DIR__.'/settings.php'; ?>
If your settings.php file is located in some other folder then you can use $_SERVER['DOCUMENT_ROOT'] to include that file like below:
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$path .= "/yourpath/settings.php";
include_once($path);
Also just an FYI for anyone trying to call a function within the same class in PHP, refer to the function like below:
return $this->doSomething($str);

PHP Variable to Handle Web Properties

i look around the web for a example of manager PHP Variable think on properties of the web application .
the main problem is that i don't like to write igual that this code:
Link
I would like to include a file .php With class; up with the list of variables, something like this:
$MSGdisplay = '';
$MSGemail = '';
$MSGnotification = '';
and use them anywhere in the script with a simple call:
$G['MSGdisplay'] = 'This is an example of code'; //more short that $_GLOBALS array
without losing the ability to assign new values.
Example:
file index.php
<?php
require_once("main.php");
global $G;
$G['test'] = 'Text Test';
$WebAPP = new Class_MAIN();
$WebAPP -> Main();
?>
file Main.php
<?php
class Class_MAIN{
function Main(){
echo $G['test'];
}
}
?>
Notice: Undefined variable: G in main.php on line 4
Remove global $G; from index.php and move it inside of the Main function in Main.php
<?php
class Class_MAIN{
function Main(){
global $G;
echo $G['test'];
}
}
?>
can solve create a file vars.php with content variable:
<?php
$G['test'] = 'text test';
?>
an create a callback file varscall.php with content variable call glabal
<?php
global $G;
?>
Use Require instance file in Index on top:
<?php
require("vars.php");
require_once("main.php");
$WebAPP = new Class_MAIN();
$WebAPP -> Main();
?>
Use CallBack Vars File igual and use variable inside the function you need:
<?php
class Class_MAIN{
function Main(){
require("varscall.php");
echo $G['test'];
}
function Other(){
require("varscall.php");
echo $G['test'];
}
}
?>
The main problem is that you need repeat per funcion the require of the file call: require("varscall.php"); and need create this as object... is not eficient

How aright use globals elements

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;
}

require_once of function file in a header

ive got an global include/header.php file like:
UPDATE
folder structure :
/
include/
header.php
functions.php
content/
show.php
include/header.php
<?php
require_once('functions.php');
$settings = blaa;
....
?>
include/functions.php
<?php
function hello()
{
echo "hello world";
}
?>
and now a content file like content/show.php
content/show.php
<?php
require_once('../include/header.php');
echo "show page want to say: ";
hello();
?>
and now if i look in the apache error_log call to undefined function in content/show.php on line...
i cannot find out why :-/
Greetings
If you tweak your header.php code to this:
<?php
define('INCDIR', str_replace('\\', '/', dirname(__FILE__)));
require_once(INCDIR . '/functions.php');
It wont matter where you include it from it will constantly find the right path to the include folder

require_once() function unable to include functions()

index.php
<?php
require_once('fr.php');
header('Location:'.abspath().directory());
?>
fr.php
<?php
require_once('functions.php');
?>
functions.php
function abspath()
{
return $_SERVER['DOCUMENT_ROOT'];
}
Now when i go to index.php, it gives me this error:-
Fatal error: Call to undefined function abspath() in C:\xampp\htdocs\index.php on line 3
Hey all my commenting people were right... i don't know why it happened but when i restarted my xampp it worked fine for me :)
So this is 100% correct
index.php
<?php
require_once('fr.php');
header('Location:'.abspath().directory());
?>
fr.php
<?php
require_once('functions.php');
?>
functions.php
function abspath()
{
return $_SERVER['DOCUMENT_ROOT'];
}

Categories