This question is about code found in the demo blog, which forms the tutorial at http://www.yiiframework.com/doc/blog/1.1/en/post.display.
This is probably a newbie php quetion. I looked in the php manual but couldn’t figure it out.
This code (full function copied below), from postController.php, is meant to list all the blog posts. This code uses the following conditional
if(isset($_GET['tag']))
My question is, if this passes (i.e. _GET[‘tag’] is isset), then what code is executed. According to my understanding of the blog post linked to above, only
$criteria->addSearchCondition('tags',$_GET['tag']);
would be executed. However, if that’s true, I don’t understand what limits the scope of the conditional to that line of code. I’m expecting some { } curly braces to limit the scope like this
if(isset($_GET['tag'])) {
$criteria->addSearchCondition('tags',$_GET['tag']);
}
In the absence of those curly braces, I’m assuming that everything that follows the
if(isset($_GET['tag']))
is subject to the conditional. In other words, nothing else in the function will be executed unless
if(isset($_GET['tag']))
[b]Code from postController.php
[/b]
/**
* Lists all models.
*/
public function actionIndex()
{
$criteria=new CDbCriteria(array(
'condition'=>'status='.Post::STATUS_PUBLISHED,
'order'=>'update_time DESC',
'with'=>'commentCount',
));
if(isset($_GET['tag']))
$criteria->addSearchCondition('tags',$_GET['tag']);
$dataProvider=new CActiveDataProvider('Post', array(
'pagination'=>array(
'pageSize'=>Yii::app()->params['postsPerPage'],
),
'criteria'=>$criteria,
));
$this->render('index',array(
'dataProvider'=>$dataProvider,
));
}