Jump to content

Noob problem, code printed out as text


Recommended Posts

I'm trying to make a PHP feature on my Wordpress site that would randomly display one of three messages in the header, and the message displayed would be randomized each time the page is loaded. The message would contain a link. I'm this far:

 

In index.php, after  <?php get_header(); ?>, I have written or copy pasted:

 

$i = mt_rand(1,3);

switch ($i) {

    case 1:

        echo "message 1";

        break;

    case 2:

        echo "message 2";

        break;

    case 3:

        echo "message 3";

        break;

}

?>

 

But this whole code prints out as text. Apparently I'm missing something really basic here. Could someone help? Thank you a thousand times!

Link to comment
https://forums.phpfreaks.com/topic/202025-noob-problem-code-printed-out-as-text/
Share on other sites

im no means an expert but the echo are plain text. u need to make them a hyperlink

 

<a href="www.link1.com/index.php" title="link 1">message 1</a>

echo "<a href=www.link1.com/index.php" title="link 1">message 1</a>;
        break;

 

something like that

hyster: Thanks! That worked.

 

scvinodkumar: Now when I try to load the page, I get: "Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in C:\xampp\htdocs\wordpress\wp-content\themes\tutorial\index.php  on line 6"

 

Line 6 is: echo "<a href=www.link1.com/index.php" title="link 1">message 1</a>;

 

Entire code is:

 

<?php

$i = mt_rand(1,3);

switch ($i) {

    case 1:

        echo "<a href=www.link1.com/index.php" title="link 1">message 1</a>;

        break;

    case 2:

        echo "<a href=www.link1.com/index.php" title="link 1">message 2</a>;

        break;

    case 3:

        echo "<a href=www.link1.com/index.php" title="link 1">message 3</a>;

        break;

}

?>

The first " in

echo "<a href=www.link1.com/index.php" title="link 1">message 1</a>;

looked wrong as there is no closing ". Now I get: "Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\wordpress\wp-content\themes\tutorial\index.php  on line 6"

 

For some reason I could not edit my previous post to tell this.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.