Hướng dẫn how do you put quotation marks in php? - làm thế nào để bạn đặt dấu ngoặc kép trong php?

Tôi đang gặp lỗi phân tích cú pháp và tôi nghĩ đó là do dấu ngoặc kép trên

A
                      
                      Alphabet Soup
                      @@@
                      
8. Làm thế nào tôi có thể làm cho nó đối xử với nó như một chuỗi toàn bộ?

Vấn đề là tôi không thể thêm

A
                      
                      Alphabet Soup
                      @@@
                      
9 trước mỗi dấu ngoặc kép. Đây là văn bản thực tế tôi cần so sánh.

Sử dụng dấu ngoặc kép trong PHP Câu hỏi này dường như xuất hiện rất nhiều cho các nhà phát triển mới sử dụng PHP. & nbsp; Và nó khiến tôi suy nghĩ, "Làm thế nào chúng ta có thể giải thích các quy tắc cho dấu ngoặc kép?" & nbsp; Dù tốt hay xấu, PHP có rất nhiều quy tắc! & NBSP; Bài viết này cố gắng trả lời các câu hỏi.
This question seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rules for quotation marks?"  For better or worse, PHP has so many rules!  This article tries to answer the questions.

Đầu tiên, một số đọc bắt buộc. & nbsp; bạn cần hiểu các thuật ngữ "biến" và "chuỗi" vì chúng được sử dụng trong bối cảnh lập trình máy tính. & nbsp; dành một chút thời gian để đọc các trang này. & nbsp; nếu không phải tất cả "nhấp vào" lúc đầu, đừng quá quan tâm. & nbsp; chúng tôi sẽ hiển thị một số ví dụ và giải thích dưới đây. http: Ngôn ngữ.operators.String.php
http://php.net/manual/en/language.variables.basics.php
http://php.net/manual/en/language.types.string.php
http://php.net/manual/en/language.operators.string.php

Dấu trích dẫn được sử dụng với chuỗi Một chuỗi không có gì khác hơn một vài ký tự được xâu chuỗi lại với nhau. & nbsp; đoạn mã bên dưới có các ví dụ về các chuỗi hoàn toàn hợp lệ. & nbsp; Chuỗi đầu tiên là chữ số vốn "A" và nó có độ dài của một ký tự. & nbsp; chuỗi thứ hai là chuỗi trống. & nbsp; nó có chiều dài bằng không. & nbsp; bạn có thể có thể tìm ra độ dài của hai chuỗi khác.
A string is nothing more than a few characters strung together.  The code snippet below has examples of perfectly valid strings.  The first string is the single capital letter "A" and it has a length of one character.  The second string is the empty string.  It has a length of zero.  You can probably figure out the lengths of the other two strings.

A
                      
                      Alphabet Soup
                      @@@
                      

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.

$x = 'Alphabet Soup';
                      $y = "Alphabet Soup";
                      

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.$x, then $x is assigned to $y.

$x = 'Alphabet Soup';
                      $y = "$x";
                      

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.

$x = 'Alphabet Soup';
                      $y = $x;
                      

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.$y is "Alphabet Soup for lunch."

$x = 'Alphabet Soup';
                      $y = "$x for lunch";
                      

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.$y variable would contain " $x for lunch."  When single quotes are used, no variable substitution is performed.

$x = 'Alphabet Soup';
                      $y = '$x for lunch';
                      

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.

$x = 'Alphabet Soup';
                      $y = $x . ' for lunch';
                      

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.
PHP allows you to mix quotes and apostrophes in certain data structures, one of the most useful being the SQL query string.  You use the double quotes for the outermost wrapper and single quotes around the variables that are injected into the query.  See also When Quotes or Apostrophes are Part of the Data, below.

$nom = 'Ray';
                      $sql = "SELECT id FROM myTable WHERE name='$nom' LIMIT 1";
                      

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.

$x = '"';
                      $x = "'";
                      

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.

A
                      
                      Alphabet Soup
                      @@@
                      
0

Mở trong cửa sổ mới

Nếu bạn muốn gán các giá trị chuỗi theo nghĩa đen này cho các biến PHP, bạn phải đặt báo giá xung quanh chúng trong câu lệnh gán. & nbsp; cả hai ví dụ này đều được chấp nhận. & nbsp; Lưu ý rằng một người sử dụng các trích dẫn đơn và phần còn lại sử dụng dấu ngoặc kép.

Khi bạn sử dụng các biến (trái ngược với các chuỗi theo nghĩa đen) trong PHP, có những ý nghĩa khác nhau cho các trích dẫn đơn và đôi. & nbsp; trích dẫn kép cho phép thay thế biến; Trích dẫn đơn không. & nbsp; Ví dụ mã này giống hệt chức năng với ví dụ trước ngay lập tức. & nbsp; Đầu tiên, bảng chữ cái được gán cho $ x, sau đó $ x được gán cho $ y.\t and you can insert Unix end-of-line characters by using \n.  (But there is a better way of inserting end-of-line characters: use the predefined and context aware constant, PHP_EOL instead.  PHP knows what end-of-line sequence is appropriate for the OS and working environment.)

Vì không có hỗn hợp các giá trị chuỗi theo nghĩa đen và các giá trị biến, PHP cũng sẽ cho phép bạn viết bài này mà không có bất kỳ trích dẫn nào. & NBSP; Đây là cách ưa thích để viết biểu thức; Tránh trích dẫn không cần thiết.
Quotation marks are not necessary or appropriate with other variable types, such as arrays, objects, integers, etc.  Just don't do that!  Advanced users: Learn about the magic method __toString().

Avoid Unnecessary Quotation Marks
If quotation marks are not necessary, do not use them.  Unnecessary quotes lead to fiddly punctuation and hard-to-find parse errors that waste your time.

Avoid Unnecessary Quotation Marks
Undoubtedly someone will ignore the advice to avoid unnecessary quote marks.  When you do that, you will need to use curly brackets around your substrings.  This fiddly punctuation is very easy to get wrong, resulting in parse errors.  That's why we try to avoid it.  Here is what the PHP.net site says about this syntax:  "Complex (curly) syntax... This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions. Any scalar variable, array element or object property with a string representation can be included via this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognized when the $ immediately follows the {. Use {\$ to get a literal {$."

A
                      
                      Alphabet Soup
                      @@@
                      
1

Open in new window

Quotation Marks Used With Constants
PHP constants should be wrapped in quotation marks when they are defined with the define() function.  Since there is no variable substitution involved, single quote marks are appropriate, and double quote marks are OK. too.  Constants that are defined by the PHP function are different from class constants that are created by the const statement.

Whether define() or const were used to create a constant, it's important to remember that PHP constants are always used without quotation marks.

Quotation Marks Cause Type Coercion
The string "35" is not the same value as the integer 35.  They are different data types.  The string occupies two bytes and provides a character representation.  The integer provides the numeric value.  Many programming languages make a distinction between these two different types of data.  But PHP will usually permit these two data elements to be used interchangeably.  Run this script to see the effect as PHP changes the variable types.  On line 2, we assign an integer value to $x.  On line 4, PHP will change the data from integer to string as it is assigned to $y.  And on line 6, $y will be used as if it is an integer.

A
                      
                      Alphabet Soup
                      @@@
                      
2

Open in new window

Duck Typing in PHP
PHP is a loosely typed language.  The PHP engine attempts to cover up the complexity of the concept of a variable that can be any type by providing a uniform and intuitive set of rules that allow type conversion.  What that means is that PHP will make its own decisions about data types, based on the context in which the data is used.  You have some, but not complete control over this.  If it looks like a duck and quacks like a duck, PHP calls it a duck.  Whether the original data was an integer or a string is often unimportant to PHP.

Required reading, with plenty of examples, here:
http://php.net/manual/en/language.variables.basics.php
http://php.net/manual/en/language.types.type-juggling.php
http://php.net/manual/en/language.types.string.php#language.types.string.conversion
http://php.net/manual/en/types.comparisons.php
http://php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Missing Quotation Marks Cause Time-Bombs
Here is something that looks OK but will get you fired from any professional programming job.

A
                      
                      Alphabet Soup
                      @@@
                      
3

Open in new window

The reason this is wrong, but permitted by PHP, goes to the ancient history of PHP, when it was called "Personal Home Page."  The authors wanted it to be a very easy language to use, and they thought that structured syntax was the source of difficulty in learning programming languages.  So in an effort to be forgiving and permissive they included all kinds of fall-back definitions, creating a cascade of meanings.  After running that little script, what is the value of $y?  The answer is, "You cannot really know!"  This happens because PHP will first try to use a as a constant.  If a is found, the constant value will be used as the index into the $x array.  If a is not found among the constants, PHP will raise a Notice and will pretend that you had put single quotes around 'a' and will retry the variable assignment.  But here's the gotcha: In the standard installation of PHP, Notice-level messages are suppressed, so you will never be told what PHP is doing to your array index!  The ambiguity will lie, latent, in your code for a long time.  Maybe, seemingly, forever.  Then one day, another programmer will need to work on the project, maybe in a different function or class, and she will define() the constant a.  At this point it will hit the fan, because like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope.  And without scope encapsulation, the constant is now injected into your programming.

Bây giờ, đột nhiên và không cần thông báo, A và 'A' có ý nghĩa khác nhau và kịch bản của bạn bắt đầu thất bại. & NBSP; Sử dụng báo giá hoặc dấu nháy đơn khi chúng là cần thiết.a and 'a' have different meanings, and your script begins to fail.  Use quotes or apostrophes when they are needed.

Khi trích dẫn hoặc dấu nháy đơn là một phần của dữ liệu (truy vấn SQL), bạn có thể nhúng các dấu ngoặc kép trong các chuỗi được trích dẫn hoặc các trình điều khiển đơn trong các chuỗi được trích dẫn đơn bằng cách thoát các trích dẫn. & nbsp; Thuật ngữ thoát trong bối cảnh này đề cập đến việc đánh dấu trích dẫn hoặc dấu nháy đơn theo cách loại bỏ ý nghĩa chung của nó như một dấu phân cách và cho phép nó trở thành một phần của dữ liệu chuỗi. & nbsp; Cách thoát khỏi dấu ngoặc kép phổ biến nhất là đặt dấu gạch chéo ngược \ ngay trước khi nhân vật bạn muốn trốn thoát. & nbsp; Trong mã này, dòng đầu tiên gây ra lỗi phân tích phân tích PHP, nhưng dòng thứ hai tạo một biến chuỗi chứa tên. & nbsp; Dòng thứ ba tạo ra cùng một biến chuỗi.
You can embed quotation marks in quoted strings or single-quotes in single-quoted strings by escaping the quotes.  The term escape in this context refers to marking the quote or apostrophe in a way that removes its common meaning as a delimiter and permits it to become part of the string data.  The most common way of escaping quote marks is to put the backslash \ immediately before the character you want to escape.  In this code snippet the first line causes a PHP parse error, but the second line creates a string variable containing the name.  The third line creates the same string variable.

A
                      
                      Alphabet Soup
                      @@@
                      
4

Mở trong cửa sổ mới

Một ứng dụng thực tế của khái niệm thoát được sử dụng trong các chuỗi truy vấn SQL. & nbsp; Chúng thường được xây dựng từ các chuỗi được trích dẫn kép, để cho phép thay thế biến để dữ liệu có thể được đưa vào truy vấn. & nbsp; Động cơ SQL dự kiến ​​dữ liệu chuỗi sẽ được đặt trong các dấu ngoặc kép đơn. & nbsp; hãy xem xét chuỗi sau để xem tại sao cần một lối thoát dấu gạch chéo ngược.

A
                      
                      Alphabet Soup
                      @@@
                      
5

Mở trong cửa sổ mới

Một ứng dụng thực tế của khái niệm thoát được sử dụng trong các chuỗi truy vấn SQL. & nbsp; Chúng thường được xây dựng từ các chuỗi được trích dẫn kép, để cho phép thay thế biến để dữ liệu có thể được đưa vào truy vấn. & nbsp; Động cơ SQL dự kiến ​​dữ liệu chuỗi sẽ được đặt trong các dấu ngoặc kép đơn. & nbsp; hãy xem xét chuỗi sau để xem tại sao cần một lối thoát dấu gạch chéo ngược.

Người dùng nâng cao: Xem thêm việc sử dụng trình tự thoát trong các biểu thức chính quy.
You can embed quotation marks in PHP strings with heredoc notation.  This is incredibly powerful and useful for many reasons, the foremost being variable substitution in templates.  You can write a PHP script that sets its variables, then assigns a string with heredoc notation, and all of the variables can be automatically substituted into the string.  If the heredoc string is a complete or partial HTML document, you can avoid most of the fiddly punctuation.  Here is an example showing how easy it can be:

A
                      
                      Alphabet Soup
                      @@@
                      
6

Mở trong cửa sổ mới

Một ứng dụng thực tế của khái niệm thoát được sử dụng trong các chuỗi truy vấn SQL. & nbsp; Chúng thường được xây dựng từ các chuỗi được trích dẫn kép, để cho phép thay thế biến để dữ liệu có thể được đưa vào truy vấn. & nbsp; Động cơ SQL dự kiến ​​dữ liệu chuỗi sẽ được đặt trong các dấu ngoặc kép đơn. & nbsp; hãy xem xét chuỗi sau để xem tại sao cần một lối thoát dấu gạch chéo ngược.
As a matter of client security, a script should never echo unfiltered data directly to the client browser (malicious JavaScript is the obvious danger).  Instead it's correct to use htmlspecialchars() on any character string that you want to embed in HTML.  HtmlSpecialChars() makes appropriate and safe translations of the characters with special meanings, such as quotes, wickets like < or > and the ampersand.  The resulting string can be displayed but cannot affect the browser or invoke JavaScript.  This function has application in XML documents, too.

Người dùng nâng cao: Xem thêm việc sử dụng trình tự thoát trong các biểu thức chính quy.
Have a close look at the next code snippet.  Can you see the different kinds of quotation marks?  Your browser may render these differently enough that you will be able to tell them apart, or it may not.  The quotation marks around the $user variable value are not the standard keyboard quote marks.  Instead they are something that was probably copied from Word for Windows or a similar text editor.  PHP does not recognize them as quote marks and instead tries to use them as part of the variable assignment for $user.  Needless to say this will not work out well!  But PHP will only throw a Notice message.  If you do not have error_reporting(E_ALL) set, you will not see the Notice message and the script will fail, perhaps without any useful error message.  Executive summary: Check your quote marks very carefully -- especially if you copy and paste using a text editor.  One quote from an EE member on this topic: "I found in textedit (Mac's version of notepad) you have to turn off substitutions / smart quotes."

A
                      
                      Alphabet Soup
                      @@@
                      
7

Mở trong cửa sổ mới

Một ứng dụng thực tế của khái niệm thoát được sử dụng trong các chuỗi truy vấn SQL. & nbsp; Chúng thường được xây dựng từ các chuỗi được trích dẫn kép, để cho phép thay thế biến để dữ liệu có thể được đưa vào truy vấn. & nbsp; Động cơ SQL dự kiến ​​dữ liệu chuỗi sẽ được đặt trong các dấu ngoặc kép đơn. & nbsp; hãy xem xét chuỗi sau để xem tại sao cần một lối thoát dấu gạch chéo ngược.
Read the man page links in this article, and make sure you understand them.  Use var_dump() to check the type and contents of your variables.  And post new questions here at EE if you're not 100% sure what PHP is doing to your data!

Người dùng nâng cao: Xem thêm việc sử dụng trình tự thoát trong các biểu thức chính quy.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

Khi trích dẫn hoặc dấu nháy đơn là một phần của dữ liệu (ký hiệu Heredoc), bạn có thể nhúng dấu ngoặc kép trong chuỗi PHP với ký hiệu Heredoc. & NBSP; Điều này cực kỳ mạnh mẽ và hữu ích vì nhiều lý do, quan trọng nhất là thay thế thay đổi trong các mẫu. & NBSP; Bạn có thể viết tập lệnh PHP đặt các biến của nó, sau đó gán một chuỗi với ký hiệu Heredoc và tất cả các biến có thể được tự động thay thế vào chuỗi. & NBSP; Nếu chuỗi HEREDOC là một tài liệu HTML hoàn chỉnh hoặc một phần, bạn có thể tránh hầu hết các dấu câu khó khăn. & NBSP; Dưới đây là một ví dụ cho thấy nó có thể dễ dàng như thế nào:
If you found this article helpful, please click the "thumb's up" button below. Doing so lets the E-E community know what is valuable for E-E members and helps provide direction for future articles.  If you have questions or comments, please add them.  Thanks!

Php sử dụng trích dẫn đơn hay đôi?

Trong PHP, mọi người sử dụng báo giá đơn để xác định chuỗi không đổi, như 'A', 'My Name', 'ABC XYZ', trong khi sử dụng báo giá kép để xác định một chuỗi chứa định danh như "A $ B $ C $ D". echo "của tôi $ a"; Điều này đúng với các chuỗi được sử dụng khác.single quote to define a constant string, like 'a' , 'my name' , 'abc xyz' , while using double quote to define a string contain identifier like "a $b $c $d" . echo "my $a"; This is true for other used of string.

Bạn có thể sử dụng các trích dẫn đơn 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.You can use it when you want the string to be exactly as it is written.

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?

Chỉ cần thoát khỏi họ: Echo "\" $ Time \ "";Bạn cũng có thể sử dụng đơn xung quanh các trích dẫn kép: echo '"'.

Làm thế nào để bạn thêm dấu ngoặc kép trong HTML?

Thẻ được sử dụng để thêm dấu ngoặc kép ngắn trong HTML.Chỉ cần lưu ý nếu trích dẫn dành cho nhiều dòng, hãy sử dụng thẻ.Trình duyệt thường chèn dấu ngoặc kép xung quanh phần tử Q.Bạn cũng có thể sử dụng thuộc tính trích dẫn để chỉ ra nguồn báo giá ở dạng URL.. Just keep in mind if the quotation goes for multiple lines, use
tag. Browsers usually insert quotation marks around the q element. You can also use the cite attribute to indicate the source of the quotation in URL form.