简单 PHP 字符串检查不起作用

Simple PHP string check not working

我什至讨厌 post 这个问题,但我似乎无法让它为我工作。我可以直接在 if 语句之前回显 $the_page,但是 if 语句总是出现“else”。有任何想法吗?提前谢谢你。

<?php if(is_page('sp')){$the_page = 1;} else { };

if($the_page === 1) { ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $("#form-interest option:first:contains('---')").html('Área De Interés*:');
        });
    </script>
<?php } else { ?>
    <script type="text/javascript">
        jQuery(document).ready(function($) {
            $("#form-interest option:first:contains('---')").html('Area of Interest*:');
        });
    </script>
<?php } ?>

编辑: 使用您的一些想法,我将其清理得更干净。 is_page() 是一个 Wordpress 调用,在这个修订版中我得到了西班牙语 "Ir al Inicio" 正确打印(所以 is_page 函数必须工作),但 $interest 不打印“ Área De Interés*:“这是应该的。 else 正确输出文本和 $interest 字符串。

<?php if(is_page('sp')) { $interest = 'Área De Interés*:'; ?>
    Ir al Inicio
<?php } else { $interest = 'Area of Interest*:'; ?>
    SCROLL TO TOP
<?php } ?>
<script type="text/javascript">
    jQuery(document).ready(function($) {
        var interest = '<?php echo $interest; ?>';
        $("#form-interest option:first:contains('---')").html(interest);
    });
</script>

我只是 运行 最基本的 if 语句:

<?php 
  $the_page=1;

  if($the_page === 1) {
    echo "1: $the_page=$the_page";
  }
  else{ 
    echo "else: $the_page=$the_page";
  }
//echos "1: $the_page=1"

然后

<?php 
  $the_page=frenchfry;

  if($the_page === 1) {
    echo "1: $the_page=$the_page";
  }
  else{ 
    echo "else: $the_page=$the_page";
  }
//echoes else: $the_page=frenchfry

我会说问题出在 is_page('sp');你说你能回应它并验证它是一个数字吗1?

检查你的条件 if(is_page('sp')) ,这肯定 return false 所以你的变量 $the_page 没有设置为 1 所以它总是在你的 else statement.

在我看来,您正在关闭语句中的 php 标签,所以它 运行 都是因为它们都是普通的 html。你只能看到最后一个,因为它发生在最后。试试这个:

<?php if(is_page('sp')){$the_page = 1;} else { };

if($the_page === 1) {
    echo "<script type='text/javascript'>".
        "jQuery(document).ready(function($) {".
            "$('#form-interest option:first:contains('---')').html('Área De Interés*:');".
        "});".
    "</script>";
<?php } else { 
    echo "<script type='text/javascript'>" .
        "jQuery(document).ready(function($) {".
            "$('#form-interest option:first:contains('---')').html('Area of Interest*:');".
        "});".
    "</script>";
    }
?>

你似乎对语法有点困惑,所以希望你能理解。

关于我的最后一条评论:我确定某处存在 JavaScript 错误。如果您检查源代码,变量设置正确,因此 PHP 按预期工作。

查看 Chrome 中的 JavaScript 控制台,我看到以下错误消息,该消息仅出现在西班牙语版本中,因此使其成为进一步调查的理想候选者:

Uncaught TypeError: Cannot read property 'match' of undefined in jquery.easytabs.min.js?ver=3.2.0:12