duanbohan2015 2010-02-21 17:47
浏览 34
已采纳

当我从索引文件中调用Php变量而不是从包含文件中调用它时,它是否被激活?

The code works when I declare $lang like this (at the top of the index file):

index.php (with php variable declaration at the top):

   <?php
        $lang = 'es';
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
        <title>New Project</title>
        <link rel="stylesheet" type="text/css" href="styles/global.css" />
    </head>
    <body id="home">
    <div id="header">
        <div class="container">
            <div id="header-top">
                <h1><a href="http://widerdesign.co.nr/">wider design</a></h1>
                <ul id="lang">
                    <li><a href="index.php?lang=en" <?php if($lang=='en') {echo 'class="current"';} ?>>English</a></li>
                    <li><a href="index.php?lang=es" <?php if($lang=='es') {echo 'class="current"';} ?>>Español</a></li>
                    <li><a href="index.php?lang=tw" <?php if($lang=='tw') {echo 'class="current"';} ?>>中文(繁體)</a></li>
                    <li><a href="index.php?lang=cn" <?php if($lang=='cn') {echo 'class="current"';} ?>>中文(简体)</a></li>
                </ul>

but it doesn't work when its declared in a included file:

index.php (with included file which has the php variable declaration):

<?php
include_once 'common.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
    <title>New Project</title>
    <link rel="stylesheet" type="text/css" href="styles/global.css" />
</head>
<body id="home">
<div id="header">
    <div class="container">
        <div id="header-top">
            <h1><a href="http://widerdesign.co.nr/">wider design</a></h1>
            <ul id="lang">
                <li><a href="index.php?lang=en" <?php if($lang=='en') {echo 'class="current"';} ?>>English</a></li>
                <li><a href="index.php?lang=es" <?php if($lang=='es') {echo 'class="current"';} ?>>Español</a></li>
                <li><a href="index.php?lang=tw" <?php if($lang=='tw') {echo 'class="current"';} ?>>中文(繁體)</a></li>
                <li><a href="index.php?lang=cn" <?php if($lang=='cn') {echo 'class="current"';} ?>>中文(简体)</a></li>
            </ul>

common.php:

session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang'])) {
    $lang = $_GET['lang'];    
    // register the session and set the cookie
    $_SESSION['lang'] = $lang;    
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
} else if(isSet($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
} else if(isSet($_COOKIE['lang'])) {
    $lang = $_COOKIE['lang'];
} else {
    $lang = 'en';
}

switch ($lang) {
  case 'en':
      $lang_file = 'lang.en.php';
      break;    
  case 'es':
      $lang_file = 'lang.es.php';
      break;    
  case 'tw':
      $lang_file = 'lang.tw.php';
      break;    
  case 'cn':
      $lang_file = 'lang.cn.php';
      break;    
  default:
      $lang_file = 'lang.en.php';    
}    
include_once 'languages/'.$lang_file;

Is $lang really having any value when its declared in the common.php file?

edit: the language change in the file is working is just the class .current not being activated, weird.

  • 写回答

3条回答 默认 最新

  • doubiaozhan0745 2010-02-21 18:11
    关注

    The $lang variable is set at the top of your common.php file ; and you are then trying to use it from the content of index.php.

    But, between the top of common.php and the content of index.php, you are including the languages/lang.XX.php file.


    Are you sure there is not something, in that lang.XX.php file, that overrides the content of the $lang variable ?

    Judging from this other question (To which I answered, and this question here seems like a follow-up of that question there -- I didn't really notice that the $lang variable was being overriden), lang.en.php contains this :

    <?php
    $lang = array(
        'h1' => 'Hello World',
    );
    ?>
    

    Which overrides the $lang variable :

    • in common.php it is used to store the language code
    • in lang.en.php is it overriden, set to an array of string
    • which means that, later, in index.php, it cannot be equal to the language code -- and the condition to set the CSS class fails.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值