Jump to content
Sign in to follow this  
j.silver

Notice Error

Recommended Posts

Dear all:

I have the following php line of code:

 

<?php $var2 = 5; ?>

+= : <?php $var2 += 4; echo var2; ?><br />

 

Because of the lack of the dollar sign before var2 following the echo, it spat out the following error:

+= : Notice: Use of undefined constant var2 - assumed 'var2' in C:xampphtdocsTutorialsPHPw3schoolsintegers.php on line 29var2

 

My understanding prior to this was that Notice means something bad in the code but it is not an error and would not stop the code from executing. But the code has not executed in this example. Since the code is not executing, I am not seeing the real difference between Notice and other forms of errors that prevent codes from executing. Any clarification please?

Share this post


Link to post
Share on other sites

You forgot the "$" next to var2 on this line:

echo var2;

In PHP, a variable always has a $ next to it. A constant does not have a $ and it's defined like this:

define('var2', 5);echo var2;

Here's the PHP manual page explaining what constants are:

http://php.net/constants

Share this post


Link to post
Share on other sites

Thanks, Ingolme. But my question was not about what went wrong, it was about the Notice error:

"My understanding prior to this was that Notice means something bad in the code but it is not an error and would not stop the code from executing. But the code has not executed in this example. Since the code is not executing, I am not seeing the real difference between Notice and other forms of errors that prevent codes from executing. Any clarification please?"

Share this post


Link to post
Share on other sites

Notice means the program will continue running with no errors, however, it may not be doing what you expected it to.

 

In this case, it's printing the literal string "var2".

 

I recommend treating notices like any error, it's a good idea to not have PHP giving notices in your code because it means you're using the language incorrectly.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...