I have two files:
In a.php:
<?php echo "one"; include 'b.php'; echo "three"; ?>
In b.php:
<?php echo"one"; echo"two"; echo"three"; echo"four"; echo"five"; ?>
My question is how get only echo"two"; from b.php and what I must write in a.php?
To include other PHP files into another, add this to your code
include('otherfile.php');
You can't just retrieve a port of the code, including the file will execute the whole file. You could use function do this:
// File function.php
<?php
function a() {
echo 'one';
}
function b() {
echo 'two';
}
// File index.php
include('function.php');
a(); // will echo one
Here's a proper way to accomplish what you want.
Related
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
I have the following two files. The first a long php script, the second just a handful of variables:
File1.php:
<?php
...
function abc()
{
...
include "File2.php";
echo "$x $y $z";
...
}
?>
File2.php:
<?php
$x=1;
$y=2;
$z=3;
?>
None of the variables appear inside my function in the echo statement - using xdebug I step through it including the second file, each variable appears correct, but when it leaves File2.php, it loses all the variables. Why would that be?
I've stripped the first file to get rid of everything but the above - and it works fine. I'm not sure where to start looking in the thousands of lines of code for something that could be causing this problem to happen.
I've even tried using require/require_once - same problem...
Has anyone come across this before or have any thoughts how to debug it?
File2.php
<?php
$x=1;
$y=2;
$z=3;
?>
an if you need to call file2.php inside file1.php, you have to use include() function as well
Correct way of using it
include ('File2.php'); //correct
include 'File2.php'; //correct
include File2.php; //wrong
File1.php
<?php
function abc()
{
include ('File2.php');
echo "$x $y $z";
}
?>
Try out this :-
File1.php:
<?php
include ('File2.php');
echo $x;
echo '<br>';
echo $y;
echo '<br>';
echo $z;
?>
File2.php
<?php
$x=1;
$y=2;
$z=3;
?>
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.
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;
}
I understand that the question is rather hard to understand, I didn't know how to ask it better, so I'll use this code example to make things more clear:
If I have the following files:
test.php:
<?php
include('include.php');
echo myClass::myStaticFunction();
?>
include.php
<?php
__autoload($classname){
include_once("class/".$classname.".php"); //normally checking of included file would happen
}
?>
class/myClass.php
<?php
class myClass{
public static function myStaticFunction(){
//I want this to return test.php, or whatever the filename is of the file that is using this class
return SOMETHING;
}
?>
the magic FILE constant is not the correct one, it returns path/to/myClass.php
in case you need to get "test.php" see $_SERVER['SCRIPT_NAME']
I ended up using:
$file = basename(strtolower($_SERVER['SCRIPT_NAME']));
I am using
$arr = #debug_backtrace(false);
if (isset($arr))
foreach ($arr as $data)
{
if (isset($data['file']))
echo $data['file'];
// change it to needed depth
}
This way you don't need to modify the file from which your file is included. debug_backtrace might have some speed consenquencies.