如何包括另一个PHP文件? [英] how to include another php file?

查看:61
本文介绍了如何包括另一个PHP文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php文件,我想包含另一个具有css链接标签和javascript源标签的php文件,但是当我尝试包含它们时,它并没有添加到页面中.

I have a php file, and I want to include another php file that have css link tags and javascript source tags, but when I try to include them, it doesn't get added to the page.

我的php页面:

<?php 
    $root = $_SERVER['SERVER_NAME'] . '/mysite'; 
    $theme = $root . '/includes/php/common.php';
    echo $theme;
    include($theme);
?>

common.php:

common.php:

<link rel='stylesheet' type='text/css' href='../css/main.css'/>";

有人知道怎么了吗?谢谢

Anyone know whats wrong? Thanks

推荐答案

PHP的 include 是服务器端的,因此您需要使用服务器端的路径.最好使用dirname(__FILE__)代替$_SERVER['SSCRIPT_NAME'],但是$_SERVER['SERVER_NAME']绝对错误.

PHP's include is server-side, so you need to use the server side path. It is better to use dirname(__FILE__) instead of $_SERVER['SSCRIPT_NAME'], but $_SERVER['SERVER_NAME'] is absolutely wrong.

尝试:

include dirname(__FILE__)."/common.php";

或者,如果要包含的文件不在同一目录中,请更改路径.例如,对于父目录,请使用dirname(__FILE__)."/../common.php".

Or if the file you want to include is not on the same directory, change the path. For example for a parent directory, use dirname(__FILE__)."/../common.php".

请注意,有些人可能建议使用include "./common.php"或类似名称.此可以起作用,但是当调用include的脚本实际上已被另一个目录中的另一个脚本包含时,很可能会失败.使用dirname(__FILE__)."/common.php"可以消除此问题.

Note that some might suggest using include "./common.php" or similar. This could work, but will most likely fail when the script invoking include is actually being included by another script in another directory. Using dirname(__FILE__)."/common.php" will eliminate this problem.

这篇关于如何包括另一个PHP文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆