PHP Strings Don’t Need Quotes

I bet you didn’t know that PHP strings don’t need quotes! Indeed PHP developers work with strings with either single or double quotes, but actually in some cases you don’t need them.

PHP by Book

Here’s how PHP developer declare a string, which is something very common in any programming language.

$my_var = 'hello world';
// or
$my_var = "hello world";

PHP Tricks

What if you do the following:

echo hello;

That appears to be correct … Well, it’s not absolutely correct. You’ll be “noticed”.

// Notice: Use of undefined constant hello
echo hello;

However if you disable error reporting, the code will be completely fine.

error_reporting(0);
 
// no problem now
echo hello;

Variations

What follows from the thing above is that you can use strings without quotes:

// hello
echo hello;
 
// hello world (concatenated)
echo hello . ' world';
 
// helloworld
echo hello . world;

However you can’t have spaces and most of the “special” symbols.

// syntax error
echo hello world;
 
// syntax error
echo hello!;

Final Words

Although you can do this in PHP, that is completely wrong. The code becomes more difficult to read and understand. In the second place you can miss a $ sign in front of a variable declaration and thus the PHP interpreter will assume this is a string. So disable error reporting isn’t so great sometimes.

15 thoughts on “PHP Strings Don’t Need Quotes

  1. The real problem will be when somebody will declare constant which was “cast” as a string..

  2. The last part where it says this is absolutely wrong in PHP needs to be bold – and at the top and every other line! Articles like this are not very serving to the PHP community. Most people, especially beginners, will scan the code sections for examples and not read the small print. I understand the point you were trying to make – but things like turning off notices in PHP to solve ‘a problem’ is just really bad form.

  3. That’s the worst tip anyone can give to any programmer – it encourages bad programming practices. Also, what if your string is the following:

    “The quick brown fox jumped…”

    Will it work without the quotes?

    @EllisGL, I completely agree…

  4. Surely you’re joking? That’s completely retarded. Regardless of whether you disable error reporting, the error handler will still be triggered every single time you do this. And every time you do this, PHP will turn your string into a constant, and memory for constants is never freed or garbage collected.

    Did I mention this was retarded?

    If you don’t like quotes, you can just use the heredoc or nowdoc syntax:

    // Heredoc
    $string = <<<STOP_HERE
    bla text goes here ble look mom, no quotes!
    and I can even use $variables
    STOP_HERE;

    // Nowdoc
    $string = <<<'QUOTED_STRING'
    Equivalent of single quotes, can't use $variables now
    QUOTED_STRING

  5. I echo what everyone else says about avoiding this like the plague. It only accentuates some of PHP’s eccentricities that professionals try to avoid with coding standards.

  6. Except that if I do:

    define("hello", "goodbye");

    your

    echo hello;

    Won’t do what you think it will. Don’t abuse language features like that…

  7. *sigh*

    This is why we can’t have nice things.

    Don’t write an article like this explaining like this is something that you can do, and then provide a little tiny disclaimer saying you shouldn’t.

    EVERYONE SHOULD IGNORE THIS ARTICLE COMPLETELY AND NEVER, UNDER ANY CIRCUMSTANCES, USE UNQUOTED STRING

  8. This article is pure flame bait. More articles like this will speed up the slow death of PHP. I love it!

  9. when use ” ” php engine process all text in “” and need time ,
    when use ‘ ‘ php engine just print[echo] and no need process time for variable

  10. This is wrong on so many levels that one would need a special algorithm function to calculate the amount of possible damage from this Idea …

  11. Arrrgh!!!
    I’ve been in the IT industry for over 20 years. I’m just starting to learn PHP and no other language I know of let’s you do this, nor should any language allow this. NO! NO! NO!

    Most languages let you shot yourself in the foot in one way or another. With PHP I’m learning that you can easily blow your entire torso off.

    I completely agree with Aaron Saray that DO NOT DO THIS should be at the top and bolded!

Leave a Reply

Your email address will not be published. Required fields are marked *