-1

I have a echo call outputting some HTML code, some of this code include a css, in this css i'm trying to execute some php code.

variable set before echo

  $bg = array('bg-01.jpg', 'bg-02.jpg', 'bg-03.jpg', 'bg-04.jpg', 'bg-05.jpg', 'bg-06.jpg', 'bg-07.jpg' );
$i = rand(0, count($bg)-1);
$selectedBg = "$bg[$i]";

Then in the echo:

    echo'
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
    <link type="image/x-icon" href="test.ico" rel="shortcut icon"  />
<style>
html {background:#000;
background: url("<?php echo $selectedBg; ?>") no-repeat fixed 0% 0% / cover #000;}

i've tried several of ways to run the php inside the echo but i fail, if i put the php inside '' tags page will not load, what am i doing wrong here or is it impossible to echo new php code inside a php echo?

Thank you!

karnehe
  • 325
  • 2
  • 12

3 Answers3

3

You should use string concatenation:

echo'
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
    <link type="image/x-icon" href="test.ico" rel="shortcut icon"  />
<style>
html {background:#000;
background: url("'.$selectedBg.'") no-repeat fixed 0% 0% / cover #000;}';
Jazi
  • 6,569
  • 13
  • 60
  • 92
3

So there is actually a misunderstanding :)

Since echo is a php instruction, you do not have to re-open and re-close php tags () but you can simply use concatenation. Example :

echo "Hello there ". $name ." ! How are u ?";

Opening and closing php tags is only usefull when you're in a HTML display such as :

<span>Hello <?php echo $name; ?> ! How are u ? </span>

This is really some basic stuff and you should really get used to it :)

Cr3aHal0
  • 809
  • 4
  • 11
0
 echo'
<html>
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
<META NAME="ROBOTS" CONTENT="NOINDEX, NOFOLLOW">
    <link type="image/x-icon" href="test.ico" rel="shortcut icon"  />
<style>
html {background:#000;
background: url("'.$selectedBg.'") no-repeat fixed 0% 0% / cover #000;}
Aman Rawat
  • 2,625
  • 1
  • 25
  • 40