funguy
funguy

Reputation: 2160

How to echo a variale in html (newbie)

I wan to echo a random swf variable into html, but I can not seem to get the right result.

By checking with firebug I can see that it outputs my php code php echo '$randomImage'

What I have so far:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML+RDFa 1.0//EN" "http://www.w3.org/MarkUp/DTD/xhtml-rdfa-1.dtd">
<html>
<head>

<style media="screen" type="text/css">

.content {
    position: absolute;
    height: 200px;
    width: 400px;
    margin: -100px 0 0 -200px;
    top: 25%;
    left: 50%;
}

</style>

</head>

<body>
<?php

$imagesDir = '/';

$images = glob($imagesDir . '*.{swf}', GLOB_BRACE);

$randomImage = $images[array_rand($images)];

?>


<div class="content"> 
  <center><object width="675" height="517"><param value="<?php echo '$randomImage' ?>" name="movie"><embed width="475" height="317" type="application/x-shockwave-flash" src="<?php echo '$randomImage' ?>"></object></center><center></center>
  </div>
  </body>
  </html>

Upvotes: 0

Views: 66

Answers (3)

Austin Brunkhorst
Austin Brunkhorst

Reputation: 21130

Remove the quotes.

<?php echo $randomImage; ?>

PHP interprets that as a string rather than the variable $randomImage. You can use variables in quotes but they have to be double quotes like so.

echo "$randomImage";

There is no reason to do this however because you can just echo the variable. This is useful in situations when you want to format a string with multiple variables without concatenation operations.

echo "Hello, my name is $myName!, I am a $species.";

Which is more simple and readable than the alternative.

echo "Hello, my name is ", $myName ,"!, I am a ", $species ,".";

Upvotes: 2

Class
Class

Reputation: 3160

To echo properly you should remove the quotes '$randomImage' -> $randomImage or you can wrap them in "" (double quotes) "$randomImage"

Upvotes: 0

John Conde
John Conde

Reputation: 219934

You either:

  1. Need to use double quotes
  2. Can get rid of the qutes altogether

By placing your variable in single quotes you are telling PHP that your dollar sign is to be taken literally and not used to indicate that there is variable.

So use

<?php echo $randomImage ?>

Or

<?php echo "$randomImage" ?>

Upvotes: 0

Related Questions