Jump to content

[SOLVED] Functions...


springo

Recommended Posts

Hi,

I'd like to ask two questions:

1) How can I include functions located on other files?

2) How can I make a function modify the values of passed variables? (I'm guessing there's something with references, but I'm not familiar with this concept on PHP, I just know C++ pointers, so if there's any good paper on it, please let me know)

Thank you very much!

Link to comment
https://forums.phpfreaks.com/topic/43516-solved-functions/
Share on other sites

For #1, you can use the include(), require(), include_once() or require_once() statements. They all include other files, but they're a little different. include() will output a warning if it doesn't find the file, but require() will stop all loading on the page if it doesn't find the file. The _once suffixes to both functions will make sure that the file is never included more than once, i.e. if the call is in a loop.

 

EDIT: Sorry, forgot to give an example. You'd do something like:

 

require_once("function.php");

 

I'm not sure what you mean by #2. Are you asking how you pass variables to functions, or actually just how to change them? I'm not familiar with C++, but in PHP you can change variables passed to functions right off the bat; there're no limitations (of course, you don't change the actual variable that was used to pass that value to you, just the one you've been handed).

 

Let me know if I don't know what you mean.

Link to comment
https://forums.phpfreaks.com/topic/43516-solved-functions/#findComment-211320
Share on other sites

<?php
include('functions.php'); // include functions in another file.

function passedModify(&$modifyMe) {
     $modifyMe = "test";
}

$myTest = "How do you test me?";
passedModify($myTest);
print $myTest;
?>

 

http://us2.php.net/manual/en/language.references.pass.php

Link to comment
https://forums.phpfreaks.com/topic/43516-solved-functions/#findComment-211322
Share on other sites

Thanks for your answers!

 

I've still got one question for #1: would these work?

 

includeme.php

<?php
function foo() {
echo "foo()";
}
?>

 

index.php

<?php
include("includeme.php");
foo();
?>

 

OK I just understood how references work here in PHP. It's way easier than C++, thanks God!

Link to comment
https://forums.phpfreaks.com/topic/43516-solved-functions/#findComment-213132
Share on other sites

I've still got one question for #1: would these work?

 

includeme.php

<?php
function foo() {
echo "foo()";
}
?>

 

index.php

<?php
include("includeme.php");
foo();
?>

 

Yes. Also note that include is not a function but a language construct. It does not need the () braces, though it works either way.

 

include 'filetoinclude.php';

Link to comment
https://forums.phpfreaks.com/topic/43516-solved-functions/#findComment-213136
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.