php - Detect if empty, has only spaces, or is null
问题描述
I need to know how I can detect if my SQL Server column is empty, with spaces or null.
At the moment I have tested the following ways:
if($prof != NULL)
{
echo 'Test';
} else {
}
This 3 ways below:
if($prof != NULL)
if(empty($prof))
if(is_null($prof))
Any of them is working, my $prof
is one column from SQL table, I'm getting the column correctly.
In SQL server the column appears like this:
It looks like that I have spaces in there, but I can't tell PHP to do something if there are those spaces.
I need someone who can help me with this because I already tried more than this and I can't figure out how to solve this, I need to echo test, only if the field is not empty, but since it looks like it contains spaces it also does the echo to the ones that are "empty" it stills echo it.
解决方案
You can use trim
to remove all spaces on the beginning and end of the value:
echo (empty(trim($prof))) ? 'Test' : '';
You can also normalize your values on the SELECT
directly:
SELECT LTRIM(RTRIM(ISNULL(Profession, ''))) FROM table_name
After using LTRIM
, RTRIM
and ISNULL
you only need to check for empty string:
echo ($prof === '') ? 'Test' : '';
推荐阅读
- angular - 如何从 ngbdatepicker 角度禁用现有日期
- qt - 信号槽连接。进程被强行结束
- javascript - jquery ajax表单提交编辑值
- python - 如何返回字典中最大值对应的键?
- python - 以管理员身份运行的 Python windows 任务
- c# - 无法在 OpenFileDialog 中过滤 pdf 和 txt 文件
- python - 自动化无聊的东西第 6 章:密码柜
- python - RuntimeError: _thnn_mse_loss_forward 没有为类型 torch.cuda.LongTensor 实现
- asp.net-core - Fluent Assertion 中的对象结构比较方法
- javascript - 如何显示http错误消息角度6