解析错误:语法错误,意外'(',期待变量(T_VARIABLE)或'$'[重复]

This question already has an answer here:

I'm getting an error and I don't know how to resolve it..

The error is:

Parse error: syntax error, unexpected '(', expecting variable (T_VARIABLE) or '$' in C:\xampp\htdocsecords\delete-confirm.php on line 7

Line 7 is:

echo "<script>$(document).ready(function(){$('.modal-" . $row->id . "').hide();$('#delete-" . $row->id . "').click(function(){$('.modal-" . $row->id . "').show();});$('#cancel-" . $row->id . "').click(function(){$('.modal-" . $row->id . "').hide();});});</script>";

I have checked the whole line but there seems no errors in there? What is going wrong here?

</div>

By using double quotes " in an echo statement, PHP reads the value of the content inside the quotes. Single quotes ' assign a value to what's between the quotes.

$variable = 'Mia';  // assigns the value Mia to the $variable
echo '$variable';   // output is $variable;
echo "$variable";   // output is Mia;

In your example, wrap literal output in single quotes instead of double quotes and it will solve your problem.

echo '<div class="example" id="' . $variable . '">';

Actually it's (technically) enough to change every {$ to { $ (i.e. separated by a space) as in

echo "<script>$(document).ready(function(){ $('.modal-" . $row->id . "').hide();$('#delete-" . $row->id . "').click(function(){ $('.modal-" . $row->id . "').show();});$('#cancel-" . $row->id . "').click(function(){ $('.modal-" . $row->id . "').hide();});});</script>";

(or change the double-quoted php string to a single-quoted one, as mentioned earlier)

PHP switches in a slightly different parsing mode when you use "....{$var}..." instead of "... $var ..." and in your case it ticks the parser off because $( makes no sense to it.

But do those few whitespaces, necessary to make the code human readable, really hurt?

<?php
echo '<script>
    $(document).ready(function(){
        $(".modal-' . $row->id . '").hide();
        $("#delete-' . $row->id . '").click(function(){
            $(".modal-' . $row->id . '").show();
        });
        $("#cancel-' . $row->id . '").click(function(){
            $(".modal-' . $row->id . '").hide();
        });
    });
</script>';