在php echo中运行php echo? [重复]

时间:2022-08-27 18:13:12

This question already has an answer here:

这个问题在这里已有答案:

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.

我有一个echo调用输出一些HTML代码,其中一些代码包括一个css,在这个css我试图执行一些PHP代码。

variable set before echo

在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?

我已经尝试了几种方法来运行回声内的PHP,但我失败了,如果我把PHP里面的''标签页面将无法加载,我在这里做错了什么或者是不可能在php内回应新的PHP代码回声?

Thank you!

3 个解决方案

#1


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;}';

#2


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是一个php指令,你不必重新打开并重新关闭php标签(),但你可以简单地使用连接。示例:

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

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

打开和关闭php标签只有在HTML显示中才有用,例如:

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

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

这真的是一些基本的东西,你应该真的习惯它:)

#3


 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;}

#1


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;}';

#2


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是一个php指令,你不必重新打开并重新关闭php标签(),但你可以简单地使用连接。示例:

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

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

打开和关闭php标签只有在HTML显示中才有用,例如:

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

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

这真的是一些基本的东西,你应该真的习惯它:)

#3


 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;}