10

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

This is the first example

This is the second example

<!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.

5
  • does it work when executed? it's just a PHPStorm problem? 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 Commented Aug 2, 2016 at 11:25
  • The code works just fine. I am having a trouble PhpStorm 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. Commented Aug 2, 2016 at 11:30
  • 1
    to get rid of the warning, write <?php echo 'var myJavascriptVar = '.$myVar; ?>; Commented Nov 7, 2016 at 13:14

2 Answers 2

10

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

-- var myJavascriptSecondVar = {{$myVar;}}; -- For this statement I can understand there is a bug for laravel template. However -- var myJavascriptVar = <?php echo $myVar; ?>; -- for this statement I think PhpStorm should not give this error because it should be very common I guess.
Yes .. but that works without extra warnings in .php files ... but here you have .blade.php which is a completely different File Type (in IDE eyes). Every language is implemented/handled separately -- PHP+JS may have special exclusions already in place; now it needs to be extended to cover Blade files as well. I may only suggest to submit another ticket on Issue Tracker for this moment.
5

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];

1 Comment

var data = [{{ $data }}][0]; //this is perfect for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.