Php 测试Foreach是否成功
我需要一种方法来检测“foreach”是否不成功。如果失败,则重复当前“else”中的相同错误消息Php 测试Foreach是否成功,php,Php,我需要一种方法来检测“foreach”是否不成功。如果失败,则重复当前“else”中的相同错误消息 <?php if(file_exists('redirects.xml')) { $xml = simplexml_load_file('redirects.xml'); if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
<?php
if(file_exists('redirects.xml')) {
$xml = simplexml_load_file('redirects.xml');
if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
foreach($xml->short as $shorts) {
if($shorts->name == $_GET['r']) {
header('Location: '.$shorts->url);
break;
}
}
}
else {
header("refresh:2;url=http://www.wlatw.co/");
echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
}
}
?>
在开始循环之前创建一个标志; 失败时将其设置为“未成功”
<?php
if(file_exists('redirects.xml')) {
$xml = simplexml_load_file('redirects.xml');
if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
$success = false; // set the flag
foreach($xml->short as $shorts) {
if($shorts->name == $_GET['r']) {
header('Location: '.$shorts->url);
$success = true;
break;
}
}
if ($success) { // do what you want when not success ful.
header("refresh:2;url=http://www.wlatw.co/");
echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
}
}
else {
header("refresh:2;url=http://www.wlatw.co/");
echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
}
}
?>
注意:正如@Sverri M.Olsen所说,在设置位置头之后,您应该始终停止脚本,可以使用die、exit或任何其他机制。在开始循环之前创建一个标志; 失败时将其设置为“未成功”
<?php
if(file_exists('redirects.xml')) {
$xml = simplexml_load_file('redirects.xml');
if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
$success = false; // set the flag
foreach($xml->short as $shorts) {
if($shorts->name == $_GET['r']) {
header('Location: '.$shorts->url);
$success = true;
break;
}
}
if ($success) { // do what you want when not success ful.
header("refresh:2;url=http://www.wlatw.co/");
echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
}
}
else {
header("refresh:2;url=http://www.wlatw.co/");
echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
}
}
?>
注意:正如@Sverri M.Olsen所说,在设置位置标题后,您应该始终停止脚本,可以使用die、exit或任何其他机制。您需要添加一个变量来跟踪循环的状态
<?php
if(file_exists('redirects.xml')) {
$xml = simplexml_load_file('redirects.xml');
if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
$url_is_malformed = false;
foreach($xml->short as $shorts) {
if($shorts->name == $_GET['r']) {
header('Location: '.$shorts->url);
break;
}
}
$url_is_malformed = true;
}
else {
$file_doesnt_exist = true;
}
if( $file_doesnt_exist || $url_is_malformed )
{
header("refresh:2;url=http://www.wlatw.co/");
echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
}
}
您需要添加一个变量来跟踪循环的状态
<?php
if(file_exists('redirects.xml')) {
$xml = simplexml_load_file('redirects.xml');
if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
$url_is_malformed = false;
foreach($xml->short as $shorts) {
if($shorts->name == $_GET['r']) {
header('Location: '.$shorts->url);
break;
}
}
$url_is_malformed = true;
}
else {
$file_doesnt_exist = true;
}
if( $file_doesnt_exist || $url_is_malformed )
{
header("refresh:2;url=http://www.wlatw.co/");
echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
}
}
是什么使foreach
循环成功还是失败?你的意思是如果没有$xml->short
作为不成功的迭代吗?你应该总是在头('Location:…')
头之后终止脚本。使用模具例如,是什么使foreach循环成功或不成功?你的意思是如果没有$xml->short
作为不成功的迭代吗?你应该总是在头('Location:…')
头之后终止脚本。使用模具例如,代码>我打算对出口的代码进行注释,但是如果代码后来更改为没有重定向,那么标志将是最好的解决方案。我打算对出口的代码进行注释,但是如果代码后来更改为没有重定向,那么标志将是最好的解决方案。