简体   繁体   English

如何判断字符串的元值是空还是空?

[英]How to tell if string is null or empty in meta value?

I am trying to figure out how to find make sure a string is not null or empty then execute code. 我试图弄清楚如何查找确保字符串不为null或为空,然后执行代码。 The place I am pulling the value from is a meta value that is stored from a meta box on the post page in wordpress. 我从中提取值的位置是一个元值,该值是从wordpress中的帖子页面上的元框存储的。

I want to check if this returns a null or empty value and if it doesnt then execute code after: 我想检查是否返回null或空值,如果不返回,则在执行以下代码:

<?php echo esc_attr( get_post_meta($post->ID, 'newtheme_section_id', true) ); ?>

The code above retrieves a value that the user had input into a meta box on the post page. 上面的代码检索用户已输入到帖子页面上的meta框中的值。

I was thinking that I would remove the echo and create a variable to hold the output of get_post_meta then somehow check if the variable is null or empty. 我当时在想删除回声并创建一个变量来保存get_post_meta的输出,然后以某种方式检查该变量是否为null或为空。

I had actually posted it in the Wordpress Stack Exchange website but it was more of a php question so I posted it here. 我实际上已经将其发布在Wordpress Stack Exchange网站上,但是它更多是关于php的问题,因此我将其发布在这里。

You can check if its empty with one of these functions 您可以使用以下功能之一检查其是否为空

isset($var)

This returns TRUE is the variable is set. 如果设置了变量,则返回TRUE。

or with this 或与此

empty($var)

This returns TRUE is the variable is empty. 如果变量为空,则返回TRUE。

I just tried this while using the WP Types & Fields plugin from Toolset. 我只是在使用Toolset的WP Types&Fields插件时尝试过此操作。

isset($var) didn't work correctly. isset($var)无法正常工作。 Even empty fields returned true. 即使是空字段也返回true。

empty($var) did work correctly. empty($var)正常工作。

You could also test the string's length. 您也可以测试字符串的长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM