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
Related
I need one help. I need to pass some parameter to another php file and display it using PHP. I am explaining my code below.
user.php:
<?php
$product_id=$_POST['product_id'];
?>
I need to pass this parameter ($product_id) to the following file by calling it.
saveUser.php:
<?php
$newCustomerobj->product_id =$_REQUEST['product_id'];
print_r($newCustomerobj);
?>
Here i need to pass the value from user.php to saveUser.php.Please help me.
If it need not be secure, then pass it as a query string like saveUser.php?id=product_id and access it using $_GET['id'].
If you write a function as following
public function functionName($product_id, $variable 2,...)
{
require_once ('saveUser.php');
}
Then usually all variable will get in saveUser.php file. I don't know if it can help you.
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 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
From what I understand using something like require_once will essentially copy and paste the code from one file into another, as if it was in the first file originally.
Meaning if I was to do something like this it would be valid
foo.php
<?php
require_once("bar.php");
?>
bar.php
<?php
print "Hello World!"
?>
running php foo.php will just output "Hello World!"
Now my question is, if I include require_once inside a method, will the file that is included be loaded when the script is loaded, or only when the method is called?.
And if it is only when the method is called, is there any benefit performance wise. Or would it be the same as if I had kept all the code into one big file.
I'm mainly asking as I've created an API file, which handles a large amount of calls, and I wan't to simplify the file. (I know I can do this just be creating separate classes, but I thought this would be good to know)
(Sorry if this has already been asked, I wasn't sure what to search for)
It will only include when the method is called, but have you looked at autoloading?
1) Only when the method is called.
2) I would imagine there's an intangible benefit to loading on the fly so the PHP interpreter doesn't have to parse extra code if it's not being used.
I usually use the include('bar.php'); i use it for when i use databvase information, i have a file called database.php with login info and when the file loads it calls it right up. I don't need to call up the function. It may not be the most effective and efficient but it works for me. You can also use include_once... include basically does what you want it to, it copies the code essencially..
As others have mentioned, yes, it's included just-in-time.
However, watch out for variable definitions (require()ing from a method will only allow access to local variables in that method's scope).
Keep in mind you can also return values (i.e. strings) from the included file, as well as buffer output with ob_start() etc.
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 ...