Yii Framework Forum: when to use quotes - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

when to use quotes Rate Topic: -----

#1 User is offline   Jimlam 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 195
  • Joined: 17-March 12

Posted 28 June 2012 - 08:23 AM

I have a question: when should we use quotes?

<?php echo CHtml::link('Create Employee',array('employee/create','id'=>$model->id)); ?>


The above works without quotes around $model->id. If I put quotes, it does not work.

When to use simple quotes and when to use double quotes. I haven't figured out how to use them. At times, if I put quotes(simple or double), it does not work at all. But at times, I must use quotes for it to work.

<?php $this->widget('zii.widgets.grid.CGridView', array(

	'id'=>'employee-grid',

	'dataProvider'=>$model->search(),

	'filter'=>$model,

	'columns'=>array(

		'id',
		'departmentId',
      array('name'=>'dept_name', 'value'=>'$data->department->name'),
      'username',
  		'firstName',



The above ('$data->department->name') does not work without quotes
0

#2 User is offline   Roman Solomatin 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 215
  • Joined: 21-October 10
  • Location:Tallinn, Estonia

Posted 28 June 2012 - 10:57 AM

PHP: Strings
0

#3 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 3,411
  • Joined: 16-February 11
  • Location:Japan

Posted 28 June 2012 - 10:58 AM

As to the strings in PHP, you should read this portion of PHP manual:
http://php.net/manua...ypes.string.php

To summarize, "$something" in double quotes is treated as a variable and will be expanded, while '$something' in single quotes is treated as a literal string:
$name = 'Bianca';
echo $name;               // Bianca
echo "Hello, " . $name;   // Hello, Bianca
echo 'Hello, ' . $name;   // Hello, Bianca
echo "Hello, $name";      // Hello, Bianca
echo 'Hello, $name';      // Hello, $name


We usually use a single-quoted string that contains '$data' when we set CDataColumn::value, because we want to pass the literal string of '$data' to it.

CDataColumn::value is an "expression" string that is to be passed to "eval()" function each time the cell get displayed. And once it is passed to eval(), '$data' in the expression will be treated as a variable.

CGridView will populate $data with the data model of the row each time before it calls eval(). So, for example, the expression of '$data->name' will return 'name' attribute of the model when it is eval()ed.

But until eval()ed, we don't want $data to be expanded as a variable.

http://www.yiiframew...mn#value-detail
http://jp.php.net/ma...nction.eval.php
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users