Satch3000
Satch3000

Reputation: 49422

PHP else condition not working

I am stuck with some code.

It's quite simple, I have the following code:

 if ($somevariable == 1) {
   echo 'Do Something here';
} else {
   echo 'Do Something Else';                
} 

$somevariable is returning either 1 or nothing at all.

When the first condition is true ...when it's 1 it's working as it should ...

BUT the else condition does not return "Do Something Else" when nothing is return or it's empty.

Shouldn't ELSE mean any other condition?

How can I sort this problem out?


UPDATE

Actual Code:

$result2 = mysql_query("SELECT * FROM `follows` WHERE `user_id` = '$session_user_id' AND `venue_id` = '$venue_id'");

while ($row2 = mysql_fetch_assoc($result2))
{
$following = $row2['following'];

if ($following == 1) {
echo 'do something';
} else {

echo 'do something else';

}



}

Hope this helps

Upvotes: 1

Views: 28568

Answers (3)

themhz
themhz

Reputation: 8424

Try to Echo out the value and see what the database returns. What is the actual value? did you see the database?

$result2 = mysql_query("SELECT * FROM `follows` WHERE `user_id` = '$session_user_id' AND `venue_id` = '$venue_id'");

while ($row2 = mysql_fetch_assoc($result2))
{
$following = $row2['following'](int);

echo $following;
if ($following == 1) {
echo 'do something';
} else {

echo 'do something else';

}



}

PHP has different functions which can be used to test the value of a variable. Three useful functions for this are isset(), empty() and is_null(). All these function return a boolean value. If these functions are not used in correct way they can cause unexpected results.

isset() and empty() are often viewed as functions that are opposite, however this is not always true. In this post I will explain the differences between these functions.

isset()

From PHP Manual:

isset — Determine if a variable is set and is not NULL

In other words, it returns true only when the variable is not null.

empty() From PHP Manual: empty — Determine whether a variable is empty

In other words, it will return true if the variable is an empty string, false, array(), NULL, “0?, 0, and an unset variable.

is_null() From PHP Manual: is_null — Finds whether a variable is NULL

Check out more here

https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/

Upvotes: -1

Chris Bier
Chris Bier

Reputation: 14455

Try this:

if (isset($somevariable) && $somevariable === 1) {
   echo 'Do Something here';
} else {
   echo 'Do Something Else';                
} 

This will check if the variable is set and also if it is strictly equal to 1 both in value and in type (integer).

Edit: I've tried your code and it seems to work fine. I recommend that you post your actual code. I don't think that the problem is with your if statement.

Upvotes: 10

Your Common Sense
Your Common Sense

Reputation: 158005

How can I sort this problem out?

By finding and correcting typo-like errors.

Your code will ALWAYS echo something. So, there are only 2 possibilities

  • if it always echoing 'here' - $somevariable always contains non-empty value. Or in the real code you are running there is single =, not == as you posted
  • if it prints nothing - you are running the code without else part:

    if ($somevariable == 1) {
        echo 'Do Something here';
    }
    

So, here goes an algorithm for solving all such problems

  1. Trust your eyes.
  2. Check the code
  3. Check the code again.

Upvotes: 4

Related Questions