PHP: Эквивалентный из включают оценку использования

Основная часть вставляет разделители тысяч, что можно сделать так:

<script type="text/javascript">
function ins1000Sep(val){
  val = val.split(".");
  val[0] = val[0].split("").reverse().join("");
  val[0] = val[0].replace(/(\d{3})/g,"$1,");
  val[0] = val[0].split("").reverse().join("");
  val[0] = val[0].indexOf(",")==0?val[0].substring(1):val[0];
  return val.join(".");
}
function rem1000Sep(val){
  return val.replace(/,/g,"");
}
function formatNum(val){
  val = Math.round(val*100)/100;
  val = (""+val).indexOf(".")>-1 ? val + "00" : val + ".00";
  var dec = val.indexOf(".");
  return dec == val.length-3 || dec == 0 ? val : val.substring(0,dec+3);
}
</script>

<button onclick="alert(ins1000Sep(formatNum(12313231)));">
14
задан Lajos Veres 15 December 2014 в 21:22
поделиться

4 ответа

After some more research I found out what was wrong myself. The problem is in the fact that is a "short opening tag" and so will only work if short_open_tag is set to 1 (in php.ini or something to the same effect). The correct full tag is , which has a space after the second p.

As such the proper equivalent of the include is:

eval('?>' . file_get_contents('external.php') . '<?php ');

Alternatively, you can leave the opening tag out all together (as noted in the comments below):

eval('?>' . file_get_contents('external.php'));

My original solution was to add a semicolon, which also works, but looks a lot less clean if you ask me:

eval('?>' . file_get_contents('external.php') . '<?php;');
13
ответ дан 1 December 2019 в 09:33
поделиться

AFAIK you can't take advantage of php accelerators if you use eval().

6
ответ дан 1 December 2019 в 09:33
поделиться

If you are using a webserver on which you have installed an opcode cache, like APC, eval will not be the "best solution" : eval'd code is not store in the opcode cache, if I remember correctly (and another answer said the same thing, btw).

A solution you could use, at least if the code is not often changed, is get a mix of code stored in database and included code :

  • when necessary, fetch the code from DB, and store it in a file on disk
  • include that file
  • as the code is now in a file, on disk, opcode cache will be able to cache it -- which is better for performances
  • and you will not need to make a request to the DB each time you have to execute the code.

I've worked with software that uses this solution (the on-disk file being no more than a cache of the code stored in DB), and I worked not too bad -- way better that doing loads of DB requests of each page, anyway...

Some not so good things, as a consequence :

  • you have to fetch the code from the DB to put it in the file "when necessary"
    • это может означать повторное создание временного файла один раз в час или его удаление при изменении записи в БД? Есть ли у вас способ определить, когда это произойдет?
  • вам также необходимо изменить свой код, чтобы использовать временный файл, или при необходимости заново сгенерировать его
    • if you have several places to modifiy, this could mean some work

BTW : would I dare saying something like "eval is evil" ?

6
ответ дан 1 December 2019 в 09:33
поделиться

Это позволяет вам включать файл, предполагая, что файловые оболочки для включаемых файлов включены в PHP:

function stringToTempFileName($str)
{
    if (version_compare(PHP_VERSION, '5.1.0', '>=') && strlen($str < (1024 * 512))) {
        $file = 'data://text/plain;base64,' . base64_encode($str);
    } else {
        $file = Utils::tempFileName();
        file_put_contents($file, $str);
    }
    return $file;
}

... Затем включить этот «файл». Да, это также отключит кеши опкодов, но делает этот eval таким же, как включение в отношении поведения.

0
ответ дан 1 December 2019 в 09:33
поделиться
Другие вопросы по тегам:

Похожие вопросы: