springo Posted March 20, 2007 Share Posted March 20, 2007 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 More sharing options...
qhiiyr Posted March 20, 2007 Share Posted March 20, 2007 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 More sharing options...
pocobueno1388 Posted March 20, 2007 Share Posted March 20, 2007 Here are some good tutorials: <a href="http://www.w3schools.com/php/php_functions.asp">Tutorial 1</a> <a href="http://www.php-mysql-tutorial.com/php-tutorial/php-functions.php">Tutorial 2</a> <a href="http://www.tizag.com/phpT/phpfunctions.php">Tutorial 3</a> Link to comment https://forums.phpfreaks.com/topic/43516-solved-functions/#findComment-211321 Share on other sites More sharing options...
per1os Posted March 20, 2007 Share Posted March 20, 2007 <?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 More sharing options...
springo Posted March 22, 2007 Author Share Posted March 22, 2007 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 More sharing options...
trq Posted March 22, 2007 Share Posted March 22, 2007 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 More sharing options...
per1os Posted March 22, 2007 Share Posted March 22, 2007 Those should work just fine. And yes, PHP is a very laid back language compared to a lot of others. Thats why I love it. Link to comment https://forums.phpfreaks.com/topic/43516-solved-functions/#findComment-213137 Share on other sites More sharing options...
springo Posted March 23, 2007 Author Share Posted March 23, 2007 OK, thanks again! Link to comment https://forums.phpfreaks.com/topic/43516-solved-functions/#findComment-213148 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.