using functions from remote files

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

using functions from remote files

Post 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?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Whats the links() function supposed to do and look like?
What do you mean its not working?
Any Errors?
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Post 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;
    }
}
?>
Last edited by HiddenS3crets on Mon Dec 12, 2005 2:06 pm, edited 1 time in total.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Post 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 >

Code: Select all

display();
echo $string;
Should this work or no?
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 ;)
Post Reply