Hướng dẫn php heredoc vs nowdoc - php heredoc so với nowdoc

Với sự xuất hiện của Php 7.3 vào tháng trước, một số thay đổi thú vị đối với các cú pháp HEREDOC và NowDoc hiện tại. Tuy nhiên, không phải tất cả mọi người tôi đã nói chuyện thậm chí biết rằng cú pháp này tồn tại trong PHP, vì vậy bây giờ có vẻ như là một cơ hội tốt để xem xét chúng là gì và cách sử dụng chúng.PHP 7.3 last month came some interesting changes to the existing heredoc and nowdoc syntaxes. However, not everyone I spoke to even knew that this syntax existed in PHP, so now seems like a good opportunity to take a look at what they are and how to use them.PHP 7.3 last month came some interesting changes to the existing heredoc and nowdoc syntaxes. However, not everyone I spoke to even knew that this syntax existed in PHP, so now seems like a good opportunity to take a look at what they are and how to use them.PHP 7.3 last month came some interesting changes to the existing heredoc and nowdoc syntaxes. However, not everyone I spoke to even knew that this syntax existed in PHP, so now seems like a good opportunity to take a look at what they are and how to use them.

Nội dung chính

  • Quy tắc định danh đóng mới
  • Đóng các từ
  • Sự khác biệt giữa Heredoc và NowDoc trong PHP là gì?
  • Mục đích của việc sử dụng Heredoc và NowDoc trong PHP là gì?
  • Heredoc PHP là gì?
  • PHP bây giờ là gì?

Nội dung chính

  • Quy tắc định danh đóng mới
  • Đóng các từ
  • Sự khác biệt giữa Heredoc và NowDoc trong PHP là gì?
  • Mục đích của việc sử dụng Heredoc và NowDoc trong PHP là gì?
  • Heredoc PHP là gì?
  • PHP bây giờ là gì?

Dây

Trong PHP, chúng tôi có một số tùy chọn để chỉ định giá trị chuỗi. Hai cách tiếp cận phổ biến nhất là sử dụng trích dẫn đơn hoặc đôi. Chúng tôi cũng có cú pháp Heredoc và NowDoc hữu ích khi chúng tôi muốn xác định một chuỗi đa dòng.PHP we have several options for specifying a string value. The two most common approaches are to use either single or double quotes. We also have heredoc and nowdoc syntaxes which are useful when we want to define a multiline string.PHP we have several options for specifying a string value. The two most common approaches are to use either single or double quotes. We also have heredoc and nowdoc syntaxes which are useful when we want to define a multiline string.PHP we have several options for specifying a string value. The two most common approaches are to use either single or double quotes. We also have heredoc and nowdoc syntaxes which are useful when we want to define a multiline string.

Trước khi chúng ta nhìn vào Heredoc và NowDoc, hãy để làm mới các ký ức của chúng ta trên các chuỗi trích dẫn đơn và đôi. Có một vài khác biệt giữa hai phương pháp này.

Khi chúng tôi sử dụng chuỗi trích dẫn kép, chúng tôi có thể sử dụng các ký tự đã thoát như

$foo = 'bar';
echo 'Hello $foo\nGoodbye!';
// Output:- 
// Hello $foo\nGoodbye!
3 được hiểu là phá vỡ dòng mới và các biến sẽ được thay thế cho các giá trị tương ứng của chúng. Ví dụ:-
$foo = 'bar';
echo "Hello $foo\nGoodbye!";
// Output:-
// Hello bar
// Goodbye!

Chuỗi được trích dẫn đơn là theo nghĩa đen.

  • Điều này có nghĩa là các ký tự thoát ra không mở rộng, ví dụ:
    $foo = 'bar';
    echo 'Hello $foo\nGoodbye!';
    // Output:- 
    // Hello $foo\nGoodbye!
    4 sẽ không dẫn đến một dòng mới, chuỗi theo nghĩa đen sẽ là một dấu gạch chéo ngược sau đó là ký tự n
  • Các biến không được thay thế bằng các giá trị tương ứng của chúng

Ví dụ.

$foo = 'bar';
echo 'Hello $foo\nGoodbye!';
// Output:- 
// Hello $foo\nGoodbye!

Một ngoại lệ là bạn có thể sử dụng

$foo = 'bar';
echo 'Hello $foo\nGoodbye!';
// Output:- 
// Hello $foo\nGoodbye!
0 trong một chuỗi được trích dẫn nếu bạn cần bao gồm một báo giá duy nhất trong giá trị chuỗi.

Vậy còn cú pháp Heredoc và NowDoc thì sao? Họ cung cấp một cách khác để xác định chuỗi trong PHP. Chúng đặc biệt hữu ích khi chúng ta cần xác định một chuỗi trên nhiều dòng.PHP. They are particularly useful when we need to define a string over multiple lines.PHP. They are particularly useful when we need to define a string over multiple lines.PHP. They are particularly useful when we need to define a string over multiple lines.

Chúng hoạt động bằng cách xác định một định danh sẽ đánh dấu sự khởi đầu và kết thúc của chuỗi. Mã định danh có thể là bất kỳ giá trị chữ và số nào theo cùng một quy tắc mà chúng tôi sử dụng cho các tên biến. Một điều quan trọng cần lưu ý với định danh là kể từ Php 7.3, bạn cần đảm bảo rằng nó không xuất hiện trong chính chuỗi.PHP 7.3 you need to make sure that it does not appear within the string itself.PHP 7.3 you need to make sure that it does not appear within the string itself.PHP 7.3 you need to make sure that it does not appear within the string itself.

Ở đây, một ví dụ về một Heredoc.

echo <<<EOT
Hello World.
Goodbye!
EOT;
// Output:-
// Hello World.
// Goodbye!

Định danh cho heredoc này là

$foo = 'bar';
echo 'Hello $foo\nGoodbye!';
// Output:- 
// Hello $foo\nGoodbye!
1. Hãy nhớ rằng chúng tôi có thể đã sử dụng bất kỳ định danh chữ và số nào để đánh dấu phần đầu và cuối của chuỗi, ví dụ:
$foo = 'bar';
echo 'Hello $foo\nGoodbye!';
// Output:- 
// Hello $foo\nGoodbye!
2. Định danh mở phải luôn được tiến hành bởi toán tử
$foo = 'bar';
echo 'Hello $foo\nGoodbye!';
// Output:- 
// Hello $foo\nGoodbye!
3.

Heredoc từ tương đương với một chuỗi được trích dẫn kép. Điều đó có nghĩa là bất kỳ biến nào trong chuỗi sẽ được thay thế cho các giá trị tương ứng của chúng. Chúng ta có thể viết lại ví dụ chuỗi được trích dẫn kép ở trên dưới dạng Heredoc:-

$foo = 'bar';
echo <<<EOT
Hello $foo
Goodbye!
EOT;
// Output:-
// Hello bar
// Goodbye!

NowDoc, tương đương với một chuỗi được trích dẫn duy nhất. Để biểu thị một nowDoc, chúng ta chỉ cần sử dụng các trích dẫn duy nhất xung quanh định danh mở:-

$foo = 'bar';
echo "Hello $foo\nGoodbye!";
// Output:-
// Hello bar
// Goodbye!
0

Vị trí của định danh đóng là rất quan trọng. Trước Php 7.3, nó phải luôn được đặt trên một dòng mới, sau đó là một đại tá và không có không gian trắng phía trước nó. Vì vậy, tất cả các ví dụ trên sẽ là hợp lệ. Tuy nhiên, kể từ Php 7.3, các quy tắc này đã được nới lỏng một chút.PHP 7.3 it had to always be placed on a new line followed by a semi-colon and with no white-space in front of it. So all the examples above would be valid. However, as of PHP 7.3 these rules have been relaxed a little.PHP 7.3 it had to always be placed on a new line followed by a semi-colon and with no white-space in front of it. So all the examples above would be valid. However, as of PHP 7.3 these rules have been relaxed a little.PHP 7.3 it had to always be placed on a new line followed by a semi-colon and with no white-space in front of it. So all the examples above would be valid. However, as of PHP 7.3 these rules have been relaxed a little.

Quy tắc định danh đóng mới

Kể từ Php 7.3, sau đây là hợp lệ.PHP 7.3 the following is valid.PHP 7.3 the following is valid.PHP 7.3 the following is valid.

Nếu chúng ta sử dụng khoảng trắng ở phía trước định danh đóng thì cùng một lượng khoảng trắng sẽ được khấu trừ từ đầu mỗi dòng của chuỗi.

$foo = 'bar';
echo "Hello $foo\nGoodbye!";
// Output:-
// Hello bar
// Goodbye!
1

Trong ví dụ này, có bốn không gian trước khi định danh đóng

$foo = 'bar';
echo 'Hello $foo\nGoodbye!';
// Output:- 
// Hello $foo\nGoodbye!
1. Điều này có nghĩa là bốn không gian sẽ bị xóa từ đầu cả hai dòng của chuỗi. Kết quả là chỉ có dòng thứ hai sẽ trông thụt vào.

Nó rất quan trọng để đảm bảo rằng không có dòng nào của chuỗi có ít khoảng trắng hơn trước mặt chúng so với trước số nhận dạng đóng; Nếu đây là trường hợp, một lỗi cú pháp sẽ bị PHP.PHP.PHP.PHP.

$foo = 'bar';
echo 'Hello $foo\nGoodbye!';
// Output:- 
// Hello $foo\nGoodbye!
1

Trong ví dụ trên, PHP đã ném một lỗi cú pháp vì dòng đầu tiên của chuỗi không có khoảng trắng phía trước, nhưng mã định danh đóng yêu cầu bốn khoảng trắng.PHP throws a syntax error because the first line of the string has no whitespace in front of it, but the closing identifier requires four spaces.PHP throws a syntax error because the first line of the string has no whitespace in front of it, but the closing identifier requires four spaces.PHP throws a syntax error because the first line of the string has no whitespace in front of it, but the closing identifier requires four spaces.

Một thay đổi khác của cú pháp được giới thiệu với PHP 7.3 là mã định danh đóng có thể được theo sau bởi mã khác trên cùng một dòng. Vì vậy, theo Php 7.3, những điều sau đây là hợp lệ:-PHP 7.3 is that the closing identifier can be followed by other code on the same line. So as of PHP 7.3 the following is valid:-PHP 7.3 is that the closing identifier can be followed by other code on the same line. So as of PHP 7.3 the following is valid:-PHP 7.3 is that the closing identifier can be followed by other code on the same line. So as of PHP 7.3 the following is valid:-

$foo = 'bar';
echo 'Hello $foo\nGoodbye!';
// Output:- 
// Hello $foo\nGoodbye!
2

Đóng các từ

Heredoc và NowDoc cung cấp các lựa chọn thay thế hữu ích để xác định chuỗi trong PHP cho cú pháp chuỗi được trích dẫn được sử dụng rộng rãi hơn. Chúng đặc biệt hữu ích khi chúng ta cần xác định một chuỗi kéo dài nhiều dòng (dòng mới cũng được giải thích khi được sử dụng trong các chuỗi được trích dẫn) và khi việc sử dụng khoảng trắng là quan trọng. Chúng tôi cũng không cần phải lo lắng về việc thoát khỏi dấu ngoặc kép khi sử dụng Heredoc và NowDoc.PHP to the more widely used quoted string syntax. They are especially useful when we need to define a string that spans multiple lines (new lines are also interpreted when used in quoted strings) and where use of whitespace is important. We also don’t need to worry about escaping quote marks when using heredoc and nowdoc.PHP to the more widely used quoted string syntax. They are especially useful when we need to define a string that spans multiple lines (new lines are also interpreted when used in quoted strings) and where use of whitespace is important. We also don’t need to worry about escaping quote marks when using heredoc and nowdoc.PHP to the more widely used quoted string syntax. They are especially useful when we need to define a string that spans multiple lines (new lines are also interpreted when used in quoted strings) and where use of whitespace is important. We also don’t need to worry about escaping quote marks when using heredoc and nowdoc.

Cả hai đều có thể làm cho các chuỗi dễ đọc hơn trong mã, đặc biệt là với sự thư giãn của cả hai cú pháp được giới thiệu trong PHP 7.3. Vì vậy, lần tới khi bạn cần làm việc với một chuỗi dài, có lẽ bạn sẽ cân nhắc sử dụng Heredoc hoặc NowDoc để hoàn thành công việc.PHP 7.3. So next time you need to work with a long string, perhaps you’ll consider using heredoc or nowdoc to get the job done.PHP 7.3. So next time you need to work with a long string, perhaps you’ll consider using heredoc or nowdoc to get the job done.PHP 7.3. So next time you need to work with a long string, perhaps you’ll consider using heredoc or nowdoc to get the job done.

Sự khác biệt giữa Heredoc và NowDoc trong PHP là gì?

Các chuỗi Heredoc giống như các chuỗi được trích dẫn kép mà không thoát ra. Chuỗi mới giống như các chuỗi được trích xuất đơn mà không thoát ra.Nowdoc strings are like single-quoted strings without escaping.Nowdoc strings are like single-quoted strings without escaping. Nowdoc strings are like single-quoted strings without escaping.

Mục đích của việc sử dụng Heredoc và NowDoc trong PHP là gì?

Heredoc và NowDoc cung cấp các lựa chọn thay thế hữu ích để xác định chuỗi trong PHP cho cú pháp chuỗi được trích dẫn được sử dụng rộng rãi hơn.Chúng đặc biệt hữu ích khi chúng ta cần xác định một chuỗi kéo dài nhiều dòng (dòng mới cũng được giải thích khi được sử dụng trong các chuỗi được trích dẫn) và khi việc sử dụng khoảng trắng là quan trọng.provide useful alternatives to defining strings in PHP to the more widely used quoted string syntax. They are especially useful when we need to define a string that spans multiple lines (new lines are also interpreted when used in quoted strings) and where use of whitespace is important.provide useful alternatives to defining strings in PHP to the more widely used quoted string syntax. They are especially useful when we need to define a string that spans multiple lines (new lines are also interpreted when used in quoted strings) and where use of whitespace is important.provide useful alternatives to defining strings in PHP to the more widely used quoted string syntax. They are especially useful when we need to define a string that spans multiple lines (new lines are also interpreted when used in quoted strings) and where use of whitespace is important.

Heredoc PHP là gì?

HEREDOC PHP Cú pháp là một cách để viết khối văn bản lớn bên trong PHP, không có trích dẫn đơn lẻ, trích dẫn đôi.Nó dựa vào

a way to write large bloc of text inside PHP, without the classic single quote, double quotes delimiters. It relies on < and a token that will also mark the end of the string.

PHP bây giờ là gì?

NowDoc ¶ NowDocs là các chuỗi được trích xuất một lần mà Heredocs là các chuỗi được trích dẫn kép.Một nowDoc được chỉ định tương tự như Heredoc, nhưng không có phân tích cú pháp nào được thực hiện bên trong một nowDoc.Cấu trúc là lý tưởng để nhúng mã PHP hoặc các khối văn bản lớn khác mà không cần phải trốn thoát.single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.