doutangguali32556 2015-05-13 07:57
浏览 20
已采纳

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

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.

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!

</div>
  • 写回答

3条回答 默认 最新

  • duanqian2368 2015-05-13 07:59
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)