PHP - 将多个变量组合成1然后回显它们

时间:2014-04-10 19:22:29

标签: php html variables

所以我会用代码解释我的意思:

$sub_content_1 = "<h2>View files</h2><br><ul><li><p>";

$var = rand(1,2,3,4,5,6,7,8,9,10);
if ($var == 1){
print '<a href="/dir1/file1">link1</a>';
}
if ($var == 2){
print '<a href="/dir2/file2">link2</a>';
}
if ($var == 3){
print '<a href="/dir3/file3">link3</a>';
}
if ($var == 4){
print '<a href="/dir4/file4">link4</a>';
}
if ($var == 5){
print '<a href="/dir5/file5">link5</a>';
}
if ($var == 6){
print '<a href="/dir6/file6">link6</a>';
}
if ($var == 7){
print '<a href="/dir7/file7">link7</a>';
}
if ($var == 8){
print '<a href="/dir8/file8">link8</a>';
}
if ($var == 9){
print '<a href="/dir9/file9">link9</a>';
}
if ($var == 10){
print '<a href="/dir10/file10">link10</a>';
}

$sub_content_2 = "</p></li></ul><br><button type=\"button\" onclick=\"window.location.href='/files';\">View All</button>";

现在我需要统一3个变种:

$content = "sub_content_1 + sub_content_2 + the printed value of $var"

echo "$content";

期望的结果:

[来自$sub_content_1的HTML MARKUP]

[来自$var的HTML MARKUP]

[来自$sub_content_2的HTML MARKUP]

如何实现这一目标?

2 个答案:

答案 0 :(得分:1)

    $sub_content_1 = "<h2>View files</h2><br><ul><li><p>";

    $var = rand(1,10);
    if ($var == 1){
    $var = '<a href="/dir1/file1">link1</a>';
    print '<a href="/dir1/file1">link1</a>';
    }
    if ($var == 2){
$var = '<a href="/dir2/file2">link2</a>';
    print '<a href="/dir2/file2">link2</a>';
    }
    if ($var == 3){
$var = '<a href="/dir3/file3">link3</a>';
    print '<a href="/dir3/file3">link3</a>';
    }
    if ($var == 4){
    $var = '<a href="/dir4/file4">link4</a>';
    print '<a href="/dir4/file4">link4</a>';
    }
    if ($var == 5){
$var = '<a href="/dir5/file5">link5</a>';
    print '<a href="/dir5/file5">link5</a>';
    }
    if ($var == 6){
    $var = '<a href="/dir6/file6">link6</a>';
    print '<a href="/dir6/file6">link6</a>';
    }
    if ($var == 7){
    $var = '<a href="/dir7/file7">link7</a>';
    print '<a href="/dir7/file7">link7</a>';
    }
    if ($var == 8){
    $var = '<a href="/dir8/file8">link8</a>';
    print '<a href="/dir8/file8">link8</a>';
    }
    if ($var == 9){
$var =  '<a href="/dir9/file9">link9</a>';
    print '<a href="/dir9/file9">link9</a>';
    }
    if ($var == 10){
    $var =  '<a href="/dir10/file10">link10</a>';
    print '<a href="/dir10/file10">link10</a>';
    }

    $sub_content_2 = "</p></li></ul><br><button type=\"button\" onclick=\"window.location.href='/files';\">View All</button>";

    $content = $sub_content_1 . $sub_content_2 . $var;
    echo $content;

点'。'是在PHP中组合变量。在javascript中它是'+'

答案 1 :(得分:0)

php函数rand()接受2个参数。如果你想生成1-10之间的数字,那么应该像这个rand(1,10)

一样调用该函数
<html>
<head>
<title>Online PHP Script Execution</title>
</head>
<body>
<?php
   $sub_content_1 = "<h2>View files</h2><br><ul><li><p>";

$var = rand(1,10);
if ($var == 1){
print '<a href="/dir1/file1">link1</a>';
}
if ($var == 2){
print '<a href="/dir2/file2">link2</a>';
}
if ($var == 3){
print '<a href="/dir3/file3">link3</a>';
}
if ($var == 4){
print '<a href="/dir4/file4">link4</a>';
}
if ($var == 5){
print '<a href="/dir5/file5">link5</a>';
}
if ($var == 6){
print '<a href="/dir6/file6">link6</a>';
}
if ($var == 7){
print '<a href="/dir7/file7">link7</a>';
}
if ($var == 8){
print '<a href="/dir8/file8">link8</a>';
}
if ($var == 9){
print '<a href="/dir9/file9">link9</a>';
}
if ($var == 10){
print '<a href="/dir10/file10">link10</a>';
}
?>
</body>
</html>