Home > Backend Development > PHP Tutorial > 一起来精简这个语句

一起来精简这个语句

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:47:37
Original
1420 people have browsed it

if($type==0)
$type_value="a";
if($type==1)
$type_value="b";
if($type==2)
$type_value="c";

看看可以精简到什么程度。主要是为了得到$type_value的值,可以增加第三或第四个变量
我的做法如下:

$arr=array('a','b','c');
$type_valu=$arr[$type];

回复内容:

if($type==0)
$type_value="a";
if($type==1)
$type_value="b";
if($type==2)
$type_value="c";

看看可以精简到什么程度。主要是为了得到$type_value的值,可以增加第三或第四个变量
我的做法如下:

$arr=array('a','b','c');
$type_valu=$arr[$type];

方法一:高逼格,$type_value=$type==0?'a':($type==1?'b':($type==2&&'c'))
方法二:用ascii码,$type_value=chr($type+97);
不用谢,请叫我红领巾

'a' + type
Copy after login

类似的php写法请自行YY

另外,你的那种想法其实"abc"[$type]就行了。

$types  = array('x', 'y', 'z', ...);
$values = array('a', 'b', 'c', ...);
$res = array_combine($types, $values);
$type_value = $res($type);
Copy after login

前提:

  • $types值不重复
  • $types值很多
  • $types, $values无规则
Related labels:
php
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template