Hướng dẫn can you use single and double quotes in php? - bạn có thể sử dụng dấu ngoặc đơn và dấu ngoặc kép trong php không?

Cải thiện bài viết

Show

Lưu bài viết

  • Đọc
  • Bàn luận
  • Cải thiện bài viết

    Lưu bài viết

    ĐọcSingle-quoted Strings: It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written. All the escape sequences like \r or \n, will be output as specified instead of having any special meaning. Single-quote is usually faster in some cases. The special case is that if you to display a literal single-quote, escape it with a backslash (\) and if you want to display a backslash, you can escape it with another backslash (\\)

    Bàn luậnSingle-quoted Strings: 

    Trích dẫn đơn hoặc kép trong lập trình PHP được sử dụng để xác định một chuỗi. Nhưng, có rất nhiều sự khác biệt giữa hai người này. Chuỗi được trích xuất đơn: Đó là cách dễ nhất để xác định một chuỗi. Bạn có thể sử dụng nó khi bạn muốn chuỗi được chính xác như nó được viết. Tất cả các chuỗi thoát như \ r hoặc \ n, sẽ được đầu ra theo quy định thay vì có bất kỳ ý nghĩa đặc biệt nào. Quan điểm đơn thường nhanh hơn trong một số trường hợp. Trường hợp đặc biệt là nếu bạn hiển thị một trình điều khiển đơn theo nghĩa đen, hãy thoát nó bằng dấu gạch chéo ngược (\) và nếu bạn muốn hiển thị dấu gạch chéo ngược, bạn có thể thoát nó bằng một dấu gạch chéo ngược khác (\\). & NBSP; 

    Dưới đây chương trình minh họa các chuỗi được trích dẫn đơn: & NBSP;

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    7

    Chương trình 1: & NBSP;

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    2
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    PHP

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    2
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    9
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    2
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    5
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    1
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    2
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    6
    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    7
    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    <ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>
    
    1
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    Output:

    I am a geek. 
    It'll be interesting to know about the string. 
    A \ is named as backslash. 
    This is a portal for $string. 
    This is a portal for \n geeks.

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    <ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>
    
    7
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0
    By using Double quotes the PHP code is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value. 

    <ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>
    
    9Double-quoted Strings: 

    Các chuỗi được trích dẫn kép: Bằng cách sử dụng trích dẫn kép, mã PHP buộc phải đánh giá toàn bộ chuỗi. Sự khác biệt chính giữa báo giá kép và trích dẫn đơn là bằng cách sử dụng dấu ngoặc kép, bạn có thể bao gồm các biến trực tiếp trong chuỗi. Nó diễn giải các chuỗi thoát. Mỗi biến sẽ được thay thế bằng giá trị của nó. & NBSP; 

    Dưới đây chương trình minh họa các chuỗi được trích dẫn đơn: & NBSP;

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    7

    Chương trình 1: & NBSP;

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    2
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    PHP

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    2
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    9
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    2
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    5
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    1
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    <ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>
    
    9

    Output:

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.

    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    6
    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    7
    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    <ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>
    
    1
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0
    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    <ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>
    
    7
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0
    <ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>
    
    9
    1. Các chuỗi được trích dẫn kép: Bằng cách sử dụng trích dẫn kép, mã PHP buộc phải đánh giá toàn bộ chuỗi. Sự khác biệt chính giữa báo giá kép và trích dẫn đơn là bằng cách sử dụng dấu ngoặc kép, bạn có thể bao gồm các biến trực tiếp trong chuỗi. Nó diễn giải các chuỗi thoát. Mỗi biến sẽ được thay thế bằng giá trị của nó. & NBSP;Dưới đây chương trình minh họa các chuỗi được trích dẫn kép: & nbsp;
    2.

    Chương trình 2: & NBSP;

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    print '<ul>';
    print '<li>Flour - 300 grams</li>';
    print '<li>Butter - 200 grams</li>';
    print '<li>Water - 100 ml</li>';
    print '</ul>';
    
    2
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    Chương trình 2: & NBSP;

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    print '<ul>';
    print '<li>Flour - 300 grams</li>';
    print '<li>Butter - 200 grams</li>';
    print '<li>Water - 100 ml</li>';
    print '</ul>';
    
    2
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0

    3.
    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    print '<ul>';
    print '<li>Flour - 300 grams</li>';
    print '<li>Butter - 200 grams</li>';
    print '<li>Water - 100 ml</li>';
    print '</ul>';
    
    8
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0
    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    <ul><li>Flour - 300 grams</li><li>Butter - 200 grams</li><li>Water - 100 ml</li></ul>
    
    4
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0
    4.
    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    6
    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    
    7
    echo 'Za\'atar is a Middle Eastern spice mix.';
    
    1
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0
    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    8
    echo 'Za\'atar is a Middle Eastern spice mix.';
    
    4
    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    
    0
    5.

    Hãy cho chúng tôi hiểu sự khác biệt trong một dạng bảng -:

    & nbsp;

    Hãy cho chúng tôi hiểu sự khác biệt trong một dạng bảng -:

    & nbsp;


    Làm thế nào tôi có thể thêm dấu ngoặc kép vào một biến trong PHP?

    Ví dụ, chuỗi thoát php, bao gồm một dòng mới trong một chuỗi, chuỗi \ n có thể được sử dụng. Để bao gồm các dấu ngoặc kép, chuỗi \ "có thể được sử dụng. Tương tự, để bao gồm các trích dẫn đơn, trình tự \ 'có thể được sử dụng. Xin chào, thế giới!

    HTML có quan tâm đến việc bạn sử dụng báo giá đơn hay dấu ngoặc kép không?

    Cộng đồng HTML từ lâu đã thông qua bằng cách sử dụng các trích dẫn kép trong các thuộc tính HTML. Chắc chắn chúng ta có thể sử dụng độc thân, nhưng tại sao lại chiến đấu với cộng đồng? Cộng đồng JavaScript từ lâu đã được thông qua bằng cách sử dụng các trích dẫn đơn trong mã, cho phép chèn các trích dẫn kép về các thuộc tính của các đoạn trích HTML được thêm vào.

    Đây là một bài viết tôi đã viết một thời gian trước trên blog cũ của tôi khi tôi mới bắt đầu học PHP. Tôi sẽ đăng lại nó ở đây, trong trường hợp nó giúp bất cứ ai :)

    // Using single quotes to save a string in a variable:
    $recipe_title = 'Meatball Spaghetti';
    
    // Using single quotes to write something on the screen:
    echo '<h2>Meatball Spaghetti</h2>';
    
    // The line above will get output as-is in your code:
    <h2>Meatball Spaghetti</h2>
    

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Nếu bạn muốn lưu văn bản dưới dạng biến hoặc hiển thị nó trên màn hình (với nói, I am a geek. It'll be interesting to know about the string. This is a simple string. The word is ABC.8 hoặc echo 'Za\'atar is a Middle Eastern spice mix.'; 8), bạn phải bao quanh văn bản trong các trích dẫn. Văn bản xung quanh bằng trích dẫn làm cho văn bản đó trở thành một chuỗi.

    Bạn có thể sử dụng các trích dẫn đơn (

    echo 'Za\'atar is a Middle Eastern spice mix.';
    
    9) hoặc trích dẫn kép (____ 80), nhưng có một số khác biệt quan trọng, mà tôi sẽ xem trong bài đăng này.

    print '<ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>';
    

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Dấu nháy đơn

    <ul>
      <li>Flour - 300 grams</li>
      <li>Butter - 200 grams</li>
      <li>Water - 100 ml</li>
    </ul>
    

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Trích dẫn đơn là cách đơn giản nhất để tạo một chuỗi. Họ chỉ hiển thị những gì chúng được đưa ra, không có chuông và còi, không có "quyền hạn" đặc biệt như có thể hiển thị các giá trị biến (xem bên dưới trong phần trích dẫn kép).

    print '<ul>';
    print '<li>Flour - 300 grams</li>';
    print '<li>Butter - 200 grams</li>';
    print '<li>Water - 100 ml</li>';
    print '</ul>';
    

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Dấu nháy đơn

    <ul><li>Flour - 300 grams</li><li>Butter - 200 grams</li><li>Water - 100 ml</li></ul>
    

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Trích dẫn đơn là cách đơn giản nhất để tạo một chuỗi. Họ chỉ hiển thị những gì chúng được đưa ra, không có chuông và còi, không có "quyền hạn" đặc biệt như có thể hiển thị các giá trị biến (xem bên dưới trong phần trích dẫn kép).

    Phá vỡ dòng với các trích dẫn đơn lẻ

    Nếu bạn cần hiển thị văn bản trên nhiều dòng, bạn có thể sử dụng ngắt dòng trong các trích dẫn để đạt được điều này. Ví dụ:

    Điều này sẽ được đầu ra như:

    echo 'Za\'atar is a Middle Eastern spice mix.';
    

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Điều này sẽ xuất hiện đúng như sau mà không gây ra lỗi.

    echo 'A \\ is called a "backslash."';
    
    5

    Hai: ________ 86 & nbsp; để thoát khỏi sự chao đảo, nhân vật thoát khỏi chuỗi

    Bạn đã có thể sử dụng

    echo 'A \\ is called a "backslash."';
    

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Để in

    echo 'A \\ is called a "backslash."';
    
    7

    *Nó có thể phụ thuộc vào trình biên dịch, nhưng khi tôi đang thử nghiệm điều này, & nbsp; & nbsp; ________ 88 & nbsp; dường như cũng tự mình hoạt động. Nhưng, nếu vì một lý do nào đó bạn muốn hiển thị & nbsp; ________ 86, bạn có thể cần sử dụng một cái gì đó như ________ 90 & nbsp; hoặc & nbsp; ________ 91. Vì nó là một nhân vật đặc biệt, có lẽ tốt nhất là thoát khỏi trường hợp.

    Dấu ngoặc kép

    Một sự khác biệt lớn về dấu ngoặc kép so với trích dẫn đơn là bạn có thể sử dụng dấu ngoặc kép để bao gồm các biến trực tiếp bên trong chuỗi. Nếu bạn sử dụng các trích dẫn đơn, bạn sẽ phải kết hợp các mảnh lại với nhau. Hãy xem một ví dụ.

    Giả sử bạn có công thức nấu ăn và bạn lưu các tiêu đề vào một biến gọi là

    $recipe_title = 'Meatball Spaghetti';
    
    2:

    $recipe_title = 'Meatball Spaghetti';
    

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Nếu bạn muốn tạo HTML cho các tiêu đề công thức để chúng trông như thế này (và bạn không nhúng PHP trực tiếp vào các tệp HTML, trong đó bạn có thể sử dụng thẻ ____93 thay vào đó để tạo ra các biến):

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    0

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Sử dụng các trích dẫn duy nhất bạn cần để thêm các phần khác nhau với nhau:

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    1

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Tuy nhiên, với dấu ngoặc kép, bạn có thể đặt biến trực tiếp bên trong các trích dẫn:

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    2

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Cả hai phương pháp đều hoạt động tốt, nhưng sử dụng dấu ngoặc kép có thể giúp bạn tiết kiệm một số rắc rối.

    Mẹo chuyên nghiệp:

    Sử dụng niềng răng xoăn để chỉ định rõ ràng kết thúc của một tên biến khi phân tích cú pháp nó vào một chuỗi được trích dẫn kép.

    Cố gắng in & nbsp; 2 cốc trên dòng 2 bên dưới sẽ cho bạn một lỗi vì mã cho rằng tên biến là & nbsp; $ unter_cups & nbsp; thay vì

    $recipe_title = 'Meatball Spaghetti';
    
    4:

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    3

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Để tránh các lỗi như thế này, bạn có thể bao quanh tên biến trong các dấu ngoặc xoăn thích như vậy:

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    4

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Bạn cũng có thể thực hiện một số hoạt động phức tạp hơn ngay trong các trích dẫn kép, nhưng điều đó nằm ngoài phạm vi của bài viết này. Để tìm hiểu thêm về các hoạt động phân tích phức tạp trong các báo giá kép, hãy xem các ví dụ trong hướng dẫn sử dụng PHP.

    -

    Nhân tiện, giống như các trích dẫn đơn, bạn có thể thêm các lần ngắt dòng vào đầu ra của mình bằng cách bao gồm các lần ngắt dòng trong chuỗi. Ví dụ,

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    5

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    displays:

    I am a geek.
    It'll be interesting to know about the string.
    This is a simple string.
    The word is ABC.
    6

    Nhập chế độ FullScreenen EXIT Mode FullScreen

    Nhân vật đặc biệt và ký tự thoát có trích dẫn đôi

    Double Trích dẫn cung cấp cho bạn nhiều nhân vật đặc biệt hơn để làm việc với các trích dẫn đơn lẻ, bao gồm cả ký tự phá vỡ dòng.

    • $recipe_title = 'Meatball Spaghetti';
      
      5 cho một dòng mới
    • $recipe_title = 'Meatball Spaghetti';
      
      6 cho một tab
    • $recipe_title = 'Meatball Spaghetti';
      
      7 để trở về vận chuyển
    • $recipe_title = 'Meatball Spaghetti';
      
      8 cho một dấu hiệu đô la (nếu không nó có thể bị nhầm là biến)
    • $recipe_title = 'Meatball Spaghetti';
      
      9 cho một báo giá kép
    • Xem thêm trong hướng dẫn sử dụng PHP

    Tôi nên sử dụng những gì?

    Nói chung, bạn sử dụng hoặc, nhưng bạn nên phù hợp với loại bạn sử dụng và khi nào. Ví dụ: bạn có thể chọn sử dụng các báo giá đơn theo mặc định trừ khi bạn cần sử dụng các biến hoặc ký tự đặc biệt trong chuỗi.

    Bạn có thể nghĩ rằng vì các trích dẫn đôi cung cấp cho bạn nhiều tính năng hơn, nên tốt hơn nếu sử dụng chúng mọi lúc, nhưng các trích dẫn đơn lẻ có lẽ tốt hơn cho các chuỗi đơn giản vì bạn không cần phải thoát khỏi các ký tự đặc biệt như dấu hiệu đô la.

    Chúng ta có thể sử dụng trích dẫn đơn hoặc đôi trong PHP không?

    Trích dẫn đơn hoặc kép trong lập trình PHP được sử dụng để xác định một chuỗi. Nhưng, có rất nhiều sự khác biệt giữa hai người này. Chuỗi được trích xuất đơn: Đó là cách dễ nhất để xác định một chuỗi. Bạn có thể sử dụng nó khi bạn muốn chuỗi được chính xác như nó được viết.. But, there are lots of differences between these two. Single-quoted Strings: It is the easiest way to define a string. You can use it when you want the string to be exactly as it is written.

    Bạn có thể sử dụng các trích dẫn đơn trong PHP không?

    Trích dẫn đơn ¶ Cách đơn giản nhất để chỉ định một chuỗi là đặt nó trong các trích dẫn đơn (ký tự ').Để chỉ định một trích dẫn đơn theo nghĩa đen, hãy thoát nó bằng một dấu gạch chéo ngược (\).Để chỉ định một dấu gạch chéo ngược theo nghĩa đen, nhân đôi nó (\\).The simplest way to specify a string is to enclose it in single quotes (the character ' ). To specify a literal single quote, escape it with a backslash ( \ ). To specify a literal backslash, double it ( \\ ).

    Làm thế nào tôi có thể thêm dấu ngoặc kép vào một biến trong PHP?

    Ví dụ, chuỗi thoát php, bao gồm một dòng mới trong một chuỗi, chuỗi \ n có thể được sử dụng.Để bao gồm các dấu ngoặc kép, chuỗi \ "có thể được sử dụng. Tương tự, để bao gồm các trích dẫn đơn, trình tự \ 'có thể được sử dụng. Xin chào, thế giới!

    HTML có quan tâm đến việc bạn sử dụng báo giá đơn hay dấu ngoặc kép không?

    Cộng đồng HTML từ lâu đã thông qua bằng cách sử dụng các trích dẫn kép trong các thuộc tính HTML.Chắc chắn chúng ta có thể sử dụng độc thân, nhưng tại sao lại chiến đấu với cộng đồng?Cộng đồng JavaScript từ lâu đã được thông qua bằng cách sử dụng các trích dẫn đơn trong mã, cho phép chèn các trích dẫn kép về các thuộc tính của các đoạn trích HTML được thêm vào.Sure we can use single, but why fight the community? The JavaScript community has long ago adopted using single quotes in code, allowing for the insertion of double quotes on attributes of appended HTML snippets.