is_dir 和 if else 输出语法错误,意外的 T_ELSEIF

发布于 2024-09-29 00:46:44 字数 905 浏览 1 评论 0原文

我在使用 ifelseis_dir 时遇到一些问题;我正在尝试创建一个小脚本,告诉我输入是文件夹还是文件,我查看过: https://www.php.net/manual/en/function.is-dir.php 举几个例子,但它们似乎都不像我的,我也阅读了一点 if 和 else ,看起来我做得对,所以我认为我没有按照它应该使用的方式使用 is_dir 。有人可以解释一下吗?

我得到的确切错误是:

Parse error: syntax error, unexpected T_ELSE in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\dev\test\php\test.php on line 8

尝试 1:

<?php

$dir = "/some/random/path/on/the/server";

if (is_dir($dir));
 {
  echo "Works!";
 } elseif(!is_dir($dir)); {
  echo "Not good!";
 } 
?>

尝试 2:

<?php

$dir = "/some/random/path/on/the/server";

if (! is_dir($dir));
 {
  echo "Error\n";
 } else {
  echo "Proceed";
 }
 
?>

谢谢您的帮助!

I am having some trouble with an if, else and is_dir; I am trying to create a small script that tells me if the input is a folder or a file, I have looked at: https://www.php.net/manual/en/function.is-dir.php for a few examples, and none of them seemed to resemble mine, I read a bit on if and else as well, and it looks like I am doing that right so I think I am not using is_dir the way it is meant to be used. Can someone shed some light on this?

The exact error im getting is:

Parse error: syntax error, unexpected T_ELSE in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\dev\test\php\test.php on line 8

Attempt 1:

<?php

$dir = "/some/random/path/on/the/server";

if (is_dir($dir));
 {
  echo "Works!";
 } elseif(!is_dir($dir)); {
  echo "Not good!";
 } 
?>

Attempt 2:

<?php

$dir = "/some/random/path/on/the/server";

if (! is_dir($dir));
 {
  echo "Error\n";
 } else {
  echo "Proceed";
 }
 
?>

Thank you for your help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事未完 2024-10-06 00:46:44

删除 ifelseif 语句后的分号,就可以开始了。

Remove semicolon after if and elseif statements and you are good to go.

江挽川 2024-10-06 00:46:44

您正在关闭您的条件; - 那是你的问题。
你必须这样做:

<?php

$dir = "/some/random/path/on/the/server";

if (is_dir($dir))
{
  echo "Works!";
} elseif( !is_dir($dir) ) 
{
    echo "Not good!";
} 
?>

You are closing your conditions with ; - thats your problem.
You will have to do this:

<?php

$dir = "/some/random/path/on/the/server";

if (is_dir($dir))
{
  echo "Works!";
} elseif( !is_dir($dir) ) 
{
    echo "Not good!";
} 
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文