Should I use "" (quotations) While defining variable? But With/Without Works
I have just started learning PHP, and here's my first doubt...
Both开发者_C百科 of these work:
<?php
$function1 = "Aahan";
print "Hi, $function1";
?>
and
<?php
$function2 = Aahan;
print "Hi, $function2";
?>
See the difference? In the second example I didnt use the "" (quotation marks) for the variable string. But it still works.
Also, the stupid author of the book (which I wont name), uses "" in some examples and doesnt use them in some, without explanation. So, what should I think? Either way is okay or what do you advise?
EDIT: Sorry guys, the author is a good one. I misunderstood this string $x = 3;
, and started checking out the above examples. Just realized that constants don't need quotes. Sorry again.
EDIT-2: it doesn't show me any error like you all have mentioned. How do I make it show the errors? or is it something wrong with my server itself? (I dont think so)
Use the first!!
In PHP, strings without quotes constitute a constant. If no existing constant is found, the value produced is the name itself, which is why the second "works".
The second produces an unsightly E_NOTICE as well:
Notice: Use of undefined constant Aahan - assumed 'Aahan' in /test.php on line 2
The second one actually should be issuing a warning, "UNDEFINED CONSTANT Aahan ASSUMING 'Aahan'". Always quote to avoid this.
Also, make sure your error reporting is set to max while developing.
Without quotes is incorrect, and if you have notices enabled:
Notice: Use of undefined constant Aahan - assumed 'Aahan' in test.php on line xx
What its telling you is that while the code isn't valid, PHP is clever enough to make an assumption to allow your code to execute and not terminate. You should avoid this sort of thing though as PHP's assumption of what the value of 'Aahan' is may not be correct. In this case, its assuming it is a constant, and its value is 'Aahan'.
To do that correctly would be this:
define("Aahan","Aahan");
echo Aahan;
Results in
Aahan
And no errors - although its common practice for a constant to be defined with its name in block capitals. Its not required, but it makes things easier to read.
If you're defining a string in a variable, use quotes. If you're defining a number, don't.
Valid code:
$myVariable = "Hello World"; //this is a string
$myVariable = 3.42; //this would set $myVariable to a numeric value.
$myVariable = HELLO_WORLD; //this would set $myVariable to the value of constant HELLO_WORLD
In the third case, if you hadn't defined HELLO_WORLD - you'd get a notice and it would assume the value to be "HELLO_WORLD" - same as if you'd typed:
$myVariable = "HELLO_WORLD";
Which would be error free. Of course there are many reasons why you might want to define a constant.
Aahan,
$function1 = "Aahan";
Is the right method for php. Its a string of chareters. Refer to php strings.
Now when you do
$function2 = Aahan;
Php searches for a constant Aahan and when its not defined it treats it as a string. To explain further. Consider these:
$function1 = "Aahan Is My Name";
And
$function2 = Aahan Is My Name;
While the first one with " "(quotes) is right. The 2nd one gives you a syntax error.
This is the warning gernerated, unless you have turned it off
Notice: Use of undefined constant Aahan - assumed 'Aahan'
EDIT: To turn on comprehensive Error reporting Add the following code to the first line of your code.
error_reporting(E_ALL);
If constant Aahan is undefined it is treated as "Aahah" and send warning to you.
You should use ""
Use quotes when you want a string:
$string = "this is a string";
$numeric_string = "1"; //actually a string, but will act like an integer
$integer = 1; //an integer
$value_of_a_constant = foo; // If no constant named "foo" exists,
// PHP assumes you meant a string containing "foo",
// but emits a notice, see below
I recommend that you turn your error_reporting on (set to E_ALL, or E_STRICT), and turn display_errors on. Do this at the very top of your script:
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
If you had such a setup, you'd notice when you do $var = Aahan
, PHP would output an error message (well, technically, a "notice"), telling you that you used an undefined constant, and it was treating it like a string.
Having E_ALL on while learning will help you learn better, faster.
精彩评论