Be careful with PHP empty when using it in Wordpress shortcodes

- Wordpress , PHP empty . , .

empty(),

php.net, .

, empty() . , $foo :

if( empty($foo) ){ echo "variable is empty"; } // "variable is empty"

      
      



isset():

$foo = 1;
if( isset($foo) && !empty($foo) ){ echo "variable = ".$foo; } //  "variable = 1"

      
      



, PHP empty() , :

  • "" ( - strlen('') == 0 );
  • 0 ( - (int)0 );
  • 0.0 ( - (float)0.0 );
  • "0" ( - strlen("0") == 1 );
  • NULL;



  • FALSE;



  • array() ( - count( array( ) ) == 0 ).
$string_1 = '';
echo strlen($string_1); // 0
if( empty( $string_1 ) ){ echo 'string_1 is empty'; } //  "string_1 is empty"

$string_2 = '0';
echo strlen($string_2); // 1
if( empty( $string_2 ) ){ echo 'string_2 is empty'; } //  "string_2 is empty"
      
      



($string_2) ('0') empty() .

:

$string_3 = ' ';
echo strlen($string_3); // 1
if( empty( $string_3 ) ){ echo 'string_3 is empty'; } //   

      
      



PHP empty , .





empty() Wordpress

, Wordpress, . ("self") ("another") . . , "self" "another" , , , 25 15 .

[author_posts]
      
      



, :

[author_posts self="10" another="5" ]
      
      



, :

<?php
/**
 * @package author_posts
 */
/*
 * Plugin Name: Author posts
 * Description: Author posts
 * Version: 1.0.0
 * Author: V.Taran
 */
function posts_shortcode( $atts ) {

	$atts = shortcode_atts(
		array(
			'self' => '',
			'another' => '',
		),
		$atts,
		'author_posts'
	);
	if(!empty($atts['self'])){ 
		echo $atts['self']." post(s)"; 
	}else{ 
		echo "25 self posts"; }
	if(!empty($atts['another'])){ 
		echo $atts['another']." post(s)"; 
	}else{ 
		echo "15 posts of another authors";
	}
}
add_shortcode( 'author_posts', 'posts_shortcode' );
      
      



, . 10 5 - .

Wordpress Shortcode Output




, , , "another=0", . ! PHP empty() "0" , "0" , , .. 15 .









, :

[author_posts another="0" ]
[author_posts ]
      
      



, :

$is_null = (int)(-1);

'another' => $is_null,

if($atts['another'] > -1 ){ 
	echo $atts['another']." post(s)"; 
}else{ 
	echo "15 posts of another authors";
}


      
      



"another" , (-1). , , (-1).

, :

[author_posts another="0" ]
      
      



, .. 25 (.. "self" ) .

Features of php empty to be considered in Wordpress shortcodes




:

<?php
/**
 * @package author_posts
 */
/*
 * Plugin Name: Author posts
 * Description: Author posts
 * Version: 1.0.0
 * Author: V.Taran
 */
function posts_shortcode( $atts ) {
	
	$is_null = (int)(-1);
	
	$atts = shortcode_atts(
		array(
			'self' => '',
			'another' => $is_null,
		),
		$atts,
		'author_posts'
	);
	if(!empty($atts['self'])){ 
		echo $atts['self']." post(s)<br>"; 
	}else{ 
		echo "25 self posts<br>"; }
	if($atts['another'] > -1 ){ 
		echo $atts['another']." post(s)<br>"; 
	}else{ 
		echo "15 posts of another authors<br>";
	}
}
add_shortcode( 'author_posts', 'posts_shortcode' );
      
      



, , empty() . Wordpress "/wp_content/plugins/" , .

empty() PHP

, empty(), , , :

  • (array()), count sizeof;
  • false, if( false == $var );
  • , isset is_null();
  • is_numeric().

, , , , .

We found that empty PHP is a function that checks if a variable is empty. You need to remember what variable values ​​are defined as zero if you use it in your code, so you don’t have to look for why the program is not working properly.




All Articles