Jump to content

[SOLVED] Variable Changing Suddenly


ialsoagree

Recommended Posts

I've run into a rather odd problem that I've run out of ways of diagnosing (mostly because there doesn't seem to be any possible explanation).

 

The simplest way I can describe it is simply by showing the code and displaying the output:

 

<?php
$count_all = 0;
if ($permission->check_permission($proper_parameters))
	$count_all += 2;
if ($permission->check_permission($proper_parameters))
	$count_all += 4;
if ($permission->check_permission($proper_parameters))
	$count_all += 2;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
echo ' >= '.$count_all; // echoes: >= 10
echo ' >= '.$count_all; // echoes: >= 101
?>

 

I'm not sure if this will help diagnose, but:

 

<?php
$count_all = 0;
if ($permission->check_permission($proper_parameters))
	$count_all += 2;
if ($permission->check_permission($proper_parameters))
	$count_all += 4;
if ($permission->check_permission($proper_parameters))
	$count_all += 2;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
if ($permission->check_permission($proper_parameters))
	$count_all += 1;
echo ' >= '.$count_all; // echoes: >= 10
echo ' >= '.$count_all; // echoes: >= 10
echo ' >= '.$count_all; // echoes: >= 101
?>

 

$count_all should be 10, I have no idea why it suddenly changes to 101.

 

The problem is that following these echoes is an "if" evaluation of a count on an array and the $count_all variable. Because $count_all seems to spontaneously change, the evaluation always results in false.

 

I first discovered the bug when I attempted to submit a form via AJAX. Instead of informing me that there were no updates to be made, the AJAX request received no response.

 

This is the only appearance of $count_all in any of my files, this is the only time I've ever used a variable with this name in this project. It doesn't appear before this in the file, nor does it appear again after (except in the "if" evaluation following the echoes).

 

If more code needs to be posted I will do so. There's no loop involved here, the above code appears within an "if" statement that appears within a switch's case. I don't see how any previous code could cause this problem, as I've said, $count_all is never used before or after the above code anywhere.

Link to comment
Share on other sites

You're echoing something else out in the code somewhere after that last $count_all that is equal to 1.  Since there's no newline or anything, it appears right next to it.

 

<?php
$count_all = 10;
echo "$count_all $count_all $count_all";
//arbitrary code here, as long as it doesn't echo anything
echo true;
?>

Link to comment
Share on other sites

DarkWater, thanks for the duh moment! I knew it was going to be something intuitively obvious but just couldn't figure it out.

 

It appears there's something wrong with my AJAX in processing the response. 1 is what it should have been getting all along, but after a recent update it seems to not process it correctly anymore.

 

In any event, thanks again.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.