PHP variable Under what circumstances braces {}

Source: Internet
Author: User


Such as:
$sql = "INSERT into article (' channel_id ', ' title ', ' Detail ', ' pub_time ') values (' {$cid} ', ' {$title} ', ' {$detail} ', ' {$ Time} '); ";

What does it mean to add {}?
And why do you include the field name?

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

Best Answer

The meaning of the PHP variable placed in curly braces

  1. //The following is okay as it ' s inside a string. Constants is not
  2. //looked for within strings so no E_notice error here
  3. print "Hello $arr [fruit]"; //Hello Apple
  4.      
  5. //With one exception, braces surrounding arrays within strings
  6. //allows constants to being looked for
  7. Print "Hello {$arr [fruit]}"; //Hello carrot
  8. Print "Hello {$arr [' Fruit ']}"; //Hello Apple

Here are some explanations for why:

    1. Represents a variable inside {}, which is handled by variable when executed
    2. Refer to the special inclusion of variables in a string, so that you can reduce the amount of code input by not using the. operator.

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

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

The PHP variable is appended with a curly brace {}, which is filled with numbers, which refers to the character of the corresponding ordinal of the PHP variable.
For example:
$str = ' Hello ';
echo $str {0}; Output is H
echo $str {1}; Output to E
If you want to check how much length a string satisfies, consider replacing the strlen function with this curly brace (curly brace) and isset, because Isset is a language structure and strlen is a function, so using isset is more efficient than using strlen.
For example, determine whether a string is less than 5 in length:
if (!isset ($str {5})) is better than if (strlen ($STR) < 5).


PHP variable Under what circumstances braces {}

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.