Php variable when to add brackets {}

Source: Internet
Author: User
For example, $ sqlinsertintoarticle ('Channel _ id', 'title', 'detail', 'pub _ Time') values ({$ cid}, {$ title }, {$ detail}, {$ time}); can be added without adding. What does adding {} mean? Why does the field name need to be included in?

For example, $ SQL = insert into article ('Channel _ id', 'title', 'detail ', 'pub _ Time') values (' {$ cid }', '{$ title}', '{$ detail}', '{$ time}'); can be added without adding? Why does the field name need to be included in? ========================================================== ============

For example:
$ SQL = "insert into article ('Channel _ id', 'title', 'detail ', 'pub _ Time') values (' {$ cid }', '{$ title}', '{$ detail}', '{$ time }');";

It seems that you can do it without adding {}. What does adding {} mean?
Why does the field name need to be included in?

========================================================== ============

Best Answer

At least it is easy to read ~~~ ''Is required by the insert into statement, because the string must appear in pairs, the addition of {} is sometimes used to prevent the variable name and the subsequent string from being connected together. For example, {$ cid} dd If cid = aa then {$ cid} dd = aadd if this parameter is not added, You can see $ ciddd on your own, won't it become a ciddd variable ~~

The meaning of the PHP variable in braces

  1. // The following is okay as it's inside a string. Constants are not
  2. // Looked for within strings so no E_NOTICE error here
  3. Print "Hello $ arr [fruit]"; // Hello apple
  4. // With one exception, braces surrounding arrays within strings
  5. // Allows constants to be looked
  6. Print "Hello {$ arr [fruit]}"; // Hello carrot
  7. Print "Hello {$ arr ['fruit']}"; // Hello apple


The following comparison explains the cause:

  1. Indicates that the variable in {} is processed according to the variable during execution.
  2. The special include method used to reference a variable in a string, so that the. operator is not used, thus reducing the input of code.

In fact, the output block is equivalent to print "hello". $ arr ['fruit'];

PHP: The Role of braces (curly braces {}) in string variables

Add a braces {} next to the PHP variable, and fill in a number, which is the character of the corresponding sequence number of the PHP variable.
For example:
$ Str = 'hello ';
Echo $ str {0}; // The output is h.
Echo $ str {1}; // The output is e.
If you want to check whether a string contains a certain length, consider using this braces (curly braces) and isset to replace the strlen function, because isset is a language structure and strlen is a function, therefore, using isset is more efficient than using strlen.
For example, to determine whether the length of a string is less than 5:
If (! Isset ($ str {5}) is better than if (strlen ($ str) <5.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.