PHP Warning:  Use of undefined constant

Posted on 2004-09-15
Last Modified: 2008-02-01
How to avoid Warning logs for an undeclared variables in PHP?

I have used many variables as,

$_SESSION[dbConnectInfo][host] = "localhost";
$_SESSION[dbConnectInfo][username] = "root";

and so on.

Errors are as follows,
[Wed Sep 15 04:42:23 2004] [error] PHP Warning:  Use of undefined constant dbConnectInfo - assumed 'dbConnectInfo' in /../../../php/master.php on line 17

How to avoid these error logs. PLease give solutions for this to avoid. But I do not want to supress warning logs.
Question by:isas_n
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 32

Expert Comment

ID: 12065269
>> PHP Warning:  Use of undefined constant dbConnectInfo - assumed 'dbConnectInfo'

The error is here: $_SESSION[dbConnectInfo][host]

dbConnectInfo and host not variables because there is no $ symbol. They are not strings because there is no ' or " enclosed.

It should be "host" and "dbConnectInfo".

But, why do you want to store the host and username in SESSION? Are you trying to keep it in session and use it in the required pages. If so, SESSION is not really the best way. Just do the DB Connection in a PHP and include it in the required pages.
LVL 32

Expert Comment

ID: 12065316
even then, why 2-D for sessions !? this should be enough:

$_SESSION["dbConnectInfo_host"] = "localhost";
$_SESSION["dbConnectInfo_username"] = "root";

Author Comment

ID: 12071732
Hi, ldbkutty

I'm trying to make changes in the code. I will let you know after some time.
For example,

$_SESSION[webImgPath] = $_SESSION[siteURL]."/userdata/uploadedimages";

In Most of the pages we are using, the variable,

So we can store it in a session, so that it will be used wherever required.

Also we are using only sessions in our program,
eg. Storing field values. That field value will be used in different forms.

Is there any alternate way for this?

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 32

Expert Comment

ID: 12077749
>> Also we are using only sessions in our program, eg. Storing field values
>> Is there any alternate way for this?

If you just want to pass the form-fields to the next page, POST method is enough to retrieve in the next page.
POST carries the field values to the next page, SESSIONS carry all over the pages.

>> $_SESSION[webImgPath]

This is not the right format for SESSIONS. you should have it as: $_SESSION['webImgPath'] or $_SESSION["webImgPath"].

With $_SESSION[webImgPath], If your warnings are suppressed in your php.ini(which is not secure), you wont get any warnings in your output page. Otherwise it will show errors like you got.

So the right format is:

$_SESSION["webImgPath"] = $_SESSION["siteURL"]."/userdata/uploadedimages";

I hope you understand.
LVL 32

Accepted Solution

ldbkutty earned 500 total points
ID: 12077777
wherever you have $_SESSION[someVar], change it to $_SESSION["someVar"].

One more suggestion. There a re a lot of PHP Experts who dont really look in this 'Programming' section. If you post your Question here, you will get fast and best responses:

Author Comment

ID: 13652072

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

734 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