为什么 php 函数在第二个 <?php 中不起作用?>

Why php function doesn't work inside second <?php ?>

为什么这很完美:

<?php
$url = $_SERVER["REQUEST_URI"];
$locale_lang = "pl_PL";

if (substr($url,0,3) == "/pl") { $locale_lang = "pl_PL"; }
if (substr($url,0,3) == "/en") { $locale_lang = "en_US"; }  

$lang = substr($locale_lang,0,2);

require_once("lib/streams.php");
require_once("lib/gettext.php");

$locale_file = new FileReader("locale/$locale_lang/LC_MESSAGES/messages.mo");
$locale_fetch = new gettext_reader($locale_file);

function _loc($text) {
    global $locale_fetch;
    return $locale_fetch->translate($text);
}

echo "<!doctype html>
<html lang=\"$lang\">
<head>
<title>"._loc("Summoners War")."</title>";
?>

当这不起作用时。函数_loc return 空值。没有phpnotice/error

<?php
$url = $_SERVER["REQUEST_URI"];
$locale_lang = "pl_PL";

if (substr($url,0,3) == "/pl") { $locale_lang = "pl_PL"; }
if (substr($url,0,3) == "/en") { $locale_lang = "en_US"; }  

$lang = substr($locale_lang,0,2);

require_once("lib/streams.php");
require_once("lib/gettext.php");

$locale_file = new FileReader("locale/$locale_lang/LC_MESSAGES/messages.mo");
$locale_fetch = new gettext_reader($locale_file);

function _loc($text) {
    global $locale_fetch;
    return $locale_fetch->translate($text);
}
?>
<!doctype html>
<html lang="<?php echo $lang; ?>">
<head>
<title><?php _loc("Summoners War"); ?></title>

我可以保留第一个(工作)形式,但是 html 代码在 php 里面并且难以阅读

在第一种情况下,您将 _loc 函数放在 echo 中,而第二个示例中没有。

将最后一行代码更改为:

<title><?php echo _loc("Summoners War"); ?></title>

将 echo 放在 _loc 函数之前以打印其输出