处理页面时变量不会被重写
我有一个 php 脚本来检查特定文件是否存在。该文件名由“compartment”变量定义。当脚本被复制并再次粘贴到单独的块中时,仅更改隔间变量就会遇到问题...
例如,1.jpeg 存在,但 2.jpeg 不存在。第一个块显示该文件的链接,但第二个块也显示了上传表单,因为 2.jpeg 不存在。
就好像 $currentfile 或 $filename 变量被转移到它们下面的块中。
请在下面找到我的问题的示例...
<?php
$compartment = "1";
foreach (glob("$compartment.*") as $filename) {
$currentfile = "$filename";
}
if (file_exists($currentfile)) {
echo "
/* If the file exists, it will display a link to the file. */
<a href='$currentfile' target='_blank'>LAUNCH PREVIEW</a>
";
} else {
echo "
/* Here is an uploader form that would transform foobar.jpeg into $compartment.jpeg. */
";
}
?>
<?php
$compartment = "2";
foreach (glob("$compartment.*") as $filename) {
$currentfile = "$filename";
}
if (file_exists($currentfile)) {
echo "
/* If the file exists, it will display a link to the file. */
<a href='$currentfile' target='_blank'>LAUNCH PREVIEW</a>
";
} else {
echo "
/* Here is an uploader form that would transform foobar.jpeg into $compartment.jpeg. */
";
}
?>
谢谢。
I have a php script that checks to see if a particular file exists. This name of the file is defined by the 'compartment' variable. When the script is copied and pasted again into a separate block, changing only the compartment variable it runs into a problem...
Say for example 1.jpeg exists but 2.jpeg doesn't. The first block displays a link to this file, but so does the second block when it should be displaying the upload form as 2.jpeg doesn't exist.
It's as though the $currentfile or $filename variables are being carried over into the blocks below them.
Please find an example of my problem below...
<?php
$compartment = "1";
foreach (glob("$compartment.*") as $filename) {
$currentfile = "$filename";
}
if (file_exists($currentfile)) {
echo "
/* If the file exists, it will display a link to the file. */
<a href='$currentfile' target='_blank'>LAUNCH PREVIEW</a>
";
} else {
echo "
/* Here is an uploader form that would transform foobar.jpeg into $compartment.jpeg. */
";
}
?>
<?php
$compartment = "2";
foreach (glob("$compartment.*") as $filename) {
$currentfile = "$filename";
}
if (file_exists($currentfile)) {
echo "
/* If the file exists, it will display a link to the file. */
<a href='$currentfile' target='_blank'>LAUNCH PREVIEW</a>
";
} else {
echo "
/* Here is an uploader form that would transform foobar.jpeg into $compartment.jpeg. */
";
}
?>
Thank You.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您提供非数组变量,
foreach
将无法执行(并且应该对您大喊大叫)。因此,由于 2.jpeg 不存在,glob() 将返回 NULL,使得 foreach 不执行。但是,您在永远不会执行的
foreach
中分配$currentfile
,因此$currentfile
将保留其旧值“1.jpeg”。这可能看起来以相反的方式工作(当
$compartment = 1
时)的原因是因为$currentfile
在第一次使用时用垃圾初始化,位于if (file_exists($currentfile))
。这当然评估为 false,因此执行跳转到 else 部分。华泰
foreach
will fail to execute (and should yell at you) if you provide a non-array variable.Therefore since 2.jpeg doesn't exist, glob() will return NULL making foreach not execute. However, you are assigning
$currentfile
within aforeach
that never executes so$currentfile
will keep its old value "1.jpeg".The reason this might appear to work the other way around (when
$compartment = 1
) is because$currentfile
is initialized with garbage on first use which is inif(file_exists($currentfile))
. This of course evaluates to false so execution jumps to the else part.HTH
将整个 if/else 块放入 foreach 中,并将 file_exists($currentfile) 替换为 file_exists($filename);
place whole if/else block inside foreach and replace file_exists($currentfile) with file_exists($filename);
.php 文件中的单独部分是同一命名空间/块/执行的一部分。如果您在第一部分中使用变量,它仍然会被定义,并且在第二部分中仍然具有相同的值。
之间没有区别
和
Seperate sections in a .php file are part of the same namespace / block / execution. If you use a variable in your first section, it will still be defined and and still have the same value in your second section.
There is no difference between
and