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

Posted on 2013-08-19
Last Modified: 2013-09-11
Just curious, when defining a string/ array/ etc, are there any common situations where someone does not need to quote the value?
Question by:Andy_Fang
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
LVL 17

Expert Comment

ID: 39422015
$new_var = str_replace($var, $var2, $new_var);

You mean """ quote correct?


Author Comment

ID: 39422020
Yeah, I mean single quotes and double quotes. Could someone give a list of common PHP codes that do not require quotes?
LVL 57

Assisted Solution

by:Julian Hansen
Julian Hansen earned 250 total points
ID: 39422706
You don't need quotes for numeric values for instance

$value = 9;

However this is analogous to

$value = '9';

In other words if you do the following
echo $value * 10;

Open in new window

You will get 90 output.

Maybe give us more info on why you kneed to know this and we can answer you better.
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

LVL 83

Accepted Solution

Dave Baldwin earned 250 total points
ID: 39422752
Strings always require that quotes be used properly.  Here are all the rules about strings: 

Numbers used as numbers are not quoted.  Numbers used as text must be quoted.

Constants should not be quoted but they must be defined before they are used.  Read here  and especially read the notes.  Constants inside strings are not recognized as constants.
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39422798
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.
LVL 110

Expert Comment

by:Ray Paseur
ID: 39423096
This article has some good "getting started" guidance.

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:

Here is how to do something wrong, that looks OK but will get you fired from any professional programming job.
$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.
LVL 17

Expert Comment

ID: 39423129
~Well put ~Ray.

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

LVL 110

Expert Comment

by:Ray Paseur
ID: 39423224
@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.



Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question