09c07-4.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 62 行

PHP
62
字号
<?php// This is a library, that by including it, automatically determines// the proper language to use and includes the language file.// First define an array of all possible languages:$languages = array('en' => 'English', 'fr' => 'French', 'de' => 'German');// Look at the GET string to see if lang is specified:if (isset($_GET['lang'])) {    // It's been specified, so set the language    $lang = $_GET['lang'];    // While here, send a cookie to remember this selection for 1 year.    setcookie('lang', $lang, time()+(3600*24*365));}// Ok, otherwise look for the cookie itself:elseif (isset($_COOKIE['lang'])) {    // Use this    $lang = $_COOKIE['lang'];} else {    // Otherwise, default to English    $lang = 'en';}// Make sure that the language string we have is a valid one:if (!(in_array($lang, array_keys($languages)))) {    die("ERROR: Bad Language String Provided!");}// Now include the appropriate language file:// LIVE Version://require_once "{$lang}.php"// TESTING Version:$langfiles = array('en' => '09c07-1', 'fr' => '09c07-2', 'de' => '09c07-3');require_once "{$langfiles[$lang]}.php";// As one last step, create a function that can be used to output// language options to the user:function switch_language_options() {    // Include a few globals that we will need:    global $text, $languages, $lang;        // Start our string with a language specific 'switch' statement:    $retval = $text['switch'];    // Loop through all possible languages to create our options.    $get = $_GET;    foreach ($languages as $abbrv => $name) {        // Create the link, ignoring the current one.        if ($abbrv !== $lang) {            // Recreate the GET string with this language.            $get['lang'] = $abbrv;            $url = $_SERVER['PHP_SELF'] . '?' . http_build_query($get);            $retval .= " <a href=\"{$url}\">{$name}</a>";        }    }    // Now return this string.    return $retval;    }?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?