WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] PHP if does not work (4 posts)

  1. laviolette99
    Member
    Posted 4 years ago #

    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,

  2. iridiax
    Member
    Posted 4 years ago #

    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 } ?>
  3. t31os
    Member
    Posted 4 years ago #

    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...

  4. laviolette99
    Member
    Posted 4 years ago #

    It works marvelously!

    Thanks to both of you guys!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags