Table of Contents
回复讨论(解决方案)
Home Backend Development PHP Tutorial 如何让switch语句跳出后向下执行

如何让switch语句跳出后向下执行

Jun 23, 2016 pm 01:25 PM

 $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){} //读出数据有就返回 没有就返回“此项为空” 这里不算错
Copy after login


现在出现了一个情况
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){} //读出数据有就返回 没有就返回“此项为空” 这里不算错}
Copy after login
Copy after login

$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){} //读出数据有就返回 没有就返回“此项为空” 这里不算错}
Copy after login
Copy after login


我试了一下
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);}}
Copy after login
Copy after login
Copy after login


function reponse($rep){	echo json_encode($rep); die;}
Copy after login
Copy after login
Copy after login

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);}}
Copy after login
Copy after login
Copy after login


function reponse($rep){	echo json_encode($rep); die;}
Copy after login
Copy after login
Copy after login



你确定 $sql = SQL语句; $b = mysql_fetch_assoc( $sql );这样能返回查询结果?
你这样必然是返回false的
$b = mysql_fetch_assoc(mysql_query($sql ));
Copy after login
Copy after login

那就请你说明规则和贴出真实的代码


验证登陆的去掉了还是当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);}}
Copy after login
Copy after login
Copy after login


function reponse($rep){	echo json_encode($rep); die;}
Copy after login
Copy after login
Copy after login



你确定 $sql = SQL语句; $b = mysql_fetch_assoc( $sql );这样能返回查询结果?
你这样必然是返回false的
$b = mysql_fetch_assoc(mysql_query($sql ));
Copy after login
Copy after login


哦哦 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);}
Copy after login

$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__;}
Copy after login

这样是可以 不过有个小问题
就是成功都是返回数据 
但失败要返回的错误提示是不一样的 还有要判断为空但是不算错的情况
所以要完全实现就要做多次判断  我想这样肯定不是最好的
我做了些调整 整体运作没问题了 不过应该也还有可以优化的地方
顺便我case实际有16个 写那么多function也挺麻烦了 不过这种方式学到了 
感谢二位

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

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

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

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

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

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-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

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

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

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' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

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.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

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

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

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

See all articles