0

I have a newbie issue with PHP functions calls. File:

    <?php
        session_start();
        include("../conexionbbdd.php");
        include("../conexionapi.php");

        $id = $_SESSION['id'];   
        $inclass = $_SESSION['inclass'];   

    if($_SESSION['estado'] == 'activo'){

        if($inclass==='1'){       
            checkCost();        
        }
        else{
            sendMessage();        
        }

function checkCost(){
    //DO WHATEVER
}

function sendMessage(){
    //DO WHATEVER
}


}else{

header('location:../login.php');

}
?>

Console emerges an error ( ! ) Fatal error: Call to undefined function checkCost() in C:\wamp\www[removedbyme]\actions\msg_newMessage.php on line 14

1
  • 1
    put the function before it's call, especially in case it's not at top-level.. Commented Sep 11, 2015 at 10:25

3 Answers 3

3

Functions must be declared before they are used.

This will work:

function doSomething(){

}

doSomething();

This won't:

doSomething();

function doSomething(){

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

1 Comment

Thank you. I'lll tick in 3 minutes
1

Try this:

       session_start();
        include("../conexionbbdd.php");
        include("../conexionapi.php");

function checkCost(){
    //DO WHATEVER
}

function sendMessage(){
    //DO WHATEVER
}


        $id = $_SESSION['id'];   
        $inclass = $_SESSION['inclass'];   

    if($_SESSION['estado'] == 'activo'){

        if($inclass==='1'){       
            checkCost();        
        }
        else{
            sendMessage();        
        }



}else{

header('location:../login.php');

}

Comments

0

PHP code is read from top to bottom, so when you call checkCost, php is not aware that checkCost exist:

<?php
    session_start();
    include("../conexionbbdd.php");
    include("../conexionapi.php");
    function checkCost(){
    //DO WHATEVER
    }

    function sendMessage(){
    //DO WHATEVER
    }
    $id = $_SESSION['id'];   
    $inclass = $_SESSION['inclass'];   

    if($_SESSION['estado'] == 'activo'){

       if($inclass==='1'){       
           checkCost();        
       }
       else{
           sendMessage();        
       }

    }else{

    header('location:../login.php');

   }
?>

Comments

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.