Page 1 of 1
using functions from remote files
Posted: Mon Dec 12, 2005 1:57 pm
by HiddenS3crets
I have a function in functions.php which requires a parameter and want to use it in another file like so:
Code: Select all
include("./functions.php");
$file = basename($_SERVER['SCRIPT_NAME']);
links($file);
the links() function being in functions.php
This is not working though, what am I doing wrong?
Posted: Mon Dec 12, 2005 2:00 pm
by shiznatix
well if the include is not working then you would get 2 errors, 1 would be saying somthing like "could not include file functions.php" and the other would be like "undefined function links on line whatever"
are you getting those errors? is error_reporting set to E_ALL -> error_reporting(E_ALL); ?
I am going to say that its a problem with your function, and not with the include.
Posted: Mon Dec 12, 2005 2:00 pm
by John Cartwright
Whats the links() function supposed to do and look like?
What do you mean its not working?
Any Errors?
Posted: Mon Dec 12, 2005 2:04 pm
by HiddenS3crets
Ok, here's function.php
Code: Select all
<?php
function links($file) {
$index = "/images/index_05.jpg";
$projects = "/images/index_07.jpg";
$gallery = "/images/index_09.jpg";
$contact = "/images/index_11.jpg";
$about = "/images/index_13.jpg";
switch($file) {
case "index.php":
$index = "/images/mo_05.jpg";
break;
case "projects.php":
$projects = "/images/mo_07.jpg";
break;
case "gallery.php":
$gallery = "/images/mo_09.jpg";
break;
case "contact.php":
$contact = "/images/mo_11.jpg";
break;
case "about.php":
$about = "/images/mo_13.jpg";
break;
}
}
?>
Posted: Mon Dec 12, 2005 2:06 pm
by shiznatix
always, always, always, save a backup copy on your own computer. most editors can do this automatically for you whenever you save the file as a remote file.
Posted: Mon Dec 12, 2005 2:33 pm
by HiddenS3crets
I tried out a simple echo function and it works so i'm thinking the problem is because i'm declaring variables in functions.php to be used in index.php which can't be done or something?
ex:
functions.php >
Code: Select all
function display() {
$string = "hey";
}
index.php >
Should this work or no?
Posted: Mon Dec 12, 2005 2:40 pm
by shiznatix
no that would not work
Code: Select all
function display_2()
{
return "display";
}
$string = display_2();
echo $string//outputs -> display
echo display_2();//outputs -> display
in your case I think that you just want to return the variable that you are setting. if you set a variable in a function it does you not good usually unless you return it.
Posted: Mon Dec 12, 2005 2:44 pm
by Chris Corbyn
What hiddens3crets came up with could be made to work using references though

Far more advanced than you run-of-the-mill function stuff by look up "PHP Functions By Reference" in the manual. It's not exactly like that... you do need to pass the argument "by reference" but it still makes changes such as that
