1

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

13
  • 1
    Why not just use isset() instead? Commented Apr 30, 2013 at 15:30
  • 2
    What is the type on $somevariable? Is it an integer? Commented Apr 30, 2013 at 15:30
  • Is that the actual code? it may be that you have used = instead of == and that will always return true so else will never run. Commented Apr 30, 2013 at 15:30
  • Do you start with $somevariable = 1? If yes, did you double check the variable that overrides the 1? Commented Apr 30, 2013 at 15:31
  • 1
    If that is the case, your code should work. Try echoing out $row2['following'] inside the while loop. The output should be a series of zeros and ones. Let me know if that is or isn't the case. This will help us understand what's going on. echo "Following: " . $row2['following'] . "\n"; Commented Apr 30, 2013 at 18:24

3 Answers 3

10

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Or from the definition Satch3000 gave of what $somevariable contains try if (!empty($somevariable))
4

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.

1 Comment

I just comment for eye part I was doing code with without order I just put variable with no echo before know why because else I thought it echo Here is the code . If ($err[0]>0 ) echo $err[0].' '.$err[1]; else $pln1." $";
-1

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/

1 Comment

While I won't -1, isset will only check that the variable is set not empty.. You should use !empty instead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.