PHP Statement Explanation - php

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

Related

Set parameters when require_once a php script

First, I do not hope this question is too stupid for stackoverflow, but I am quite new to php and do not have much experience.
I have the file page.php and the file sendTestMail.php and I would like to run the file sendTestMail.php with specific parameters.
I am calling sendTestMail.php like that:
require_once WPGAMAIL_PATH.'sendTestMail.php';
And I need to set two array parameters $wp_set and $ga_set.
Any suggestions what a best-practice solution is?
I appreciate your replies!
You have tons of possiblities, however without knowing the structure within your sendTestMail.php I can only give you hints.
You should basically create a function within your sendTestMail.php - e.g.
<?php
function sendTestMail($wp_set, $ga_set) {
/* your actual code ...*/
}
If you now require the script you can simply call the function an pass the parameters
<?php
require_once WPGAMAIL_PATH.'sendTestMail.php';
sendTestMail('john', 'doe'); // or whatever your parameters are
Other examples of how to pass variables to included / required files can be found here
PHP pass variable to include

using includes in overall php rather then each function

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']

very interesting use of return keyword in php

as we know, return keyword will RETURN some value and exit current function. Mean, that this one used only inside some functions.
BUT, I saw some php-dev's use return keyword outside functions, even in index.php file (in root of web server). What is that mean???? By the way, maybe it's logical to require some file inside function, but this style isnt mine.
There's not much more to say than what the docs do.
About the common usage of return:
If called from within a function, the return statement immediately
ends execution of the current function, and returns its argument as
the value of the function call. return will also end the execution of
an eval() statement or script file.
About the less common usage:
If called from the global scope, then execution of the current script
file is ended. If the current script file was included or required,
then control is passed back to the calling file. Furthermore, if the
current script file was included, then the value given to return will
be returned as the value of the include call. If return is called from
within the main script file, then script execution ends. If the
current script file was named by the auto_prepend_file or
auto_append_file configuration options in php.ini, then that script
file's execution is ended.
Its documented somewhere within the manual
// myFile.php
return array( 'foo' => 'bar');
// somewhere else
$config = include 'myFile.php';
echo $config['foo'];
If you use return in the main scope php will leave the file inclusion and use the value as "return value" of the inclusion (include[_once](), require[_once]()).
BUT, I saw some php-dev's use return keyword outside functions, even
in index.php file (in root of web server). What is that mean???
You know the common purpose. But what you are asking is used to prevent code injection in php include files. Take a look at this post which explains it:
Prevent Code Injection in PHP include files
While discussing Coding Standards it was not long ago I argued against
adding ?> at the end of php files. But miqrogroove pointed to me an
interesting aspect why it actually can make sense to have it and an
additional return statement at the end of each file: That one (merely
the return statement) can prevent an attacker to append payload code
to existing PHP files, for example known include files. The
countermeasurement is pretty easy, just add a return statement at the
end of the file. It will end the include “subroutine”:
Example:
/* all the include file's php code */
return;
?>

How can I reference variables from another included file in PHP?

So I'm working on a PHP app and trying to make everything moduler. I have an index.php file that includes other php files. The first file included is settings.php which has my postgres credentials defined so they can be accessed elsewhere. The second file is connect.php that has a function you can pass sql to and it will return $result. The third file has functions that call the sql function and receive $result and parse it. In the third file, I can read the results of the $result however if I try if($result) it breaks and isset/empty have no effect.
Anyone have any ideas on a way to make this work, or is my structure just terrible?
Thanks so much!
Mike
let's say you have the following three files:
inc1.php
<?php
$foo = 'hello';
?>
inc2.php
<?php
echo $foo;
?>
main.php
include('inc1.php');
include('inc2.php');
it should echo "hello". however, passing variables around among files is a bad idea, and can lead to a lot of confusing, hard-to-follow code. If you need to pass variables around, use functions and/or objects so that you can at least see where they are coming from.
beyond that though, it's difficult to tell exactly what your problem is without seeing the code in question.
I would really try to switch to OOP. This makes things a lot of easier. If you just have to deal with classes, their methods and attributes you only have to include the classes and not this choas of functions. So I would recommend, give it a go ...

Variable scope in a required file

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.

Categories