简体   繁体   English

从文件内容中获取变量

[英]Getting variables from content of file

I have php language files and i'm including to my project.我有 php 语言文件,我包含在我的项目中。

For example: content of en.php例如: en.php的内容

<?php
$lng["home"]="Home Page";
$lng["my_account"]="My Account";
$lng["administrator"]="Administrator";
$lng["logout"]="Logout";

I want to change the file content in administration page.我想更改管理页面中的文件内容。

Since the file is included, $lng variable already defined.由于包含文件,$lng 变量已经定义。 So when I include the file again it overwrites the existing $lng variable.因此,当我再次包含该文件时,它会覆盖现有的 $lng 变量。

I did this by changing the key of the old variable.我通过更改旧变量的键来做到这一点。 Like this:像这样:

$old_lng = $lng; 
include ("file"); 
$last_included_lng = $lng; 
$lng = $last_included_lng;

is that best way?那是最好的方法吗?

Include the file.包括文件。

include 'en.php';

Then all of your $lng items will be overwritten, it essentially runs the content of en.php as if it were written in the same file.然后你所有的$lng项目将被覆盖,它基本上运行en.php的内容,就好像它写在同一个文件中一样。

A way to be independent from the variable name is to return the array directly.一种独立于变量名的方法是直接返回数组。

That will look like:这看起来像:

<?php
return [
    'home'       => 'Home Page',
    'my_account' => 'My Account',
    // and so on
];

You can assogn this by using $foo = include('en.php');您可以使用$foo = include('en.php'); . .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM