MEM
MEM

Reputation: 31347

Why does this line of code gets executed even if the conditional returns false?

perhaps someone could help me here. I have those lines of code:

1 $(document).ready(function(){
2 if('<?php echo Yii::app()->controller->action->id?>' == 'update'){
3   if('<?php echo Yii::app()->user->id;?>' != '<?php echo $model->createUser->id; ?>'){

I'm getting,

trying to access property of a non-object (on line 3).

And it makes sense, because I have no object with that property here.

The problem is that the second if is running, even if, the first if condition returns FALSE.

Yii::app()->controller->action->id is NOT equal to 'update' so, the second IF shouldn't NEVER execute. So, I don't understand.

Can anyone please clarify whats going on here plz ?

Cheers

Upvotes: 0

Views: 162

Answers (1)

ernie
ernie

Reputation: 6356

The PHP will always evaluate, because that's happening server side, while the Javascript is going to execute on client side. By the time the code gets to the browser, the PHP has all been evaluated and replaced, leaving just Javascript. Put another way, all the PHP in your view is going to be evaluated (probably by the PHP interpreter in Apache), and the Javascript won't matter until the code runs in the browser.

You need to separate your server-side and client-side code.

Your code is failing server side, since all the PHP has to evaluate to render the Javascript, and in this case, the third PHP block:

<? if(isset($model)) echo $model->createUser->id; ?>

Looks like when the PHP is running, $model has been set, just not to the expected object.

Upvotes: 2

Related Questions