Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

75%
+4 −0
Q&A PHP variables aren't expanded when inside HTML which is by itself inside PHP

In PHP the single quotation mark ('x') is reading the content as raw values. This means, that escape sequences, such as \n, or variable interpolation, such as $x, is not supported. There are three...

posted 3y ago by luap42‭

Answer
#1: Initial revision by user avatar luap42‭ · 2021-03-28T11:15:32Z (over 3 years ago)
In PHP the single quotation mark (`'x'`) is reading the content as raw values. This means, that escape sequences, such as `\n`, or variable interpolation, such as `$x`, is not supported.

There are three possible ways to resolve that issue:

1. **String concatenation.** You can concatenate strings with the `.` operator:

    ```php
    $name = 'JohnDoea';
    'hello ' . $name == 'hello JohnDoea'
    ```

2. **Double-quote string.** Instead of using a single-quotation mark, you can use double-quoted strings (`"x"`). They support escape sequences and variable interpolation:


    ```php
    $name = 'JohnDoea';
    "hello $name" == 'hello JohnDoea'
    ```

    In this syntax, you need to keep in mind, though, to escape the double quotes in the HTML with a backslash (`\"`).

3. **Heredoc syntax.** PHP also has a string format called [*Heredoc*](
https://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc). This allows you to easily include long strings with escape sequences and variable interpolation:

    ```php
    $name = 'JohnDoea';
    $string = <<<LABEL
    hello $name
    LABEL;
    $string == 'hello JohnDoea'
    ```