开发者

Having <?php tags inside a js .html() method?

开发者 https://www.devze.com 2023-01-27 20:33 出处:网络
If we have: html(\'<a href=\"blabla.org\">go here</a>\') 开发者_C百科Can we have <?php tags inside inside href on this case, or it\'s a bad practice?

If we have:

html('<a href="blabla.org">go here</a>') 

开发者_C百科Can we have <?php tags inside inside href on this case, or it's a bad practice?

Thanks. MEM


You can have those, sure. Just know that the code inside it will be executed on the server, before the JS is executed. So if you have:

html('<a href="blabla.org"><?php echo 'go here'; ?></a>')

Your Javascript will become:

html('<a href="blabla.org">go here</a>') 

You can't influence the PHP code at the time the JS is executed.


theres nothing wrong with it but we do like to have some standards on writing php within view logic.

Firstly you should familiarize your self while / for / if / else / foreach loops within the alternative syntax.

This gives a more clearer prospective on html / php combined code., Example Below

<?php if ($a == 5): ?>
    <b>HTML</b> Here <br />
<?php endif; ?>

Theres also another trick that helps you keep your code clear and that's short tags but its not switched on in most conmen php server configurations so you have to make sure

html('<a href="blabla.org"><?=$blabla?></a>')

Have a look for yourself:

http://php.net/manual/en/control-structures.alternative-syntax.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号