-3

I noticed this really weird phenomenon occurring with creating a variable in php. I was able to create a String variable, without using double quotes. This only works for single strings though. Excuse me if this question is stupid or something, I am a beginner to php.

<?php
$firstname = notreserved;
$lastname = Interstellar;
Dude156
  • 158
  • 1
  • 12
  • If you enabled error reporting/display, what do you get as the result? – Nigel Ren Jul 18 '18 at 14:32
  • there was no error; in fact my code ran perfectly – Dude156 Jul 18 '18 at 14:33
  • Can you print the variables? – Mobin F.R.G Jul 18 '18 at 14:36
  • yah. Using vardump() gives me this: string(4) "Case" – Dude156 Jul 18 '18 at 14:37
  • PHP is weird. Syntactically it treats this word as a constant, and will automatically assign it a value (that word, as a string) if you use it without assigning it first. This behavior is quite silly. Furthermore PHP does give a notification level error on this, but they are often suppressed. I guess all of this by the others as well, but I just wanted to add that it's not your fault, it's just one of the many quirks of PHP. You, as anybody else, would expect a more serious error message when you make a mistake like this, instead of auto-recovering magic that hardly ever does what you want. – GolezTrol Jul 18 '18 at 14:45
  • There's no version of PHP that will return you the string "Case", since it's a reserved word. For a valid token, also note that PHP 7.2+ will raise a warning instead of a notice, and this is likely to increase to an error in future versions. – iainn Jul 18 '18 at 14:46
  • @GolezTrol thanks man! Coming from Java, I've never seen something like that go unmarked. What do you mean by a constant though? – Dude156 Jul 18 '18 at 14:51
  • Basically a variable that is assigned once and can't change: [Constants in PHP](http://php.net/manual/en/language.constants.php) and [How to do them in Java](https://stackoverflow.com/questions/66066/what-is-the-best-way-to-implement-constants-in-java). – GolezTrol Jul 18 '18 at 15:14

4 Answers4

2

See the documentation for Constants Syntax:

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. See also the manual entry on why $foo[bar] is wrong (unless you first define() bar as a constant). This does not apply to (fully) qualified constants, which will raise a fatal error if undefined. If you simply want to check if a constant is set, use the defined() function.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

There is nothing weird here. It is a common pitfall that is even explained in the documentation.

The line:

$firstname = Case;

creates the PHP variable $firstname and tries to assign to it the value of the Case constant.

If the Case constant has already been created (by a call to define() or by a const statement) then the above code copies its value into the $firstname variable and that's all.

But if a Case constant has not already been declared then the code above triggers a notice (E_NOTICE) that you probably don't see because, by default, the PHP is configured to ignore the notices.

The PHP interpreter is tolerant and assumes you actually wanted to write 'Case' and forgot the quotes. It then initializes the variable $firstname with the string 'Case' and this can help you identify the source of your error (the Case constant has not been defined) even if you ignore the notices.

Assuming you want to initialize the variables with strings then the correct syntax for your code is:

$firstname = 'Case';
$lastname = 'Interstellar';

Read more about PHP strings.

axiac
  • 68,258
  • 9
  • 99
  • 134
0

You should get a NOTICE.

What you do here is assign an undefined constant. This will implicitely define a constant with the name as its value.

king_nak
  • 11,313
  • 33
  • 58
0

it's because you have all warnings suppressed, check this 3v4l.org result - every php version complains about it and doesn't let it print anything because it's not correct syntax

bestestefan
  • 852
  • 6
  • 20