A
A
andr20172016-09-14 16:18:11
PHP
andr2017, 2016-09-14 16:18:11

Is it worth taking out the values ​​in variables or substituting them directly?

2 options, in one the values ​​​​are placed in variables, in the other they are substituted directly, what is the right way to do it, in which cases do you substitute directly, in which ones should they be taken out separately?

//данные вынесенны в переменные
    public function actionGet_places($term) {
        if(!Yii::app()->request->isAjaxRequest)
            throw new CException('Попытка прямого доступа');

        $term = CHtml::encode($term);

        $sellersId = Yii::app()->user->getSellerId( ! Yii::app()->user->isSeller( Yii::app()->user->id) );

        $places = Yii::app()->db->createCommand()
                ->selectDistinct('place')
                ->from('trips')
                ->where('place LIKE :place AND sellers_id = :sellers_id',
                        [':place' => '%'.$term.'%', ':sellers_id' => $sellersId ])
                ->queryColumn();

        echo CJSON::encode($places);
    }

 	//данные подставляются на прямую
    public function actionGet_places($term) {
        if(!Yii::app()->request->isAjaxRequest)
            throw new CException('Попытка прямого доступа');

        $places = Yii::app()->db->createCommand()
                ->selectDistinct('place')
                ->from('trips')
                ->where('place LIKE :place AND sellers_id = :sellers_id',
                        [':place' => '%'.CHtml::encode( $term ).'%', ':sellers_id' => Yii::app()->user->getSellerId( ! Yii::app()->user->isSeller( Yii::app()->user->id) )])
                ->queryColumn();

        echo CJSON::encode($places);
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yustas Alexu, 2016-09-14
@andr2017

Personally, I create a variable when I start writing the same thing over and over. but in general I don’t think about such things for a long time, everything is done automatically.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question