首页 文章

在Jquery和PHP里面解析Error escape PHP [关闭]

提问于
浏览
-2

解析错误:语法错误,意外''(T_ENCAPSED_AND_WHITESPACE),期待标识符(T_STRING)或变量(T_VARIABLE)或数字(T_NUM_STRING)...

这是我得到的错误

<?php
      function my_custom_js() {
        echo " <script>" ;
        echo " jQuery(document).ready(function(){ 

    jQuery('#secondary-front .first h3').addClass('
     <?php $options = get_option('mytheme_theme_options'); 
     if(!empty($options['first_widget_icon'])) echo $options['first_widget_icon']?>    ');

    jQuery('#secondary-front .second h3').addClass('<?php $options =        get_option('mytheme_theme_options');
    if (!empty($options['second_widget_icon'])) echo $options['second_widget_icon'];?>');

    jQuery('#secondary-front .third h3').addClass('<?php $options =     get_option('mytheme_theme_options');
    if (!empty($options['third_widget_icon'])) echo $options['third_widget_icon'];?>');
    });  

    ";  
    echo "</script> ";
    }
    add_action('wp_head', 'my_custom_js');
?>

我无法正确转义此代码,我有php> jquery> php

1 回答

  • 2

    问题是你的报价( " )没有去研究这个问题,我已经完全为你重写了它:

    <?php
    
        function my_custom_js() {
            $options = get_option('mytheme_theme_options'); 
    
            echo "<script>
                jQuery(document).ready(function(){
                    jQuery('#secondary-front .first h3').addClass('" . ($options['first_widget_icon'] ?: NULL) . "');
                    jQuery('#secondary-front .second h3').addClass('" . ($options['second_widget_icon'] ?: NULL) . "');
                    jQuery('#secondary-front .third h3').addClass('" . ($options['third_widget_icon'] ?: NULL) . "');
                });
            </script>";
        }
    
        add_action('wp_head', 'my_custom_js');
    
    ?>
    

    我做过的一件事就是将 $options = get_option('mytheme_theme_options'); 移到了顶部 . 我've also removed the repeated calls to this. Also, that'具有连锁效应, echo 可以在1声明中完成,巧妙地使用ternary operator .

    echo ($something ?: NULL); 表示如果$ something存在,则回显它,否则不回显 .

    使用带有 ?: 速记的三元运算符需要PHP> = 5.3.0

    对于低于此的版本,只需填写中间部分,即:

    // PHP >= 5.3.0
    ($options['first_widget_icon'] ?: NULL)
    
    // PHP < 5.3.0
    ($options['first_widget_icon'] ? $options['first_widget_icon'] : NULL)
    

    当然,代码可能需要根据自己的喜好进行调整,但它应该是改进的基础 .

相关问题