定义常量PHP


Define constant PHP

我有一个多语言脚本,它的工作原理如下:

en.php

define('lang_hello_world', 'Hello World!');

index.php

<?php include('en.php'); echo lang_hello_world; ?>

脚本运行良好,并替换了语言。但现在我需要转换存储在MySQL上的常量。

lang_hello_world在MySQL中,我需要在页面上打印转换后的内容,我该怎么做?

在en.php中,您需要连接数据库并定义值,如下所示:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if (mysqli_connect_errno()) {
    printf("Connect failed: %s'n", mysqli_connect_error());
    exit();
}
$result = mysqli->query("SELECT * FROM language_table WHERE language = 'EN' LIMIT 1");
$row = $result->fetch_assoc();
define('lang_hello_world', $row[0]['lang_hello_world']);

希望能有所帮助!

使用constant()。如果从数据库中提取的$row['var']等于lang_hello_world,则:

$val = constant($row['var']);

现在,如果加载了英文文件,则$val等于Hello World!。或者很明显:

echo constant($row['var']);

在将其放入常量之前,先从数据库中获取它?

<?php
    mysql_connect("localhost", "root", "passw");
    mysql_select_db("languages");
    $query = mysql_query("SELECT lang_hello_world FROM languages");
    $fetch = mysql_fetch_assoc($query);
    define("lang_hello_world", $fetch['lang_hello_world']);
    mysql_close();
?>