2015-08-16 94 views
1

工作,为什么这个完美的作品:为什么PHP函数不内第二<?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返回空值。有没有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); 
} 
?> 
<!doctype html> 
<html lang="<?php echo $lang; ?>"> 
<head> 
<title><?php _loc("Summoners War"); ?></title> 

我可以将它留在第一(工作)的形式,但随后的html代码里面PHP和难以阅读

回答

5

在第一种情况下,你把你的_loc函数中echo你的第二个例子中不存在。

改变你的最后一行代码:

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

虽然正确_loc功能,这将是很好的补充什么是错的确切的解释。 – jeroen

+1

echo打印_loc()的返回值。而不是使用<?php echo _loc(...); ?>你也可以使用简写形式<?= _loc(..); ?>。 – aimed

+1

你是对的@ jeroen。谢谢。 – Chayan

相关问题