问题
I've this snippet of code in main layout file (protected/views/layouts/main.php):
<?php if (defined('YII_DEBUG')) { ?>
<button id="search-hidden-button"></button>
<script>
$(function(){
$('#search-hidden-button').click(function(){
$('#search-query-form').submit();
});
});
</script>
<?php } ?>
But I get this error and I dont know why:
Use of undefined constant - assumed ' '
<?php if (true) { ?>
<button id="search-hidden-button"></button>
<script>
$(function(){
$('#search-hidden-button').click(function(){
$('#search-query-form').submit();
});
});
</script>
<?php } ?>
回答1:
You pressed a combination of ALT-GR + SPACE that your editor shows as a normal space but in fact is a special character that PHP can't decode.
This happens usually on Italian Keyboards, where "{" is printed with ALT-GR + SHIFT + [. When you type really fast (as good programmers do :) you do like this
- Press shift and ALT-GR almost at the same time
- Press [
- Release [
- Release ALT-GR and SHIFT
- Press Spacebar
Sometimes it happens that step 4 and 5 are switched, and you get your unknown character.
Remove all spaces in your IF statements and try again, it will be fixed (then re-space correctly!)
回答2:
No need defined again, try :
if(YII_DEBUG){}
来源:https://stackoverflow.com/questions/19157772/how-to-check-if-a-constant-is-defined-in-main-layout