when to use quotes

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

PHP: Strings

As to the strings in PHP, you should read this portion of PHP manual:

http://php.net/manual/en/language.types.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.yiiframework.com/doc/api/1.1/CDataColumn#value-detail

http://jp.php.net/manual/en/function.eval.php