php 中的 Switch() 和 foreach() 问题
Switch() and foreach() issues in php
我正在尝试将 foreach()
放入开关中。但是我得到了以下错误代码:
Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting
case (T_CASE) or default (T_DEFAULT) or '}' in
/.../index.php on line 20
我从 à $_GET['']
中获取 $lang
值
$dir = 'content/'.$lang;
$scan = array_diff(scandir($dir), array('..','.'));
switch($page) {
foreach ($scan as $value){
$value_ext_free = pathinfo($value, PATHINFO_FILENAME);
case $value_ext_free :
$page = $value_ext_free;
break;
}
default :
$page = 'welcome';
}
这对我来说似乎很合乎逻辑,但我不是 PHP 专家...有什么想法吗?
编辑
我只是用这个更新我的代码:
$dir = 'content/'.$lang;
$scan = array_diff(scandir($dir), array('..','.'));
foreach ($scan as $value){
$value_ext_free = pathinfo($value, PATHINFO_FILENAME);
}
if($page == $value_ext_free) {
include 'content/'.$lang.'/'.$page.'.php';
}
else {
include 'content/'.$lang.'/welcome.php';
}
除了我的文件夹中只有最后一页在内容中有效之外,它几乎可以正常工作...:/
谢谢! :)
卡塞勒
This method of implementation is dangerous and not recommended.
PHP Switch 我很确定您需要一个案例标记,例如:
Case X
并在其中包含语句块。因此对于其中的每个块。
看来你甚至不需要开关。
$dir = 'content/'.$lang;
if(file_exists($dir . DIRECTORY_SEPARATOR . $GET['filename']))
$page = $GET['filename'];
else
$page = 'welcome';
case
必须紧挨着开关
Foreach 将对数组的每个索引进行一次迭代
如果我正确理解你想要什么(抱歉,你的代码对我来说似乎不太合逻辑)你可能需要那样的东西
$dir = 'content/'.$lang;
$scan = array_diff(scandir($dir), array('..','.'));
foreach ($scan as $value){
$value_ext_free = pathinfo($value, PATHINFO_FILENAME);
$page = $value_ext_free;
}
if(!isset($page))
$page = 'welcome';
对于 switch 语句,使用以下语法
<?php
switch ($variable) {
case 'value':
runFunction1('value');
break;
case 'value2':
runFunction1('value2');
break;
default:
runFunction1('default');
break;
}
?>
对您更新后的代码的更正:
$dir = 'content/'.$lang;
$scan = array_diff(scandir($dir), array('..','.'));
$default = true;
foreach ($scan as $value){
$value_ext_free = pathinfo($value, PATHINFO_FILENAME);
if ($page == $value_ext_free) {
include 'content/'.$lang.'/'.$page.'.php';
$default = false;
break;
}
}
if ($default) {
include 'content/'.$lang.'/welcome.php';
}
我正在尝试将 foreach()
放入开关中。但是我得到了以下错误代码:
Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting case (T_CASE) or default (T_DEFAULT) or '}' in /.../index.php on line 20
我从 à $_GET['']
$lang
值
$dir = 'content/'.$lang;
$scan = array_diff(scandir($dir), array('..','.'));
switch($page) {
foreach ($scan as $value){
$value_ext_free = pathinfo($value, PATHINFO_FILENAME);
case $value_ext_free :
$page = $value_ext_free;
break;
}
default :
$page = 'welcome';
}
这对我来说似乎很合乎逻辑,但我不是 PHP 专家...有什么想法吗?
编辑
我只是用这个更新我的代码:
$dir = 'content/'.$lang;
$scan = array_diff(scandir($dir), array('..','.'));
foreach ($scan as $value){
$value_ext_free = pathinfo($value, PATHINFO_FILENAME);
}
if($page == $value_ext_free) {
include 'content/'.$lang.'/'.$page.'.php';
}
else {
include 'content/'.$lang.'/welcome.php';
}
除了我的文件夹中只有最后一页在内容中有效之外,它几乎可以正常工作...:/
谢谢! :) 卡塞勒
This method of implementation is dangerous and not recommended.
PHP Switch 我很确定您需要一个案例标记,例如:
Case X
并在其中包含语句块。因此对于其中的每个块。
看来你甚至不需要开关。
$dir = 'content/'.$lang;
if(file_exists($dir . DIRECTORY_SEPARATOR . $GET['filename']))
$page = $GET['filename'];
else
$page = 'welcome';
case
必须紧挨着开关
Foreach 将对数组的每个索引进行一次迭代 如果我正确理解你想要什么(抱歉,你的代码对我来说似乎不太合逻辑)你可能需要那样的东西
$dir = 'content/'.$lang;
$scan = array_diff(scandir($dir), array('..','.'));
foreach ($scan as $value){
$value_ext_free = pathinfo($value, PATHINFO_FILENAME);
$page = $value_ext_free;
}
if(!isset($page))
$page = 'welcome';
对于 switch 语句,使用以下语法
<?php
switch ($variable) {
case 'value':
runFunction1('value');
break;
case 'value2':
runFunction1('value2');
break;
default:
runFunction1('default');
break;
}
?>
对您更新后的代码的更正:
$dir = 'content/'.$lang;
$scan = array_diff(scandir($dir), array('..','.'));
$default = true;
foreach ($scan as $value){
$value_ext_free = pathinfo($value, PATHINFO_FILENAME);
if ($page == $value_ext_free) {
include 'content/'.$lang.'/'.$page.'.php';
$default = false;
break;
}
}
if ($default) {
include 'content/'.$lang.'/welcome.php';
}