开发者

Is it bad practice to have PHP conditional statements directly called from the index file (mixed with HTML markup)?

Some people have told me that the following code is bad for HTML validation:

index.php:

line 1 ~ 5 (this one is OK. Just for reference):

<?php include_once 'localization.php'; ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1开发者_高级运维/DTD/xhtml1-strict.dtd">
<html>
<head>

line 15 (if Chinese is selected load those CSS files):

<?php if($lang_file=='lang.zh-tw.php' || $lang_file=='lang.zh-cn.php') 
{echo '<link rel="stylesheet" type="text/css" href="styles/chinese.css" />';} ?>

line 21 (if English is the current language change assign to it the class: current):

<li <?php if($lang_file=='lang.en.php') {echo 'class="current"';} ?>>
<a href="index.php?lang=en">ENGLISH</a></li>

line 168 ~ 171 (this is the only way I found to add different languages to the jquery validation plugin):

<?php if($lang_file=='lang.en.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-en.js"></script>';} ?>
<?php if($lang_file=='lang.es.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-es.js"></script>';} ?>
<?php if($lang_file=='lang.zh-tw.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-zh-tw.js"></script>';} ?>
<?php if($lang_file=='lang.zh-cn.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-zh-cn.js"></script>';} ?>

lozalization.php (for reference only):

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang'])) {
    $lang = $_GET['lang'];

    // register the session and set the cookie
    $_SESSION['lang'] = $lang;
    setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang'])) {
    $lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang'])) {
    $lang = $_COOKIE['lang'];
}
else {
    $lang = 'en';
}

// use appropiate lang.xx.php file according to the value of the $lang
$languages = array('en', 'es', 'zh-tw', 'zh-cn');
if (in_array($_SESSION['lang'], $languages)) {
    $lang_file = 'lang.'.$_SESSION['lang'].'.php';
} else {
    $lang_file = 'lang.en.php';
}

//localization helper function
function l($localization) {
    global $lang;
    return $lang[$localization];
}
    include_once 'languages/'.$lang_file;
?>

I am falling into bad practices or validation problems? The code works, but is there's a way of doing the things above in a cleaner and better way?


PHP statements in your index file are not bad for HTML validation, assuming they generate valid HTML. If you check the source of the page in a web-browser, you'll see that all your PHP tags are gone and replaced with whatever they were there to produce. This is what gets validated.

With regards to making things cleaner, you might try replacing

<?php if($lang_file=='lang.en.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-en.js"></script>';} ?>
<?php if($lang_file=='lang.es.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-es.js"></script>';} ?>
<?php if($lang_file=='lang.zh-tw.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-zh-tw.js"></script>';} ?>
<?php if($lang_file=='lang.zh-cn.php') {echo '<script type="text/javascript" src="scripts/jquery-validate/val-zh-cn.js"></script>';} ?>

with something like

<?php
$lang = explode('.',$lang_file);
echo '<script type="text/javascript" src="scripts/jquery-validate/val-'.$lang[1].'.js"></script>' ?>

That way, assuming the variable translation between $lang_file and the validation script are always along the lines of lang.en.php to val-en.js, this will support any languages you care to throw at it without requiring a line of code each. Obviously if $lang_file can be manipulated by the user you'll want to make sure it contains what you expect it to contain.


I can't speak specifically for bad practices, but in terms of loading the correct jquery script, just do a substring match on the contents of $lang_file, then concatenate it onto the end of the directory holding all of the scripts. That way, you can add/remove locales without having to change your script.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜