MastaBot
MastaBot

Reputation: 277

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

Why this works perfect:

<?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>";
?>

when this doesn't work. Function _loc return empty value. There are no php notice/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>

I can leave it in first (working) form, but then html code is inside php and is difficult to read

Upvotes: 1

Views: 365

Answers (2)

Al Amin Chayan
Al Amin Chayan

Reputation: 2500

In first case you put your _loc function inside echo which is absent in your second example.

change your last line code to:

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

Upvotes: 5

ashishmaurya
ashishmaurya

Reputation: 1196

Put echo before _loc function for printing its output

Upvotes: 0

Related Questions