Kepp the following Quote in mind:
If eval() is the answer, you're almost certainly asking the
wrong question. -- Rasmus Lerdorf, BDFL of PHP(PHP 4, PHP 5, PHP 7, PHP 8)
eval — Bir dizgeyi bir PHP kodu olarak yorumlar
   kod_dizgesi ile belirtilen dizgeyi bir PHP kodu
   olarak yorumlar.
  
Değerlendirilen kod, eval() çağrısının gerçekleştiği satırın değişken etki alanını devralır. Bu satırda bulunan tüm değişkenler, değerlendirilen kodda okunabilir ve değiştirilebilir. Ancak, tanımlanan tüm işlevler ve sınıflar genel isim alanında tanımlanacaktır. Başka bir deyişle, derleyici değerlendirilen kodu ayrı bir dahil edilen dosyaymış gibi ele alır.
eval() dil yapısı çok tehlikelidir çünkü keyfi PHP kodunun çalıştırılmasına izin verir. Bu nedenle kullanımı önerilmez. Bu yapıyı kullanmaktan başka bir seçeneğin olmadığına dikkatlice karar verilirse, önceden düzgün bir şekilde doğrulamadan kullanıcı tarafından sağlanan herhangi bir veriyi bu işleve aktarmamaya özellikle dikkat edilmelidir.
kod_dizgesiYorumlanacak PHP kodlarını içeren dizge.
       Dizge içindeki bir return deyimi dizgenin
       yorumlanmasını, anında durdurur.
      
       Kod, PHP açılış ve kapanış
       PHP etiketleri ile
       sarmalanmaMAlıdır, yani
       '<?php echo "Merhaba!"; ?>' dizgesi yerine
       'echo "Merhaba!";' dizgesi aktarılmalıdır. Uygun PHP
       etiketlerini kullanarak PHP kipinden çıkmak ve yeniden girmek hala
       mümkündür, örn. 'echo "PHP kipindeyiz!"; ?>HTML kipindeyiz!
       <?php echo "Tekrar PHP kipindeyiz!";'.
      
       Bunun dışında aktarılan kod geçerli PHP kodu olmalıdır. Bu, tüm
       ifadelerin noktalı virgül kullanılarak uygun şekilde sonlandırılması
       gerektiği anlamına gelir. Örneğin 'echo "Merhaba!"'
       bir ayrıştırma hatasına sebep olurken,
       'echo "Merhaba!";' çalışacaktır.
      
       Bir return deyimi kodun yorumlanmasını derhal
       sonlandıracaktır.
      
Kod, eval() işlevini çağıran kodun etki alanında çalıştırılacaktır. Bu nedenle eval() çağrısında tanımlanan veya değiştirilen tüm değişkenler, işlev sonlandırıldıktan sonra görünür kalacaktır.
   Kod dizgesi içinde bir return  bulunmadıkça
   eval() daima null döndürür. return
   ile bir değer döndürülmesi durumunda bu değeri döndürür. Belirtilen dizgede
   bir çözümleme hatası saptanırsa eval() PHP 7 öncesinde
   false döndürüp çalışma dosyadaki sonraki deyimden normal olarak devam
   eder; PHP 7 ve sonrasında ise ParseError istisnası
   oluşur. eval() işlevinin yorumladığı kodlardaki bir
   hatayı set_error_handler() kullanarak döndürmek mümkün
   değildir.
  
Örnek 1 - eval() örneği
<?php
$dizge = 'Gülhane';
$isim = 'çınar';
$metin = 'Burası $dizge parkı ve ben bir $isim ağacıyım.';
echo $metin. "\n";
eval("\$metin = \"$metin\";");
echo $metin. "\n";
?>Yukarıdaki örneğin çıktısı:
Burası $dizge parkı ve ben bir $isim ağacıyım. Burası Gülhane parkı ve ben bir çınar ağacıyım.
Bilginize: Bu bir işlev değil, dil oluşumu olduğundan değişken işlevler veya isimli bağımsız değişkenler kullanılarak çağrılamaz.
Sonuçlarını doğrudan tarayıcıya çıktılayan her şey gibi, çıktı denetleme işlevleri bu işlevin de çıktısını yakalamak ve (örneğin) string türünde saklamak için kullanılabilir.
Bilginize:
Yorumlanan kodda ölümcül bir hata varsa betiğin tamamı durdurulur.
Kepp the following Quote in mind:
If eval() is the answer, you're almost certainly asking the
wrong question. -- Rasmus Lerdorf, BDFL of PHPInception with eval()
<pre>
Inception Start:
<?php
eval("echo 'Inception lvl 1...\n'; eval('echo \"Inception lvl 2...\n\"; eval(\"echo \'Inception lvl 3...\n\'; eval(\'echo \\\"Limbo!\\\";\');\");');");
?>At least in PHP 7.1+, eval() terminates the script if the evaluated code generate a fatal error. For example:
<?php
@eval('$content = (100 - );');
?>
(Even if it is in the man, I'm note sure it acted like this in 5.6, but whatever)
To catch it, I had to do:
<?php
try {
    eval('$content = (100 - );');
} catch (Throwable $t) {
    $content = null;
}
?>
This is the only way I found to catch the error and hide the fact there was one.If you want to allow math input and make sure that the input is proper mathematics and not some hacking code, you can try this:
<?php
$test = '2+3*pi';
// Remove whitespaces
$test = preg_replace('/\s+/', '', $test);
$number = '(?:\d+(?:[,.]\d+)?|pi|π)'; // What is a number
$functions = '(?:sinh?|cosh?|tanh?|abs|acosh?|asinh?|atanh?|exp|log10|deg2rad|rad2deg|sqrt|ceil|floor|round)'; // Allowed PHP functions
$operators = '[+\/*\^%-]'; // Allowed math operators
$regexp = '/^(('.$number.'|'.$functions.'\s*\((?1)+\)|\((?1)+\))(?:'.$operators.'(?2))?)+$/'; // Final regexp, heavily using recursive patterns
if (preg_match($regexp, $q))
{
    $test = preg_replace('!pi|π!', 'pi()', $test); // Replace pi with pi function
    eval('$result = '.$test.';');
}
else
{
    $result = false;
}
?>
I can't guarantee you absolutely that this will block every possible malicious code nor that it will block malformed code, but that's better than the matheval function below which will allow malformed code like '2+2+' which will throw an error.It should be noted that imported namespaces are not available in eval.The following code
<?php
    eval( '?> foo <?php' );
?>
does not throw any error, but prints the opening tag.
Adding a space after the open tag fixes it:
<?php
    eval( '?> foo <?php ' );
?>imo, this is a better eval replacement: 
<?php
function betterEval($code) {
    $tmp = tmpfile ();
    $tmpf = stream_get_meta_data ( $tmp );
    $tmpf = $tmpf ['uri'];
    fwrite ( $tmp, $code );
    $ret = include ($tmpf);
    fclose ( $tmp );
    return $ret;
}
?>
- why? betterEval follows normal php opening and closing tag conventions, there's no need to strip `<?php?>` from the source.  and it always throws a ParseError if there was a parse error, instead of returning false (note: this was fixed for normal eval() in php 7.0). - and there's also something about exception backtracesPls note that 'eval' is not a function but a 'language construct', hence it CANNOT be disablled using 'disable_functions', either in php.ini or in a phpfpm pool etc.