I'm calling a file named ajax.php (from my browser for testing)
ajax.php require_once delete.php
delete.php require_once no_direct.php
delete.php starts like this:
$allowed = array('group'=>'admin');
require_once(ASSETS.'/no_direct.php'); //Yes, ASSETS is defined and no_direct is being included.
In no_direct.php I'm trying to:
var_dump($allowed)
and I just keep coming up NULL.
Does this happen because we are running inside ajax.php's require_once function and the scope of $allowed pushes back to the GLOBAL scope not allowing me to access it from no_delete.php?
I was looking here: PHP variable defined in 'parent' file not recognized in 'required' file , just to be diligent.
I'm sure I could solve this with the GLOBAL keyword, but I was hoping for a little clarification. The PHP scope doc didn't seem to answer the question for me.
It wasn't wrapped in another function as thought to be the case.
Is there any chance that you already have called require_once(ASSETS.'/no_direct.php'); before you assigned value to $allowed?
require_once(ASSETS.'/no_direct.php');
...
$allowed = array('group'=>'admin');
require_once(ASSETS.'/no_direct.php');
Script no_direct.php should not output $allowed in this case.
Output will be:
Notice: Undefined variable: allowed in D:\wampserver\www\gim\no_direct.php on line 2
NULL
p.s. there's my path on localhost in wamp for my test file
Does this happen because we are running inside ajax.php's require_once function and the scope of $allowed pushes back to the GLOBAL scope not allowing me to access it from no_delete.php?
Definitely not.
There are NO scope issues regarding includes.
The only scope-dependent issue is user-defined functions.
So, if there are no functions involved, the only cause can be some mistake/mistype - you're editing/including wrong file, or including HTML code it via http or something of the kind. Just double-check.
So my ajax.php file also require_once build.php and config.php
My build.php file require_once no_direct.php as well.
As the function suggests, it will only require no_direct.php ONCE!
So the NULL that I was seeing was coming from build.php's include and not the delete.php's include of no_delete.
Related
I am trying to do the following:
Open a file, say "myfile.json" from a php- let's call it "utils.php"; Use it in other php pages; close it from another php.
I have tried to include "utils.php" in the other files and write in the utils file, but it does not seem to work. I suppose this happens because utils.php is never actually executed, only included, but if I should execute it, how can I do it without having to refresh any page, preferably right when the user gets on the main page? This should not be seen by the user, what he sees should remain the main page.
Thanks in advance, I am quite new to php, and am trying to learn.
When you include a file, you are running all code inside it. The functions and classes will not be evaluated but will be defined for future use. If you open your file as this example:
util.php
<?php
$file_hand = fopen('/tmp/file.txt','r');
You will have a handle if the operation is completed. However, the variable $file_hand is global. If you need to use a function to close it, you will need the following code to do it:
other.php
function close_file(){
global $file_hand;
fclose($file_hand)
}
or you can pass the handle as parameter like:
function close_file($file_hand){
fclose($file_hand)
}
Doesn't matter how you will close the file. You have to make sure the variable you are using is the same created in utils.php. If you close like this:
function close_file(){
fclose($file_hand)
}
The variable you've created in until.php file is different of this one.
i am very new to php, i am used to writing .net, and i am finding the includes hard to understand and was hoping someone could help me understand how to correctly use an include once in a file, rather than inside each function..
take the following as an example
<?php
include 'test.php';
function test($a)
{
echo $value_from_test_php;
}
?>
the above code does not seem to work... however the below does
<?php
function test($a)
{
include 'test.php'
echo $value_from_test_php;
}
?>
i am having a hard time figuring out how to make an include work for all functions inside a file, rather then including it inside each function, any advice is greatly appreciated!
It's the scope of variable which is troubling you rather than includes, in PHP generally includes are used where there's a common page/markup to be included on each page, such as footer, header, etc
There are 4 types
include
include_once
require
require_once
The only difference is include will throw you an error if something goes wrong and will continue to execute the script where require will halt the further execution
You'll get everything here on includes - PHP Documentation
It all depends what is inside the file that you are include-ing! I would never, ever, suggest using include inside a function (or loop, or pretty much anything with brackets). Remember, the contents of the file being included are literally just "plopped in" place, right where the include statement is. So whatever scope (global, class, function, etc.) you're in when you include, is the scope that its contents will be declared in.
Put full class and function definitions in files, and include them at the top of the files where they are going to be used.
Your issue is not related to includes, but rather variable scope. By default a variable defined outside a function is not available within the function.
It's difficult to suggest the best solution without knowing exactly what it is you're trying to do, but the documentation (linked above) should get you started.
First example is not working because you use variable from global scope, if you want to use it then replace $value_from_test_php to $GLOBALS['my_var_name']
I am trying to call a function from another function. I get an error:
Fatal error: Call to undefined function getInitialInformation()
in controller.php on line 24
controller.php file:
require_once("model/model.php");
function intake() {
$info = getInitialInformation($id); //line 24
}
model/model.php
function getInitialInformation($id) {
return $GLOBALS['em']->find('InitialInformation', $id);
}
Things already tried:
Verified that the require_once works, and the file exists in the specified location.
Verified that the function exists in the file.
I am not able to figure this out. Am I missing something here?
How to reproduce the error, and how to fix it:
Put this code in a file called p.php:
<?php
class yoyo{
function salt(){
}
function pepper(){
salt();
}
}
$y = new yoyo();
$y->pepper();
?>
Run it like this:
php p.php
We get error:
PHP Fatal error: Call to undefined function salt() in
/home/el/foo/p.php on line 6
Solution: use $this->salt(); instead of salt();
So do it like this instead:
<?php
class yoyo{
function salt(){
}
function pepper(){
$this->salt();
}
}
$y = new yoyo();
$y->pepper();
?>
If someone could post a link to why $this has to be used before PHP functions within classes, yeah, that would be great.
This was a developer mistake - a misplaced ending brace, which made the above function a nested function.
I see a lot of questions related to the undefined function error in SO. Let me note down this as an answer, in case someone else have the same issue with function scope.
Things I tried to troubleshoot first:
Searched for the php file with the function definition in it. Verified that the file exists.
Verified that the require (or include) statement for the above file exists in the page. Also, verified the absolute path in the require/include is correct.
Verified that the filename is spelled correctly in the require statement.
Echoed a word in the included file, to see if it has been properly included.
Defined a separate function at the end of file, and called it. It worked too.
It was difficult to trace the braces, since the functions were very long - problem with legacy systems. Further steps to troubleshoot were this:
I already defined a simple print function at the end of included file. I moved it to just above the "undefined function". That made it undefined too.
Identified this as some scope issue.
Used the Netbeans collapse (code fold) feature to check the function just above this one. So, the 1000 lines function above just collapsed along with this one, making this a nested function.
Once the problem identified, cut-pasted the function to the end of file, which solved the issue.
Many times the problem comes because php does not support short open tags in php.ini file, i.e:
<?
phpinfo();
?>
You must use:
<?php
phpinfo();
?>
Your function is probably in a different namespace than the one you're calling it from.
http://php.net/manual/en/language.namespaces.basics.php
I happened that problem on a virtual server, when everything worked correctly on other hosting.
After several modifications I realized that I include or require_one works on all calls except in a file.
The problem of this file was the code < ?php ? > At the beginning and end of the text.
It was a script that was only < ?, and in that version of apache that was running did not work
This is obviously not the case in this Q,
but since I got here following the same error message I though I would add what was wrong with my code and maybe it will help some one else:
I was porting code from JS to PHP and ended up having a class with some public method.
The code that was calling the class (being code that originated from JS) looked something like:
$myObject.method(...)
this is wrong because in PHP it should look like this:
$myObject->method(...)
and it also resulted with "PHP Call to undefined function".
change to use -> and the problem was solved.
Presently I am working on web services where my function is defined and it was throwing an error undefined function.I just added this in autoload.php in codeigniter
$autoload['helper'] = array('common','security','url');
common is the name of my controller.
Please check that you have <?PHP at the top of your code. If you forget it, this error will appear.
I read a post here that the person wrote a statement like :
$this->_connection = require_once 'config.php';
// $this->connection is an array variable.
I find it a little bit hard to understand. Am asking myself how can you assign an included file to a variable.
Does it mean that an array must be returned from the "config.php" file? I mean should "config.php" return an array?
Is such statement good in commercial php applications?
Thank you.
The included file may have a return statement outside of any function. If this happens, the script stops running the included file and the "return value" of the require_once call is the value of the return statement.
Docs
I have a file, lets say it's index.php where the very beginning of the file has an include for "include.php". In include.php I set a variable like this:
<?php $variable = "the value"; ?>
then further down the in index.php I have another include, say "include2.php" that is included like this:
<?php include(get_template_directory_uri() . '/include2.php'); ?>
How can I call the "$variable" that I set in the first include, in "include2.php"?
The exact code that I am using is as follows:
The very first line of the index.php I have this line
<?php include('switcher.php'); ?>
Inside switcher.php I have this
<?php $GLOBALS["demo_color"] = "#fffffe"; ?>
If I use this in index.php, it works
<?php echo $GLOBALS["demo_color"]; ?>
However, If I use the following code to include another php file
<?php include(get_template_directory_uri() . '/demo_color.php'); ?>
then inside demo_color.php I have this code:
<?php echo "demo color:" . $GLOBALS["demo_color"]; ?>
The only thing it outputs is "demo color:"
edited for code-formatting
It simply can be used in include2.php, unless the inclusion of include.php happens inside of a different scope (i.e. inside a function call). see here.
If you want to be completely explicit about the intention of using the variable across the app, use the $GLOBALS["variable"] version of it's name, which will always point to the variable called variable in the global scope.
EDIT: I conducted a test against php 5.3.10 to reconstruct this:
// index.php
<?php
include("define.php");
include("use.php");
// define.php
$foo = "bar";
// use.php
var_dump($foo);
This works exactly as expected, outputting string(3) "bar".
<?PHP
//index.php
$txt='hello world';
include('include.php');
<?PHP
//include.php
echo $txt; //will output hello world
So it does work. Though there seems to be a bigger issue since this is likely to be difficult to maintain in the future. Just putting a variable into global namespace and using it in different files is not a best practice.
To make the code more maintainable it might be an idea to use classes so you can attach the variables you need explicit instead of just using them. Because the code around is not showed it is not clear what is your exact need further but it will be likely the code can be put in classes, functions etc. If it is a template you could think about an explicit set() function to send the variable data to the templates and extract() it there.
edit:
In addition based on the information first set your error_reporting to E_ALL and set the display_errors to 1. So you get all errors since the information you placed in your updated question gives indications that a missing variable is used as a constant which should raise errors all over the place.