Javascript代码中的PHP条件

I need to add a PHP condition inside some javascript code like this:

<script>
$(document).ready(function() {

<?php if (some condition) {
   $('#myID').show();
}

});
</script>

Is this possible?

Try this

<?php if (some condition): ?>
$('#myID').show();
<?php endif; ?>

Depends on file extension.

If file extension is php (or some other you might have put together to run php code using .htaccss rules or apache config) it is fine not otherwise.

Sure, if wherever you're doing your JavaScript is executed by PHP:

<script type="text/javascript">
$(document).ready(function() {

<?php if(some condition) { ?>
   $('#myID').show();
<?php } ?>

});
</script>

If you want to do this dynamically, however, it would be a much nicer and faster solution to use Ajax instead.

Sure. Just echo out the JavaScript:

<script>
$(document).ready(function() {

<?php if (some condition) {
   echo("$('#myID').show();");
}?>

});
</script>

Php is going to output something when it is executed on the server side. So when the page load you will not see php code.

You should write your php code such that it will render the JS if condition when the php script is executed.

Something like this.

if (something == <?php outputSomeThingToCompare ?>) {
   $('#myID').show();
}

Or you can also try this.

<?php if(some condition) { ?>
   $('#myID').show();
<?php } ?>

Yes and no.

Remember that PHP processes your page server side. So, if you want to change what is in your JavaScript dynamically, then yes, you can do this.

If instead you want PHP to process variables from within JavaScript on the client side, then no, this isn't possible.

Again, the browser has no knowledge of PHP.

This is not possible because JavaScript has been compiled on the client side however php works over the server. You can do this by changing it to this ...

$string  = '<script>
$(document).ready(function() {';
if (some condition) {
$string .= '$('#myID').show();';
$string .= '}

});
</script>';';

Please concat code if error occures.

Yes, it's possible, but your <?php instruction doesn't seem to be closed correctly. Where's the closing ?> ?

If think you need this:

<script>
    $(document).ready(function() {  
        <?php 
        if (some condition) { 
            echo('$("#myID").show();');
        } 
        ?>
    });                             //$(document).ready
</script>

You can put your script inside the PHP file that will be rendering your html and check conditions with PHP.

Example index.php

<html><head></head>
<body>
Your webpage content here

<script type="text/javascript">
    $(document).ready(function () {
        <?php if(condition) : ?>
            $('#myID').show();
        <?php else : ?>
            $('#mySecondID').show();
        <?php endif; ?>
    });
</script>

</body>
</html>

If u want to put your JS in a separate file you could do so and initialize some variables inside PHP file that will be rendering your html (for example index.php or whatever template file) the same way as above and this way you can read them from your .js file.

Example: index.php

<html><head></head>
<body>
Your webpage content here
<?php
  $foo = 'foo';
  $bar = 'bar';
?>
<script type="text/javascript">
        var foo = '<?php echo (condition) ? $foo : $bar ?>'; 
</script>

</body>
</html>

init.js

$(document).ready(function () {
    if(foo == 'foo')
        $('#myID').show();
});