0

I created a simple sample php file for display some function outputs. here is the code..

<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');

switch (key($_GET)){
    case 'red':
        $printHeader = pageHeader('red','on');
        $printName = hello('Joel');
        break;

    case 'blue':
        $printHeader = pageHeader('blue','off');
        $printName = hello('Duck');
        break;

    case 'yellow':
        //$printHeader = pageHeader('yellow','on');
        break;
}


function hello($name){
    return $name;
}


function pageHeader($header,$onoff){
    if ($onoff == 'on') {
        return $header."page header<br>";
    }
    else {return null;}
}

echo $printHeader;
echo $printName;

?>

This code is working fine without any problems.

When I call 'example.com/php/tipo34.php?red', it shows on the screen:

redpage header
Joel

And when I call 'example.com/php/tipo34.php?blue' it shows on the screen:

Duck

I tried to put the below functions inside of another php file called tipo34-req.php and received the following error:

Fatal error: Call to undefined function pageHeader() in C:\wamp\www\php\tipo34.php on line 8

The code I tried:

<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
    case 'red':
        $printHeader = pageHeader('red','on');
        $printName = hello('samitha');
        break;

    case 'blue':
        $printHeader = pageHeader('blue','off');
        $printName = hello('kalum');
        break;

    case 'yellow':
        //$printHeader = pageHeader('yellow','on');
        break;
}

include 'tipo34-req.php';

echo $printHeader;
echo $printName;

?>

tipo34-req.php code:

<?php
function hello($name){
    global $name;
    return $name;
}

function pageHeader($header,$onoff){ global $header, $onoff
    if ($onoff == 'on') {
        return $header."page header<br>";
    }
    else {return null;}
}
?>

How do I solve this problem? Using the function directly on the file works, but when I put the functions in another php file, it throws the error.

Thanks.

2
  • are you including that other file on the page where you want to use the functions ?php.net/manual/en/function.include.php Commented Oct 9, 2013 at 17:28
  • I want to work functions where Im called these varibles echo $printHeader; echo $printName; but I want to store functions inside of another file.... sorry for bad english :( Commented Oct 9, 2013 at 17:32

2 Answers 2

6

Include your file above its contents usage. PHP is unaware of the functions since they are included later in the code.

include 'tipo34-req.php';

switch (key($_GET)){
    case 'red':
        $printHeader = pageHeader('red','on');
        $printName = hello('samitha');
    break;

    case 'blue':
        $printHeader = pageHeader('blue','off');
        $printName = hello('kalum');
    break;

    case 'yellow':
        //$printHeader = pageHeader('yellow','on');
    break;


}

echo $printHeader;
echo $printName;

?>
Sign up to request clarification or add additional context in comments.

6 Comments

yep this is the problem, above code is sample code of my main project.. I cant change whole template layout... is there any way to do that ? please
yes I tried this. but the problem is my project code layout structure.... I cant change whole code.. plese give me another methode to do this.. thanks for the reply
Not sure I follow. Are you unable to edit the main file for some reason? All functions must be defined before usage. So, you are going to need to include the file earlier in execution one way or another. Are you able to include the file outside of the function?
yes I cant change whole code agian.... thats why I tring to find a way to do this.. in different way
The issue is, you cannot call functions before they are defined. So the inclusion will have to take place first. Perhaps you can provide more details around why this is not an option in your original post?
|
1

Have you tried including the file at the top before calling any of the functions?

3 Comments

yes I tried this. but the problem is my project code layout structure.... I cant change whole code.. plese give me another methode to do this.. thanks for the reply...
Can you add the switch statement to your functions file? Maybe enclose it in a function itself? If you provide more info about your code layout structure we could help, as that seems to be the real issue here.
sorry I cant add switch selection block to function file... :( thanks for the reply

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.