Jump to content

parse error, unexpected T_FUNCTION


mr_badger

Recommended Posts

I have followed a tutorial word for word but I'm getting this parse error, I know it will be something simple but I need help.

 

// 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) {

 

The problem is the function switch_language_options () {

Link to comment
https://forums.phpfreaks.com/topic/45636-parse-error-unexpected-t_function/
Share on other sites

sorry, I will post all the code.

 

<?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:
require_once "{$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;
}
?>

You have an opening "{" on this "if" but no closing "}"

<?php
if (! (in_array($lang, array_keys($languages)))) {
	die("ERROR: Bad Language String Provided!");
?>

Actually, you can remove the opening "{" since there is only one line in the statement block that follows the "if".

 

Ken

There's a missing semi-colon on this line:

<?php
require_once "{$lang}.php"
?>

and there's an extra "}" in this snippet:

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

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.