Newbie PHP/Yii question

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,

		));

	}

Check this … PHP if

I looked at that link, but when I apply it to this code, I don’t see what constitutes the “statement group” it refers to. is the execution of


$dataProvider=new CActiveDataProvider('Post', array(... p

dependent on the “if” clause? Why not? I don’t think white space has any meaning in php so I’m assuming that it is dependent on the if clause…


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,

                ));

  1. You need no curly braces when you want to execute just one statement conditionally.

  2. And if you want to execute 2 or more statements (i.e. a group of statements) conditionally, then you have to enclose them in a pair of curly braces.

  3. You may enclose a single conditional statement in the braces.

So the following 2 are the same in their effects.




// #1

if ( $a > $b )

  function_A();

function_B();


// #2

if ( $a > $b ) {

  function_A();

}

function_B();



function_B() will always be called because it’s not under the “if” structure.

Some folks like to write as #1 and others #2.

The if statement you refer to is only setting the criteria when a tag has been selected. This allows the procedure to work for both cases, limited by a tag search or not. In every case, the new dataprovider is created and passed to the index view.