Notices when executing script? - php

Why do I get notices when I execute a php file on my local server? Does that mean there is an error in my file? Notices like:
Notice: Use of undefined constant l - assumed 'l' in C:\xampp\htdocs\weekday.php on line 3
Notice: Use of undefined constant Monday - assumed 'Monday' in C:\xampp\htdocs\weekday.php on line 6*

You are likely getting notices because you have debugging turned on. It probably looks something like this:
error_reporting(E_ALL);
ini_set('display_errors', True);
To define a constant you need to do
define("Monday","value");
Also it is a php convention to name constants with all upper case. I suggest you stick to this convention so you can differentiate a constant from a regular string when reading your code.

These notices are there to tell you exactly where the problem is.
Look at line 3 of weekday.php and you'll see a place where you use l and realise that it's an undefined constant (look up constants)
You probably mean it to be a variable (look up variables - they start with a $ like $l in PHP) or a string constant (look up strings - they are surrounded by quotes like "l" or 'l')
going by the mention of "Monday" I'm going to assume you meant it to be a string.

Yes Notice means you have an error. As you can see it explains where the error is

There's a difference between errors and notices. An error will stop your script running; a notice is PHP telling you that there's something up with your script, and it's guessing what you actually meant.
A notice like:
Notice: Use of undefined constant Monday - assumed 'Monday' in C:\xampp\htdocs\weekday.php on line 6*
Means that you're using something like:
if ($day == Monday) .....
PHP is guess that you mean a string with the text of Monday in it, and it's right.
You don't have to fix notices - your code will still run. But it's definitely a good idea to do so - it'll make sure that your script executes exactly as you intend.

Related

PHP doesn't give me error when I use a variable name without dollar sign

I am having the following code in a php web page:
if (bInserted) echo "abc";
I would expect PHP to give me a syntax error since I forget to add the dollar sign before the variable name. Howver, the page doesn't produce any errors. Instead, it even echoes the "abc" string. I am not really able to understand this.
Your bInserted is interpreted as a constant and, since is not defined (I presume), it is then treated as the string "bInserted", which evaluates to true hence your "abc" is printed.
Anyway, such implicit conversion from constant to string should raise a notice, see the manual.
If you use an undefined constant, PHP assumes that you mean the name
of the constant itself, just as if you called it as a string (CONSTANT
vs "CONSTANT"). An error of level E_NOTICE will be issued when this
happens.
If you really do not see any message, nor on screen nor in the logs, make sure you have set the correct error reporting level, you could for example try
ini_set('display_errors', 1);
error_reporting(E_ALL);
And, by the way, E_ALL is a defined constant in this case!

I have this error "Notice: Use of undefined constant test - assumed 'test'."

Notice: Use of undefined constant test - assumed 'test'.
I am not sure where this error came from. I am using the Widget Logic Plugin and is fully updated, but I can't seem to find where this issue is. Has anyone had this issue and know how to resolve it?
The most likely answer is that you have missed a $ on a variable called $test and used test in your code somewhere.
This is hard to verify without your code, but the error message you are referring to is what generally happens when a variable is written without the $ at the start - PHP tries to assume it is a constant of the same name.
The second option is that there is an array index 'test' with the missing quotes, i.e. $array[test] instead of $array['test'].
Edit: If you are not writing any code yourself, and using only using plug-ins, you might want to do two things:
See if you can find the error in their code (search for a variable called test without a $ in front of it
Raise a bug on their site, so that they can update it

Why are all single words in php strings

So I came across this feature/bug today and I'm struggling to wrap my head around what is going on.
so we all know that in php this:
echo 'hello';
print 'world';
echo gettype('test');
will return this:
hello
world
string
However, until today I was unaware that this:
echo hello;
print world;
echo gettype(hello);
will return this:
hello
world
string
What is going on here? What is happening in php's compilation process that it sees any single word as a string? Does this have a name? Does it have any utility?
If PHP can't find a function or constant by the name of hello, then yes, it treats it as a string. However, this is bad practice, and is even warned against:
>php -r 'error_reporting(E_ALL|E_STRICT);echo gettype(blah);'
PHP Notice: Use of undefined constant blah - assumed 'blah' in Command line code on line 1
So, the moral of the story is to always turn on error_reporting in PHP (just like you should always use strict and use warnings in Perl).
If you are not wrapping a piece of "string" inside quotes (be it double or single), the interpreter will try to make a guess and cast that into a String. I believe when you run that piece of PHP in error_reporting(E_ALL) you will see a notice
Notice: Use of undefined constant string - assumed 'string' in Command line code on line 1
A good practice is to always turn on error_reporting to E_ALL in development setups, but remember to turn it to less descriptive in production, you don't want people to have access to sensitive information (such as path) if your php errors out.

PHP string comparison with no quotes

From what I know about PHP, the following syntax is not legal:
if ($s == Yes)
It should instead be written as:
if ($s == 'Yes')
However, the first example is working just fine. Anyone know why?
Ordinarily, it would be interpreted as a constant, but if PHP can't find a constant by that name, then it assumes it to be a string literal despite the lack of quotes. This will generate an E_NOTICE message (which may not be visible, depending on your error reporting level); something like:
Notice: Use of undefined constant Yes - assumed 'Yes' in script.php on line 3
Basically, PHP is just overly lenient.
In short, PHP is acting as if the quotes were there.
If PHP doesn't recognize something as a reserved token, it treats it as a string literal.
The error log will show a warning about this.
The first one is not a string.
And it is not works fine:
error_reporting(E_ALL);
if ($s == Yes) {}
It's a legacy from the times when PHP were just a "Pretty home page" form interpreter and strongly discouraged nowadays.
You need to have both error_reporting showing notices, and display_errors set on.
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', '1');
if ($s == Yes) {
// foo
}
In PHP that Yes would be treated as a constant. If the constant is undefined it will assume you meant the string 'Yes'. It should generate a notification if you have them turned on.
PHP converts Yes to 'Yes' internally when constant Yes is found not to be defined.
Btw.. If what you want is comparing if $s has "Yes" as value an is a string then you have to
a) use strcmp or
b) use the identity operator "==="

PHP: getting a "use of undefined constant COOKIE_LOGIN" how do I fix this?

I haven't made any changes to the code affecting the COOKIE's and now I get the following:
Use of undefined constant COOKIE_LOGIN - assumed 'COOKIE_LOGIN'
//Destroy Cookie
if (isset($_COOKIE[COOKIE_LOGIN]) && !empty($_COOKIE[COOKIE_LOGIN]))
setcookie(COOKIE_LOGIN,$objUserSerialized,time() - 86400 );
I'm not sure what I need to do to actually change this since I do not know what chnaged to begin with and so cannot track the problem.
Thanks.
You need to surround the array key by quotes:
if (isset($_COOKIE['COOKIE_LOGIN']) && !empty($_COOKIE['COOKIE_LOGIN']))
setcookie('COOKIE_LOGIN',$objUserSerialized,time() - 86400 );
PHP converts unknown literals to strings and throws a warning. Your php.ini probably had the error reporting level to not display warnings but someone may have updated it or something. In either case, it is bad practice to take advantange of PHP's behavior in this case.
For more information, check out the php documentation:
Always use quotes around a string literal array index. For example, $foo['bar'] is correct, while $foo[bar] is not.
This is wrong, but it works. The reason is that this code has an undefined constant (bar) rather than a string ('bar' - notice the quotes). PHP may in future define constants which, unfortunately for such code, have the same name. It works because PHP automatically converts a bare string (an unquoted string which does not correspond to any known symbol) into a string which contains the bare string. For instance, if there is no defined constant named bar, then PHP will substitute in the string 'bar' and use that.
You can't say $_COOKIE[COOKIE_LOGIN] without error unless COOKIE_LOGIN is an actual constant that you have defined, e.g.:
define("COOKIE_LOGIN", 5);
Some people have habits where they will write code like:
$ary[example] = 5;
echo $ary[example];
Assuming that "example" (as a string) will be the array key. PHP has in the past excused this behavior, if you disable error reporting. It's wrong, though. You should be using $_COOKIE["COOKIE_LOGIN"] unless you have explicitly defined COOKIE_LOGIN as a constant.

Categories