★ wanayoo — archive 1999 http://www.php.net/manual/ja/function.eval.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to その他
Quick Reference
English version of this pageGerman version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
その他
* connection_aborted
* connection_status
* connection_timeout
* define
* defined
* die
* eval
* exit
* func_get_arg
* func_get_args
* func_num_args
* function_exists
* get_browser
* ignore_user_abort
* iptcparse
* leak
* pack
* register_shutdown_function
* serialize
* sleep
* uniqid
* unpack
* unserialize
* usleep
* highlight_string
* highlight_file
* show_source
Manual: eval
View the source code for this pageSearch the site



Previous page
 die
 Updated
Mon, 14 Aug 2000
exit 
Next page


eval

(unknown)

eval -- 文字列を PHP コードとして評価する

説明

void eval (string code_str)

eval() は、code_str で与えられた文字列を PHP コードとして評価します。 中でも、データベースのテキストフィールドにコードを保存し、 後で実行するためには便利です。

eval() を使用する際、注意するべき点が いくつかあります。 パーサーが eval()の処理中に落ちないように、 渡す文字列はセミコロンで文が終了するといった有効なPHPコード である必要があります。また、code_str の 中の文字を適切にエスケープする必要があります。

eval()の中で値を与えた変数は、 この後、メインスクリプトの中でもこれらの値を維持することも 覚えておいて下さい。

例 1. eval()の例 - 簡単なテキストのマージ


<?php
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.<br>';
echo $str;
eval( "\$str = \"$str\";" );
echo $str;
?>
      

上の例は、以下を表示します。

This is a $string with my $name in it.
This is a cup with my coffee in it.
     


User Contributed Notes: eval


qfinger@mlink.net
10-May-1999 11:06
To include HTML in the string passed to eval, precede it with a PHP closing tag. Once you have finished your HTML block, insert a PHP opening tag.

eg.
<PRE>
$str = "?><H1><I>Hello, World!</I></H1>&lt;?"
eval( $str );
</PRE>
It will produce:
<PRE>
<H1><I>Hello, World!</I></H1>
</PRE>



noor@comrax.com
19-Nov-1999 07:34
Regarding eval()'ing a string that contains PHP and HTML code, this can be done easily using the following technique:

&lt;?

$value = 5;
$string = '&lt;?php echo "Hello, world. value = $value"; ?>';

eval( "?>$string" );

?>

Regarding the note mentioned before, the trailing '&lt;?' is not necessary and actually will write '&lt;?' in the evaluated HTML code.

The above code will produce:

Hello, world. value = 5

Have fun!
Noor Dawod, CTO
http://www.comrax.com/



sidi@bash.nl
28-Feb-2000 01:12
Eval has no return value. It can not be used as a function, only as a procedure.



cornholi@x-treme.gr
20-Mar-2000 06:42
ANSWER TO: Rob.Buttrose@flinders.edu.au
Try this , Rob! Just Found it out!
<PRE>
for($i = 1; $i <11; $i++)
eval("\$next = \$item".$i.";");
</PRE>



cleong@organic.com
23-Mar-2000 07:02
For people doing cross version PHP development, use eval() to dynamically define functions that exists in PHP4 but not PHP3. For example:

<PRE>
if(floor(phpversion()) == 3)
{
eval('

function in_array($needle, $haystack)
{
reset($haystack);
while(list($key, $val) = each($haystack))
{
if($val == $needle)
{
return true;
}
}
return false;
}

');
}
</PRE>

You can just add the eval(' ... ') without reformatting your code. Need to replace any \ with \\ however.



andy@harmonyworks.com
17-Apr-2000 05:34
Be very careful using eval with data which comes from the browser! It can create a major security hole in your PHP script.


cornholi@x-treme.gr
19-Jul-2000 02:23
//let's say you have 5 variables

$num_of_vars=5;
for ($i=0; $i < $num_of_vars; $i++)
{
//suppose item1, item2 ... are the variables
eval("\$item_array[]=\$item".($i+1).";");
//item_array has their values
echo $item_array[$i]
}

That was it. what do you think?



 About Notes


Previous page
 die
 Updated
Mon, 14 Aug 2000
exit 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.