Home > php教程 > php手册 > php中问号逗号运算符$a?

php中问号逗号运算符$a?

WBOY
Release: 2016-05-23 13:07:23
Original
1278 people have browsed it

在php中问号运算符可以简单代替条件运算符了,我们在很多的开发应用中都会碰到一问号运算符的应用,下面我来简单的给各位新手介绍一下吧.

问号逗号运算符语法,代码如下:

$a =1; 
$b = $a==1 ? 'A' : 'B'; 
echo $a; 
//结果输出 
A
Copy after login

解析上面的语句成if else,代码如下:

$a =1; 
if( $a ==1 ) 
{ 
  echo 'A'; 
} 
else 
{ 
  echo 'B'; 
} 
//结果输出 
A
Copy after login

例子,在应用中我们根据过来的参数定义表名,代码如下:

$tabname = $tag ==1 ? "aatt":"bfav"; 
$sql ="select * from $tabname  "; //
Copy after login

例子,在获取数据post或get时我们也可以使用isset() 与问号运算符来处理,代码如下:

$id = htmlspecialchars(isset($_GET[$str])?$_GET[$str]:'');

好了到这里我们就己经介绍完了问号运算符的用法了.


教程网址:

欢迎收藏∩_∩但请保留本文链接。

Related labels:
source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template