0

I'm learning PHP and following an exercise — I'm writing some data to a file. When I use single quotes, it does not use the line break or variables:

function add_registered_user($name, $email){
    file_put_contents('mailing_list.php', '\n$name: $email');
};

But when when I use double quotes (like the tutorial author), it works perfectly:

function add_registered_user($name, $email){
    file_put_contents('mailing_list.php', "\n$name: $email");
};

How come? And are there many other quirks in PHP that are reliant on one over another?

1 Answers1

0

Per the PHP Docs for Strings:

When a string is specified in double quotes or with heredoc, variables are parsed within it.

There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort.

The complex syntax can be recognised by the curly braces surrounding the expression.

Community
  • 1
  • 1
War10ck
  • 12,387
  • 7
  • 41
  • 54