1

I have a file which is session secured "admin.php". This is default page after login. This throwing an error given below:

Fatal error: Call to undefined function listPages() in C:\xampp\htdocs\cd-website\cms\admin.php on line 14

<?php
/*
* initialize session for admin
*/
session_start();

if(isset($_SESSION['admin_user']))
{
    require_once '../cms/config.php';

    $action = isset($_POST['action']) ? $_POST['action'] : '';

    if ($action == null)
    {
        listPages();
        exit;
    }

    /*
     * using switch for choosing function
     */
    switch($action)
    {
        case 'ManagePages':
            ManagePages();
            break;
        case 'listUsers':
            listUsers();
            break;
        case 'orderList':
            listOrders();
            break;
        case 'listBanner':
            listBanners();
            break;
        case 'NewsletterUser':
            NewsletterUsers();
            break;
        case 'Newsletter':
            Newsletter();
            break;
        case 'listQuestion':
            listquestions();
            break;
        case 'testinomial':
            listTesti();
            break;
        default:
            listPages();
    }

    /*
     * different function for different tasks
     */
    function ManagePages()
    {
        listPages();
    }

    function listUsers()
    {
        // include listusers.php here. required rows is in listusers.php file
    }

    function listOrders()
    {
        // include listorders.php here
    }

    function  listBanners()
    {
        // include listbanners.php here
    }

    function  NewsletterUsers()
    {
        // include listNUsers.php here
    }

    function listquestions()
    {
        // include listquestions.php here.
    }

    function listTesti()
    {
        // include listTesti.php here.
    }

    function Newsletter()
    {
        //  include newsletter.php
    }

    function listPages()
    {
        //  include listPages.php here
    }
}
else
{
    header("Location:index.php");
}

When i try to solve error on line 14, it shows error on every line where listPages() exist.

Why it so? Please help!

5
  • doesn;t seem any error though its very complex way of coding Commented Oct 23, 2012 at 5:20
  • no error found ; see working demo with minimal setting Commented Oct 23, 2012 at 6:19
  • @diEcho if there were no error in the code why i will post here. I am not mad and not the other people who are responding. Think a bit before writing anything. Commented Oct 23, 2012 at 6:38
  • i am not saying that you posted wrong. I said that i have tried on my end and it seems perfect. You can see the link i have given. Just chill Dhondu. Commented Oct 23, 2012 at 10:43
  • @diEcho hahaha... nice word invented Dhondu! Commented Oct 23, 2012 at 14:18

5 Answers 5

10

You declared a function inside a if block, which can be tricky. According to http://www.php.net/manual/en/functions.user-defined.php, conditional functions, they will not be available until the execution reaches the function definition, but if you place the function outside, the sequence doesn't matter and the function is available throughout the whole script.

<?php
bar(); // OK
function bar()
{
  echo "I exist immediately upon program start.\n";
}

if (TRUE) {
  foo(); // Fails because `foo` isn't defined yet.
  function foo()
  {
    echo "I don't exist until program execution reaches me.\n";
  }
}

And please check http://codepad.org/EMW3kzqC.

So the solution is to declare the function outside the if block, or place them above the location where you use it (if it really needs to reside inside the if block).

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

6 Comments

Thanks for the useful info. I were also thinking same.
no, its not right. PHP parse the complete file not in chunked way like JS
but that if condition is regarding session variable. it session available doesn't exit , that whole code of block will not execute.
assume we are inside first if block then how can u say that listpages() will not run,
there may be some other error. here you can see the working demo : codepad.viper-7.com/abxLmp
|
0

here two solutions are ther one is using the Class, above method if you are using the above method the function should be defined before calling that function.

so

function listPages()
   {
                              //include listPages.php here
   }

after that call listPages()

1 Comment

Hmmm....it working but i have a same type of code which works fine. why it so. By the way thanks Naveen!
0

I'm not exceptionally familiar with php, but it looks like you're defining your functions only within the big if block there. You might be running into the way php handles scope.

Try placing your function definitions outside of the if block and see what happens?

Comments

0

Check if Your function is set before calling. In PHP You have to declare functions before calling them.

Your error tells clearly that when You call listPages() this function doesn't exists yet.

I'm sure if You place just after:

if(isset($_SESSION['admin_user']))
{

Declaration of this function

function listPages()
{
    //Do something here
}

Your error will disapear.

What i can see You are trying to use functions in structural code as they would be in some class (object oriented code) and unfortunetly PHP dosen't work that way.

1 Comment

No, in PHP, you don't have to declare functions before calling them. This is misinformation. PHP is not Python !!
-1
<?php
/*
* initialize session for admin
*/
session_start();

if(isset($_SESSION['admin_user']))
{
    require_once '../cms/config.php';

    $action = isset($_POST['action']) ? $_POST['action'] : '';

    if ($action == null)
    {
        listPages();
        exit;
    }


    /*Functions */
    function ManagePages()
    {
        listPages();
    }

    function listUsers()
    {
        // include listusers.php here. required rows is in listusers.php file
    }

    function listOrders()
    {
        // include listorders.php here
    }

    function  listBanners()
    {
        // include listbanners.php here
    }

    function  NewsletterUsers()
    {
        // include listNUsers.php here
    }

    function listquestions()
    {
        // include listquestions.php here.
    }

    function listTesti()
    {
        // include listTesti.php here.
    }

    function Newsletter()
    {
        //  include newsletter.php
    }

    function listPages()
    {
        //  include listPages.php here
    }


    /*
     * using switch for choosing function
     */
    switch($action)
    {
        case 'ManagePages':
            ManagePages();
            break;
        case 'listUsers':
            listUsers();
            break;
        case 'orderList':
            listOrders();
            break;
        case 'listBanner':
            listBanners();
            break;
        case 'NewsletterUser':
            NewsletterUsers();
            break;
        case 'Newsletter':
            Newsletter();
            break;
        case 'listQuestion':
            listquestions();
            break;
        case 'testinomial':
            listTesti();
            break;
        default:
            listPages();
    }

    /*
     * different function for different tasks
     */

}
else
{
    header("Location:index.php");
}

1 Comment

For future reference, if you answer a question, please include an explanation as to what you have changed in the code provided. Otherwise, how do we know what you have corrected and changed?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.