使用isset()函数
isset()函数是PHP中的内置函数, 它检查是否设置了变量, 并且该变量不是NULL。此函数还检查声明的变量, 数组或数组键是否具有空值, 如果存在, 则isset()返回false, 在所有其他可能情况下返回true。可以使用isset()函数解决此问题。
语法如下:
bool isset( $var, mixed )
描述:此函数接受多个参数。该函数的第一个参数是$ var。此参数用于存储变量的值。
程序:
<
? php if(isset($_GET['submit'])) {
$var = $_GET['option1'];
if(isset($var)) {
echo "Option 1 submitted successfully";
}
}
?>
<
html lang = "en">
<
head>
<
title>
srcmini Example<
/title>
<
meta charset = "utf-8">
<
meta name = "viewport" content = "width=device-width, initial-scale=1">
<
link rel = "stylesheet" href =
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<
style>
.gfg {
font-size:40px;
font-weight:bold;
color:green;
}
body {
text-align:center;
}
<
/style>
<
/head>
<
body>
<
div class = "container">
<
div class = "gfg">
srcmini<
/div>
<
h2>
Form control: checkbox<
/h2>
<
p>
The form below contains one checkbox.<
/p>
<
form method = "get">
<
div class = "checkbox">
<
label>
<
input type = "checkbox" name = "option1"
value = "http://www.srcmini.com/Option 1">
Option 1<
/label>
<
label>
<
button name = "submit" value = 'http://www.srcmini.com/true'
class = "btn btn-default">
SUBMIT<
/button>
<
/div>
<
/form>
<
/div>
<
/body>
<
/html>
输出如下:
文章图片
【如何读取PHP中是否选中了复选框()】使用empty()函数
empty()函数是PHP中的内置函数, 用于检查变量是否为空。
语法如下:
bool empty( $var )
描述:此函数确定变量是否为空。
例子:
<
? php
if(!empty($_GET['submit'])) {
$var = $_GET['option1'];
if(isset($var)){
echo "Option 1 submitted successfully";
}
}
?>
<
!DOCTYPE html>
<
html lang = "en">
<
head>
<
title>
srcmini Example<
/title>
<
meta charset = "utf-8">
<
meta name = "viewport" content = "width=device-width, initial-scale=1">
<
link rel = "stylesheet" href =
"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<
script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js">
<
/script>
<
script src = "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">
<
/script>
<
style>
.gfg {
font-size:40px;
font-weight:bold;
color:green;
}
body {
text-align:center;
}
<
/style>
<
/head>
<
body>
<
div class = "container">
<
div class = "gfg">
srcmini<
/div>
<
h2>
Form control: checkbox<
/h2>
<
p>
The form below contains one checkbox.<
/p>
<
form method = "get">
<
div class = "checkbox">
<
label>
<
input type = "checkbox" name = "option1"
value = "http://www.srcmini.com/Option 1">
Option 1<
/label>
<
label>
<
button name = "submit" value = "http://www.srcmini.com/true"
class = "btn btn-default">
SUBMIT<
/button>
<
/div>
<
/form>
<
/div>
<
/body>
<
/html>
输出如下:
文章图片
参考:
- http://php.net/manual/en/function.isset.php
- http://php.net/manual/en/function.empty.php
推荐阅读
- 如何在Python中读取大型文本文件()
- 如何使用printf()打印%()
- 如何在C++中打印数组参数的大小()
- 如何在Java中向左或向右旋转数组()
- 如何知道代码失败的测试用例()
- 如何使用PHP 7安装ext-curl扩展()
- 如何在Docker容器中安装Linux软件包()
- 如何在MacOS上安装Golang(详细步骤图解)
- 如何在Java中初始化和比较字符串()