如何让switch语句跳出后向下执行
$type=$_POST['type'];//直接看类型获取对应数据 无需登录switch ($type){case 1: //sql读数据1 $acase 2: //sql读数据2 $a}if($a){} //读出数据有就返回 没有就返回错误 die;if($user){} //验证是否登录//登陆后才能依类型获取的数据switch ($type){case 3: xxxxxxx $bcase 4: xxxxxxx $b}if($b){} //读出数据有就返回 没有就返回错误 die;switch ($type){case 5: xxxxxxx $ccase 6: xxxxxxx $c}if($c){} //读出数据有就返回 没有就返回“此项为空” 这里不算错
现在出现了一个情况
case 1 2 if$a xx else 111
case 3 4 if$b xx else 222
case 5 6 if$c xx else 333
type = 4 结果111
也就是从4跳出后 进行了 $a的判断 本意是想进行$b的
我希望跳出后能进行相对应的条件判断
当然把3个if嵌套起来就可以了,但这样意味着每次都可能出现多1-2次的无意义的判断
有什么好办法能只进行相对应的条件判断呢 求教
回复讨论(解决方案)
$type=$_POST['type']; //$type 只会是单值switch ($type) { case 1: //生成 sql 指令1 case 2: //条件生成 sql 指令2 //sql读数据 $a if($a){} //读出数据有就返回 没有就返回错误 die; break; case 3: xxxxxxx $b case 4: xxxxxxx $b if($b){} //读出数据有就返回 没有就返回错误 die; break; case 5: xxxxxxx $c case 6: xxxxxxx $c if($c){} //读出数据有就返回 没有就返回“此项为空” 这里不算错}
$type=$_POST['type']; //$type 只会是单值switch ($type) { case 1: //生成 sql 指令1 case 2: //条件生成 sql 指令2 //sql读数据 $a if($a){} //读出数据有就返回 没有就返回错误 die; break; case 3: xxxxxxx $b case 4: xxxxxxx $b if($b){} //读出数据有就返回 没有就返回错误 die; break; case 5: xxxxxxx $c case 6: xxxxxxx $c if($c){} //读出数据有就返回 没有就返回“此项为空” 这里不算错}
我试了一下
type=4时 是进行$b判断 但是是返回错误
原本应该读出数据的
其余也试过了 判断结果总是不符合条件
1 2可以
但1 2 无需登录
在
if($a){}
break;
之后
加了if($user)
判断登录
然后case 3
case 4
if($b){1}else{2}
结果一直是2
那就请你说明规则和贴出真实的代码
switch ($type) { case 1: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); case 2: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); if($a){ $rep['code'] = 1; $rep['valid'] = $a; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 3: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 4: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($b){ $rep['code'] = 1; $rep['valid'] = $b; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 5: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 6: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($c){ $rep['code'] = 1; $rep['valid'] = $c; reponse($rep);} else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);}}
function reponse($rep){ echo json_encode($rep); die;}
switch ($type) { case 1: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); case 2: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); if($a){ $rep['code'] = 1; $rep['valid'] = $a; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 3: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 4: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($b){ $rep['code'] = 1; $rep['valid'] = $b; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 5: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 6: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($c){ $rep['code'] = 1; $rep['valid'] = $c; reponse($rep);} else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);}}
function reponse($rep){ echo json_encode($rep); die;}
你确定 $sql = SQL语句; $b = mysql_fetch_assoc( $sql );这样能返回查询结果?
你这样必然是返回false的
$b = mysql_fetch_assoc(mysql_query($sql ));
那就请你说明规则和贴出真实的代码
验证登陆的去掉了还是当type = 3 4 时 判断不符合
然后5 6读出的是数组 用while循环后 不管是5还是6 都是全部读出来了。。
也就是while循环并没有包在case里面 。。。
type 的 1、2、3、4...都代表什么意思?
case 3 和 case 4 的 sql 指令是一样的吗?
switch ($type) { case 1: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); case 2: $sql = SQL语句 $a = = mysql_fetch_assoc($sql ); if($a){ $rep['code'] = 1; $rep['valid'] = $a; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 3: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 4: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($b){ $rep['code'] = 1; $rep['valid'] = $b; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);} break; case 5: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); case 6: $sql = SQL语句 $b = mysql_fetch_assoc($sql ); if($c){ $rep['code'] = 1; $rep['valid'] = $c; reponse($rep);} else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);}}
function reponse($rep){ echo json_encode($rep); die;}
你确定 $sql = SQL语句; $b = mysql_fetch_assoc( $sql );这样能返回查询结果?
你这样必然是返回false的
$b = mysql_fetch_assoc(mysql_query($sql ));
哦哦 sql那里有mysql_query
代码里是有的 毕竟 1 2 都读出来了
这里不小心漏写了
type 的 1、2、3、4...都代表什么意思?
case 3 和 case 4 的 sql 指令是一样的吗?
1是首页信息 2是一个详情信息 不用登陆
3是个人基本信息 4是个人实名信息 不一样的 读的同一张表的不同字段
都是单条数据
5是资金日志 6是积分日志
数组所以用while循环都读出来
那应该这样写
switch ($type) { case 1: $sql = SQL语句1 break; case 2: $sql = SQL语句2 break; case 3: $sql = SQL语句3 break; case 4: $sql = SQL语句4 break; case 5: $sql = SQL语句5 break; case 6: $sql = SQL语句6 break;}$a = = mysql_fetch_assoc($sql ); if($a){ $rep['code'] = 1; $rep['valid'] = $a; reponse($rep);}else{ $rep['code'] = 1008; $rep['error'] = 'Error parameters'; reponse($rep);}
$fun = switch_.$_GET['type'];$fun();function switch_1(){ a(true);}function switch_2(){ a(false);}function a($condition){ if(!$condition){ die('error'); } echo __Method__;}function switch_3(){ b(true);}function switch_4(){ b(false);}function b($login){ if(!$login){ die('error'); } echo __Method__;}function switch_5(){ c(true);}function switch_6(){ c(false);}function c($condition){ if(!$condition){ die('error'); } echo __Method__;}
这样是可以 不过有个小问题
就是成功都是返回数据
但失败要返回的错误提示是不一样的 还有要判断为空但是不算错的情况
所以要完全实现就要做多次判断 我想这样肯定不是最好的
我做了些调整 整体运作没问题了 不过应该也还有可以优化的地方
顺便我case实际有16个 写那么多function也挺麻烦了 不过这种方式学到了
感谢二位

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio
