-1

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;
}
}

closed as off-topic by Michael, Jacob Peattie, rudtek, kero, bosco Dec 10 '18 at 6:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    change ' $testvalue = 0 ' to ' $testvalue == 0 ' – Krishna Joshi Nov 22 '18 at 6:59
0

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)
NULL
FALSE
array() (an empty array)

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

  • 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
1

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.

  • 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

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