php - 在php中定义未定义的数组键
问题描述
我正在使用以下网站在一个字段中插入两个由字母“k”分隔的代码。
<?php
if(isset($_POST['submitButton'])){
$input=$_POST['input'];
$explodedinput=explode('k', $input);
echo $explodedinput[0];
echo ' ';
echo $explodedinput[1];
}
?>
<form id="login" method="post">
<input type="password" name="input">
<input type="submit" name="submitButton" value="SEND">
</form>
问题来自用户错误并错误地引入了没有“k”的代码。然后,出现代码Warning: Undefined array key 1。
当输入中不存在“k”时,如何定义该值?
解决方案
<?php
$input=$_POST['input'];
if(isset($_POST['submitButton'])){
if (str_contains('k', $input)) {
$explodedinput=explode('k', $input);
echo $explodedinput[0];
echo ' ';
echo $explodedinput[1];
}
}
?>
推荐阅读
- c# - 使用 ItemAppearing 事件在 ListView 中向下滚动跳过 Xamarin.Forms 中的记录
- c# - 位图中的 C# 图像以定位 XY
- python - 捕获组的正则表达式可选顺序
- cypress - 赛普拉斯 scrollTo 方法的困难
- javascript - node.js 中的 curl -u 等效项
- git - 发布 Git 存储库而不暴露功能分支中的工作
- react-virtualized - React-virtualized List 滚动变得疯狂并且 Autosizer 不起作用
- spotlight-dbpedia - Dbpedia spotlight API limits
- php - 通过jquery在某个div之后插入wordpress短代码?
- nginx - 使用 Nginx 的多个域(反向代理)