Heim > php教程 > php手册 > Hauptteil

php变量什么情况下加大括号{}

WBOY
Freigeben: 2016-06-06 19:47:20
Original
1433 Leute haben es durchsucht

如: $sql = insert into article (`channel_id`,`title`,`detail`,`pub_time`) values ('{$cid}','{$title}','{$detail}','{$time}');; 不加乎也可以,加{}是什么意思呢? 还有字段名 为什么要以``包括呢? ==================================================

如:
$sql = "insert into article (`channel_id`,`title`,`detail`,`pub_time`) values ('{$cid}','{$title}','{$detail}','{$time}');";

不加似乎也可以,加{}是什么意思呢?
还有字段名 为什么要以``包括呢?

 

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

 

最佳答案

至少便于阅读嘛~~~''是insert into语句要求的,因为字符串要成对出现嘛
加{}有时候是为了防止变量名和后面的字符串连在一起嘛
例如
{$cid}dd
如果cid=aa
那么{$cid}dd=aadd
不加的话你自己看看了$ciddd,岂不变成了ciddd变量了~~ 
Nach dem Login kopieren

 

PHP变量放在大括号里面的含义

 

 

        
  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.          
  5.   //   With   one   exception,   braces   surrounding   arrays   within   strings        
  6.   //   allows   constants   to   be   looked   for        
  7.   print   "Hello   {$arr[fruit]}";         //   Hello   carrot        
  8.   print   "Hello   {$arr['fruit']}";     //   Hello   apple    


下面几个比较能说明原因的解释是:

        
  1. 表示{}里面的是一个变量  ,执行时按照变量来处理    
  2. 在字符串中引用变量使用的特殊包括方式,这样就可以不使用.运算符,从而减少代码的输入量了。

其实输出那块是等同于print   "hello   ".$arr['fruit'];  

 

PHP: 字符串变量中大括号(花括号{})的作用

 

PHP 变量后面加上一个大括号{},里面填上数字,就是指 PHP 变量相应序号的字符。
例如:
$str = 'hello';
echo $str{0}; // 输出为 h
echo $str{1}; // 输出为 e
如果要检查某个字符串是否满足多少长度,可以考虑用这种大括号(花括号)加 isset 的方式替代 strlen 函数,因为 isset 是语言结构,strlen 是函数,所以使用 isset 比使用 strlen 效率更高。
比如判断一个字符串的长度是否小于 5:
if ( !isset ( $str{5} ) ) 就比 if ( strlen ( $str )
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!