PHP - need help with Fatal error: Call to undefined function

Posted on 2009-04-24
Last Modified: 2012-05-06
I have 2 files that are used on a particular page via an include statement.

cart_totals.php contains the following:
function GetCartId()
            // This function will generate an encrypted string and
            // will set it as a cookie using set_cookie. This will
            // also be used as the cookieId field in the cart table
                  return $_COOKIE["cartId"];
                  // There is no cookie set. We will set the cookie
                  // and return the value of the users session ID
                  setcookie("cartId", session_id(), time() + ((3600 * 24) * 30));
                  return session_id();

cart_functions.php contains the following:
function RemoveItem($PID,$spec) {
            // Uses an SQL delete statement to remove an item from
            // the users cart

            global $conn_data;
            mysql_query("delete from cart where cookieId = '" . GetCartId() . "' and PID = $PID and spec = '$spec'");


I am receiving the following error :
Fatal error: Call to undefined function getcartid() in /home/ourstore/public_html/includes/cart_functions.php on line 46

If I then go and add the function GetCartId() to my cart_functions.php page I receive the following error:
Fatal error: Cannot redeclare getcartid() (previously declared in /home/ourstore/public_html/includes/cart_functions.php:9) in /home/ourstore/public_html/includes/cart_totals.php on line 28

Does anyone know the best way to correct this problem, can I make a function global?
Question by:sabecs
    LVL 39

    Accepted Solution

    Functions are global, but you can not define the same function twice. When you added it to cart_functions.php you should remove it from cart_totals.php. As an alternative, you could keep it in cart_totals.php, but include this file before you include cart_functions.php, so that the function is defined before you use it.

    Author Closing Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now