最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Expression expected in PhpStorm when using php variables in JavaScript functions - Stack Overflow

programmeradmin6浏览0评论

When I am using changing php variables to JavaScript variables, I am getting "expression expected" error from PhpStorm.

I cannot change the extension of the file to something.js.php because I am already using blade template so it should be blade.php

<!DOCTYPE html>
<html>
<body>
<?php $myVar = 5;?>

<script type="text/javascript">
    var myJavascriptVar = <?php echo $myVar; ?>;
    var myJavascriptSecondVar = {{$myVar;}};
    alert(myJavascriptVar + myJavascriptSecondVar);
</script>
</body>
</html>

When I am using changing php variables to JavaScript variables, I am getting "expression expected" error from PhpStorm.

I cannot change the extension of the file to something.js.php because I am already using blade template so it should be blade.php

<!DOCTYPE html>
<html>
<body>
<?php $myVar = 5;?>

<script type="text/javascript">
    var myJavascriptVar = <?php echo $myVar; ?>;
    var myJavascriptSecondVar = {{$myVar;}};
    alert(myJavascriptVar + myJavascriptSecondVar);
</script>
</body>
</html>

I have added a sample html page for more clarification. In PhpStrom the

var myJavascriptVar = <?php echo $myVar; ?>;

and

 var myJavascriptSecondVar = {{$myVar;}};

statements gives expression expected error.

Share Improve this question edited Aug 2, 2016 at 11:44 Serdar Saygılı asked Aug 2, 2016 at 11:21 Serdar SaygılıSerdar Saygılı 4713 silver badges14 bronze badges 5
  • does it work when executed? it's just a PHPStorm problem? – NDM Commented Aug 2, 2016 at 11:23
  • You can set the coding templates etc... in the settings in jetbrains products. You can define witch errors phpstorm should check for, and disable where it shouldnt – RFLdev Commented Aug 2, 2016 at 11:25
  • The code works just fine. I am having a trouble PhpStorm – Serdar Saygılı Commented Aug 2, 2016 at 11:27
  • Please attach/share file that has this issue (using original file name) -- code could be simplified just enough to reproduce the issue. ideally -- create and share sample project (whole project). Right now it's not 100% clear (from these small screenshots) what context it has (file type etc). Full screenshot of the whole editor window (where editor tab with file name/icon is visible) might help as well. – LazyOne Commented Aug 2, 2016 at 11:30
  • 1 to get rid of the warning, write <?php echo 'var myJavascriptVar = '.$myVar; ?>; – cari Commented Nov 7, 2016 at 13:14
Add a comment  | 

2 Answers 2

Reset to default 10

That's a bug (incomplete inter-language handling) in PhpStorm.

  • https://youtrack.jetbrains.com/issue/WI-24968
  • https://youtrack.jetbrains.com/issue/WI-25739
  • possibly some another (from "Related" list) as well

Watch those tickets (star/vote/comment) to get notified on any progress. Right now they are not assigned to any specific future versions.

Here are two workarounds:

1. function

function blade(_)
{
    return _;
}

var data = blade({{ $data }});
// or ES6 arrow function
var data = (_ => _)({{ $data }});

2. array

var data = [{{ $data }}].pop();
// or
var data = [{{ $data }}][0];
发布评论

评论列表(0)

  1. 暂无评论