php - 如何确定某物是否不是字符串以及某物是否为空?
问题描述
在我的 php 中,我有 2 个可选输入。input1=
和input2=
。两者都是可选输入。我的问题是如何确定是没有提供输入还是提供的输入不是字符串?
我只希望人们放一个实际的字符串。不是不同类型的数据结构。
例子
有效的:www.example.com/myfile.php?input1=hello&input2=bye
有效的:www.example.com/myfile.php?input1=hello
有效的:www.example.com/myfile.php?input2=hello
有效的:www.example.com/myfile.php
无效的:www.example.com/myfile.php?input1[]
<?php
function check_valid($string) {
if (!is_string($string)) {
echo "This is a not string. We tested: ".$string."<br>";
} else {
echo "This is is string. We tested: ".$string."<br>";
}
}
$input1 = check_valid($_GET['input1']);
$input2 = check_valid($_GET['input2']);
?>
解决方案
您应该使用 isset() 方法来检查是否提供了 $input1 或 $input2。
function check_valid($test) {
if (isset($test) && is_string($test)) {
echo "Valid";
return;
}
echo "Not Valid";
}
推荐阅读
- ios - 在文本字段目标 c iOS 中为文本添加前缀
- ios - iOS:此时无法安装此应用程序 - LegacyErrorString=DuplicateIdentifier
- scala - Play 框架的 MySQL 数据库连接
- c# - 如何为单元测试调用模板化控制器操作?
- docker - Jhipster 微服务应用程序在 docker 容器中启动时冻结
- java - 时区 CEST 到 GMT
- php - 如何在 PHP 5.6 中使用 glob 函数查找和删除文件
- javascript - 如何加入/合并 json 结果
- php - 不能在 Laravel 刀片模板中使用 stdClass 类型的对象作为数组
- resteasy - RestEasy 客户端没有使用正确的编码