Jump to content

j.silver

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?

Link to comment
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

Link to comment
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?"

Link to comment
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.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...