rfc:arbitrary_string_interpolation
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:arbitrary_string_interpolation [2022/03/17 18:53] – ilutov | rfc:arbitrary_string_interpolation [2022/03/27 15:40] – ilutov | ||
---|---|---|---|
Line 3: | Line 3: | ||
* Date: 2022-03-17 | * Date: 2022-03-17 | ||
* Author: Ilija Tovilo, tovilo.ilija@gmail.com | * Author: Ilija Tovilo, tovilo.ilija@gmail.com | ||
- | * Status: | + | * Status: |
* Target Version: PHP 8.2 | * Target Version: PHP 8.2 | ||
- | * Implementation: | + | * Implementation: |
===== Proposal ===== | ===== Proposal ===== | ||
Line 11: | Line 11: | ||
PHP has four types of string interpolation. The two most common forms are ''" | PHP has four types of string interpolation. The two most common forms are ''" | ||
- | All the current forms of string interpolation are limited to variables or variable chains (array access, properties, method calls, etc). This probably covers | + | All the current forms of string interpolation are limited to variables or variable chains (array access, properties, method calls, etc). This probably covers |
* Function calls | * Function calls | ||
* Constants | * Constants | ||
* Class constants | * Class constants | ||
- | * Statis | + | * Static |
<code php> | <code php> | ||
Line 37: | Line 37: | ||
echo " | echo " | ||
</ | </ | ||
+ | |||
+ | The proposed syntax works in all cases where existing string interpolation is allowed, namely double quoted strings (''""'' | ||
===== Motivation ===== | ===== Motivation ===== | ||
Line 42: | Line 44: | ||
Of course, this can already be achieved in two other ways: | Of course, this can already be achieved in two other ways: | ||
- | | + | |
- | | + | |
Creating local variables is not a terrible option. It does pollute the variable symbol table a bit and it requires more visual jumping to see what value is being embedded. | Creating local variables is not a terrible option. It does pollute the variable symbol table a bit and it requires more visual jumping to see what value is being embedded. | ||
- | Option 2 is a fair bit worse. The quotes and '' | + | Concatenation |
<code php> | <code php> | ||
echo <<< | echo <<< | ||
Hello | Hello | ||
- | TXT /* <-- Must be on a new line */ . ' | + | TXT /* <-- Must be on a new line */ . strtoupper(' |
TXT; | TXT; | ||
</ | </ | ||
Line 58: | Line 60: | ||
===== Syntax choice ===== | ===== Syntax choice ===== | ||
- | The primary advantage of the syntax ''" | + | The primary advantage of the syntax ''" |
+ | |||
+ | A different syntax could be chosen with a given BC break. | ||
+ | |||
+ | <code php> | ||
+ | echo "Hello # | ||
+ | </ | ||
+ | |||
+ | This could be mitigated by adding a prefix to the string itself. | ||
+ | |||
+ | <code php> | ||
+ | echo $" | ||
+ | </ | ||
+ | |||
+ | I think this is unfortunate because: | ||
+ | |||
+ | * Converting an existing string to '' | ||
+ | * The prefix will need to work for all existing string types that allow interpolation | ||
===== Escaping ===== | ===== Escaping ===== | ||
Line 84: | Line 103: | ||
None | None | ||
+ | |||
+ | ===== Credits ===== | ||
+ | |||
+ | Credits to Rowan Tommins (IMSoP) for the syntax suggestion. | ||
===== Vote ===== | ===== Vote ===== |
rfc/arbitrary_string_interpolation.txt · Last modified: 2022/06/25 21:47 by ilutov