Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何移动<;标题>;内部<;头>;?_Php_Html - Fatal编程技术网

Php 如何移动<;标题>;内部<;头>;?

Php 如何移动<;标题>;内部<;头>;?,php,html,Php,Html,在我的网站中,所有页面的标题都是相同的。因此,我对标题使用include函数。这包括所有的 因为根据页面的不同而不同,所以我将它放在后面 我知道这是错误的。如何解决此问题并将变量移动到标准中 谢谢在包含标题之前设置标题变量: $title = 'Whatever you want'; include('header.php'); //rest of your page 在header.php内部: <head> <title><?php echo $tit

在我的网站中,所有页面的标题都是相同的。因此,我对标题使用
include
函数。这包括所有的

因为
根据页面的不同而不同,所以我将它放在
后面

我知道这是错误的。如何解决此问题并将变量
移动到标准


谢谢

在包含标题之前设置标题变量:

$title = 'Whatever you want';
include('header.php');
//rest of your page
header.php内部

<head>
    <title><?php echo $title ?></title>
    <!-- the rest of your stuff -->
</head>

可能在include之前的变量中设置页面标题,并在标题中添加:

<title><?php echo htmlspecialchars($pageTitle, ENT_QUOTES); ?></title>

在标题包含的代码中,使用占位符作为标题:

<head>
<title><?php echo $pageTitle; ?></title>
...
</head>
<> >为了使您所包含的代码更加健壮,如果没有提供默认标题,请考虑:

<title><?php echo isset($pageTitle)? $pageTitle: 'Default Title'; ?></title>

在标题的头文件中添加一个变量,然后在包含标题之前在页面中定义该值:

您的产品包括:

<html>
<head>
<title><?= $page_title; ?></title>

在主文件中:

<?php
$page_title = 'My page title';
include('header.php');

您可以在头include上方添加一个PHP变量,例如

$title = 'test page';
然后在标题中使用

<title>Main site title | <?php isset($title) ? $title : 'No title set' ?>
Main site title |

使包含的文件只包含
标记的内容,而不包含标记本身


通过这种方式,您可以在
标记之间包含文件,并且每个页面的标题也不同。

根据您的设置方式,您可能需要在调用include之前设置标题

PHP:

<?php
$title = "My Page Title";
include('path-to-your-header.php);
<html>
<head>
<title="<?php echo($title); ?>">
...
</head>
<?php
// Controller
public function index()
{
    $data['page_title'] = 'Your Page Title';
    $data['main_content'] = 'home'; // page content
    $this->load->view( 'templates/public', $data ); // page template
}

// Tempalte
<html>
    <head>
        <title><?php echo ($page_title !== '' ) ? $page_title . ' | ' . SITE_NAME : '' . SITE_NAME; ?></title>
...
</head>
<body>
    <?php echo $this->load->view( 'header' ); ?>
    <?php echo $this->load->view( $main_content ); ?>
    <?php echo $this->load->view( 'footer' ); ?>
</body>
page.php中的

<?php    
$title = 'This title';
include('header.php');
?>

在header.php中

<html>
<head>
<title><?php echo isset($title) ? $title : '' ?></title>
</head>
<!-- rest of code goes below here -->

<html>
<head>
<title><?php echo isset($title) ? $title : '' ?></title>
</head>
<!-- rest of code goes below here -->