WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] PHP if does not work

[Resolved] PHP if does not work

  • I’m trying to implement a if statement to execute a script based on the author’s id and it does not work. The PHP engine says that there’s an unxepected < (the one that starts the script).

    Here’s the code:

    <?php
    $atr=get_the_author_ID();
    if ($atr==6)
    {
    <script type=”text/javascript”>
    var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
    document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
    </script>
    <script type=”text/javascript”>
    var pageTracker = _gat._getTracker(“UA-697623-9”);
    pageTracker._initData();
    pageTracker._trackPageview();
    </script>

    }

    elseif ($atr==4)
    {
    <script type=”text/javascript”>
    var gaJsHost = ((“https:” == document.location.protocol) ? “https://ssl.” : “http://www.”);
    document.write(unescape(“%3Cscript src='” + gaJsHost + “google-analytics.com/ga.js’ type=’text/javascript’%3E%3C/script%3E”));
    </script>
    <script type=”text/javascript”>
    var pageTracker = _gat._getTracker(“UA-697623-19”);
    pageTracker._initData();
    pageTracker._trackPageview();
    </script>

    }
    ?>

    How can I insert a script in a if statement?

    Thanks,

Viewing 3 replies - 1 through 3 (of 3 total)
  • How can I insert a script in a if statement?

    Try this format:

    <?php $atr=get_the_author_ID(); ?>
    
    <?php if ($atr==6) { ?>
    <!-- code here -->
    
    <?php } elseif ($atr==4) { ?>
    <!-- code here -->
    
    <?php } else { ?>
    <!-- code here -->
    
    <?php } ?>

    That’s right, either switch out of PHP using ?> and <?php or use echo/print, or whatever else…

    You code is being parsed as PHP, when you’re wanting to output into HTML, so you need to either switch out, as per the example above or use echo/print, etc…

    It works marvelously!

    Thanks to both of you guys!

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] PHP if does not work’ is closed to new replies.