Passing parameters to require()'d functions

C++, C#, Java, PHP, ect...
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Passing parameters to require()'d functions

Post by Jackolantern »

How exactly does this work? I am still getting to know PHP, and I was excited to see require() and include() as ways to reuse existing code. But if you are including a function, how do you call it with a parameter? For example:

Main script:

Code: Select all

<?php
echo 'Welcome to the main script!';
require('needthis.php');
$neptune = 18;
display_this($neptune);
echo 'Thank you for using this script!';
?>
needthis.php:

Code: Select all

<?php
function display_this(int $valueIn) {
echo "The input value is ".$valueIn."!!";
}
This does not work, and says that I am not sending an integer to the function. How do you do this? Thanks!
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Passing parameters to require()'d functions

Post by hallsofvallhalla »

all variables defined outside of a function are global, no need to pass. All variables inside a function can only be used in that scope unless you define them as global. So in your case no need to pass the variable.
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Passing parameters to require()'d functions

Post by hallsofvallhalla »

oh i misread, you want to pass it to the required file :oops:

use get method...needthis.php?neptune=$neptune

then in needthis

$neptune = $_GET['neptune']
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Passing parameters to require()'d functions

Post by Jackolantern »

I appreciate the help! So it would be written like this?

Main script:

Code: Select all

<?php
echo 'Welcome to the main script!';
$neptune = 18;
require('needthis.php?nepture=$neptune');
display_this();
echo 'Thank you for using this script!';
?>
needthis.php:

Code: Select all

<?php
$neptune = $_GET['neptune']
function display_this(int $neptune) {
echo "The input value is ".$neptune."!!";
}
I guess I don't really get it, as I don't seem to be putting things in the place where they need to be. There is no way to do it without having to re-write anything in the needthis.php file? Thanks again!
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Passing parameters to require()'d functions

Post by hallsofvallhalla »

ok wait, i got confused. You dont have to pass anything through a require. Take out the variables in () and try it. Also take out the get method. Sorry I looked at this wrong.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Passing parameters to require()'d functions

Post by Jackolantern »

I know that I could just use the variables by name in the needthis.php function since they are global, but that would require re-writing the function. Is there any way to to setup the code to function like in my first post, where I can call the function in the required file like "display_code($value)"? If I could, that would mean I don't have to do any re-writing of the function itself, making it much easier to re-use. Maybe I am still not getting what you are saying (I am having a hard time with some of this stuff, a lot of it coming from this being the first weakly-typed language I have worked with). Thanks again :)
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Passing parameters to require()'d functions

Post by hallsofvallhalla »

you can sill use the variable in the function without having to pass it. The variable is global.

Code: Select all

function display_this() 
{
echo "The input value is " . $neptune . "!!";
}
does that not work?
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Passing parameters to require()'d functions

Post by Jackolantern »

It does, and it would work quite well in the example I gave, but if the example function lists the variable name 200 or 300 times, it could be difficult. You could use search and replace, but again, you are localizing the code to this one use, thus breaking the re-usability of the function. Many languages allow code re-use without requiring any kind of re-write of the existing function, like if I could just call it with "display_this($neptune)", but the function prototype is "function display_this(int $variable){}". If it could be done somehow that way, it would not require the changing of any variable names in the function. That way, you could seamlessly use tens or hundreds of functions in other files without having to re-write anything in them. That's what I was wondering if PHP had :)

EDIT: Ohh, and what if I needed to call the function multiple times with different variables each time? I am wondering if maybe require() and include() are not what I am looking for, but what I have been reading about PHP make it sound like it is. There just has to be a better way, though.
The indelible lord of tl;dr
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Passing parameters to require()'d functions

Post by Jackolantern »

Figured it out! I was just setting up the function incorrectly:

Code: Select all

<?php
echo 'Welcome to the main script!';
require('needthis.php');
$neptune = 18;
display_this($neptune);
echo 'Thank you for using this script!';
?>
needthis.php:

Code: Select all

<?php
function display_this($valueIn) {
echo "The input value is ".$valueIn."!!";
}
I was declaring the type of the parameter in the function like I am used to. Freaking weakly-typed language. It now works :D
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Passing parameters to require()'d functions

Post by hallsofvallhalla »

ah i see what your saying..i had a real dumb moment :)

Noc might better answer this due to I dont use functions in PHP very often and when I do passing them works for me but I also haven't gotten into the require file that much. I use Javascript for my functions. But off the top of my head I can think of a alternative.

Make a variable

lets say $dthis

then in your function
display_this()
{
echo $dthis;
}

now before you pass any variable to it simply define $dthis
example:

$a = 10;
$b = 10;

$dthis = $a;
display_this();
$dthis = $b;
display_this();

that will print out
10
20

hope that helps and you have raised a question I must research for I am curious now too.


edit: or actually it will print out 1020 becasue I didnt break the line :)
Post Reply

Return to “Coding”