Link to home
Start Free TrialLog in
Avatar of Andy_Fang
Andy_Fang

asked on

Dumb Question: Are There Any Instances Where I Do Not Need Quotes in PHP?

Just curious, when defining a string/ array/ etc, are there any common situations where someone does not need to quote the value?
Avatar of James Williams
James Williams
Flag of United States of America image

$new_var = str_replace($var, $var2, $new_var);

You mean """ quote correct?

Selvol
Avatar of Andy_Fang
Andy_Fang

ASKER

Yeah, I mean single quotes and double quotes. Could someone give a list of common PHP codes that do not require quotes?
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To extend @julianH's example, if you have as text like...

$value = '009';

And you do...

echo $value * 1;

You will get 9 because there will be an implicit conversion because you did an arithmetic operation and there were only numbers in $value.
This article has some good "getting started" guidance.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

PHP is a loosely typed language.  The engine attempts to cover up the complexity of the concept of a variable that can be any type by providing a uniform and intuitive set of rules that allow type conversion.  What that means is that PHP will make its own decisions about data types, based on the context in which the data is used.  You have some, but not complete control over this.  Required reading, with plenty of examples, here:
http://php.net/manual/en/language.variables.basics.php
http://php.net/manual/en/language.types.type-juggling.php
http://php.net/manual/en/language.types.string.php#language.types.string.conversion
http://php.net/manual/en/types.comparisons.php

Here is how to do something wrong, that looks OK but will get you fired from any professional programming job.
<?php
$x = array( 'a' => 1, 'b' => 2);
$y = $x[a];

Open in new window

The reason this is wrong, but permitted, goes to the ancient history of PHP, when it was called "Personal Home Page."  The authors wanted it to be a very easy language to use, and they thought that rigorous structure was the source of difficulty in learning programming languages.  So they included all kinds of fall-back definitions, creating a cascade of meanings.  After running that little script, what is the value of $y?  The answer is, "You cannot know!"  This happens because PHP will first try to use a as a constant.  If a is found, the constant value will be used as the index into the $x array.  If a is not found among the constants, PHP will raise a Notice and will pretend that you had put single quotes around 'a' and will retry the variable assignment.  But here's the gotcha: In the standard installation of PHP, Notice-level messages are suppressed, so you will never be told what PHP is doing to your array index!  The ambiguity will lie, latent, in your code for a long time.  Maybe, seemingly, forever.  Then one day, another programmer will need to work on the project, maybe in a different function or class, and she will define() a.  At this point it will hit the fan, because like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope.  And without scope encapsulation, the constant is now injected into your programming.

Now, suddenly and without notice, a and 'a' have different meanings, and your script begins to fail.  So read the man pages, and make sure you understand them.  And post new questions if you're not 100% sure what PHP is doing to your data!

Best regards, ~Ray

PS: Going forward, you might consider leaving your questions open for 24 hours.  You will get better answers that way.
~Well put ~Ray.

You spend too much time on EE my friend.....

Selvol
@Selvol: I teach PHP for Boston University's extension here in Washington, DC.  When I'm not in the classroom I usually spend the early morning and late afternoon with EE.  Other than that, I play frisbee with my dog, or go sailing, or go out for a run around the monuments.

;-)

~Ray