首页 > 解决方案 > 如何确定某物是否不是字符串以及某物是否为空?

问题描述

在我的 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']);
?>

标签: phpstringget

解决方案


您应该使用 isset() 方法来检查是否提供了 $input1 或 $input2。

function check_valid($test) {
    if (isset($test) && is_string($test)) {
        echo "Valid";
        return;
    }
    echo "Not Valid";
}

推荐阅读