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

Simple PHP string check not working

本文关键字:检查 不起作用 字符串 PHP 简单      更新时间:2023-09-26

我甚至不想发布这个问题,但我似乎无法解决这个问题。我可以在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>

参考我的最后一条评论:我确信某个地方存在JavaScript错误。如果检查源代码,就会正确设置变量,因此PHP可以正常工作。

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

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

我刚刚运行了基本的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')),这肯定会返回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>";
    }
?>

你似乎在语法上有点困惑,所以希望这有点道理。