简体   繁体   English

将php代码包含到php文件中

[英]Include php code into php file

I'm not a programmer so I need help with something I guess could be easy to any php programmer. 我不是程序员,所以我需要一些我想对任何PHP程序员来说都很容易的帮助。 I would like to include a php code that I have, into a PHP file. 我想将自己拥有的php代码包含到一个PHP文件中。 Code is in a div: 代码在div中:

<div style="position: fixed;
 background: rgb(226, 30, 30);bottom:0;"><?php
 $min=819;
 $max=852;
 echo rand($min,$max);
?></div>

and the file that I'm trying to insert this starts obviously with 而我要插入的文件显然以

<?php ....

How can I insert that div with the php code correctly without getting any errors? 我如何才能正确插入带有php代码的div而不会出现任何错误?

Thanks in advance. 提前致谢。

You can do this by using the following statement: 您可以使用以下语句来做到这一点:

<?php include "filename.php" ?>

put this line where you want to put your php file with correct filename(if they are not in same path then you have to define the path also. 将这一行放在您要使用正确文件名的php文件中(如果它们不在同一路径中,则还必须定义路径。

Here is what i assume you want to do: 1. You want a .php file. 这是我假设您要执行的操作:1.您需要一个.php文件。 2. the php. 2. PHP。 file must display(echo) the div aswell as the php code. 文件必须显示(回显)div以及php代码。

Here is how i would do it: 这是我要怎么做:

<?php
$min=819;
$max=852;
$random = rand($min,$max);

echo ('<div style="position: fixed;
background: rgb(226, 30, 30);bottom:0;">');
echo ($random);
echo ('</div>');

To make the code easier to read i start off with declaring variables as well as the rand. 为了使代码更易于阅读,我从声明变量和rand开始。 This way i can just call the variable in the echo. 这样我就可以在回显中调用变量。 i use the ' mark up since you use " in your html code. this way it will ignore the " and echo it as html. 我使用'标记,因为您在html代码中使用了“。这样,它将忽略”并将其作为html回显。

Hope this helps 希望这可以帮助

How could I insert it directly into the php file? 如何将其直接插入php文件? I mean being part of the file and without including an external php file? 我的意思是成为文件的一部分,并且不包含外部php文件? – Robertoss –罗伯托斯

If you are trying to include the code directly into lets say file.php , without requiring another tobeincluded.php file, then simply copy and paste the code into file.php . 如果您试图将代码直接包含到假设的file.php中 ,而不需要另一个tobeincluded.php文件,则只需将代码复制并粘贴到file.php中

Since you mentioned that file.php starts with <?php , make sure you close it off with ?> before you insert the code. 由于您提到file.php<?php开头,因此在插入代码之前,请确保使用?>将其关闭。 Then, open <?php again after the code you inserted. 然后,在插入代码后再次打开<?php

For example: 例如:

<?php
// Original code here...

// Insert this:
?>

<!-- insert your code here -->
<div style="position: fixed;
background: rgb(226, 30, 30);bottom:0;">
<?php
$min=819;
$max=852;
echo rand($min,$max);
?>
</div>
<?php
// ^ Add this back in

You have to include de .php file: 您必须包含de .php文件:

vars.php
<?php

$color = 'verde';
$fruta = 'manzana';

?>

test.php
<?php

echo "Una $fruta $color"; // Una

include 'vars.php';

echo "Una $fruta $color"; // Una manzana verde

?>

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM