Strange... I put an if statement for $testvalue = 0. The else part runs and when I echo the value of the $testvalue it gives me a 0.

So what is the true value of $testvalue?

I need to be able for the if the part to run.

I am trying to use the post_exists() function to check if a post exists in the database. If it doesn't exist, I want to call the insert_post() function to auto create a new post or page. but I need this returning a 0 to work.

any advice on why $testvalue = 0 is not running the if part (echo "before: " . $testvalue)

if ( ! is_admin() ) {
require_once( ABSPATH . 'wp-admin/includes/post.php' );

add_action('wp_footer', 'my_customoutput');

function my_customoutput(){

$checklinktitle = 'Events 321 Page';

$testvalue = post_exists( $checklinktitle );

if ($testvalue = 0) {
    echo 'Before: ' . $testvalue;
} else {
    echo 'Firing of Else statement but the value of $testvalue is ' . $testvalue;
  • 6
    change ' $testvalue = 0 ' to ' $testvalue == 0 ' – Krishna Joshi Nov 22 '18 at 6:59

You're not checking the value correctly. A single = sign is the assignment operator. So when you write $variable = 0 you are setting the variable to 0. This happens regardless of whether or not you're in an if statement.

So when you write if ( $testvalue = 0 ) { you're setting $testvalue to 0, which is essentially the same as false, so the condition will fail.

To compare a value you need to use a comparison operator. You can see that link for more information, but essentially you need to use == to check if values are the same, or === to check if they're the same and the type.

if ( 0 === $testvalue ) {
    echo 'Before: ' . $testvalue;
} else {
    echo 'Firing of Else statement but the value of $testvalue is ' . $testvalue;

Note that I've used a yoda condition. From the WordPress PHP Coding Standards:

When doing logical comparisons involving variables, always put the variable on the right side and put constants, literals, or function calls on the left side.

|improve this answer|||||
  • 1
    Comparing 0 === $testvalue is a nicer way - this way if you make a typo and write only one =, you'll get a warning... – Krzysiek Dróżdż Nov 22 '18 at 7:49
  • Good point. Updated. – Jacob Peattie Nov 22 '18 at 7:52

You need to check emptyness of value by using empty() function.

if (empty($testvalue)) {
    echo 'Before: ' . $testvalue;
} else {
    echo 'Firing of Else statement but the value of $testvalue is ' . $testvalue;

Note: The following values are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
array() (an empty array)

Or you can use $testvalue == 0 as @Krishna Joshi suggested.

|improve this answer|||||
  • Your last line is incorrect. When using ==, 0 is the same as all your given values, except for the empty array. Since post_exists returns an integer or 0, then that's what they should be testing for. – Jacob Peattie Nov 22 '18 at 7:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.