开发者

Why does using an If statement change a variable?

I am reading a text file and processing some records, a relevant sample of the text file is

#export_dategenre_idapplication_idis_primary
#primaryKey:genre_idapplication_id
#dbTypes:BIGINTINTEGERINTEGERBOOLEAN
#exportMode:FULL
127667880285760063715151750
127667880285760123715151751

I want to perform a specific action when application_id is already stored within my database AND is_primary = 1

I wrote this PHP to test my code:

$fp1 = fopen('genre_application','r');
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;}


while (!feof($fp1)) {
    $line = stream_get_line($fp1,128,$eoldelimiter); //use 2048 if very long lines
if ($line[0] === '#') continue;  //Skip lines that start with # 
    $field = explode ($delimiter, $line);
list($export_date, $genre_id, $application_id开发者_StackOverflow中文版, $is_primary ) = explode($delimiter, $line);

// does application_id exist? 
$application_id = mysql_real_escape_string($application_id); 
$query = "SELECT * FROM jos_mt_links WHERE link_id='$application_id';"; 
$res = mysql_query($query); 
if (mysql_num_rows($res) > 0 ) { 
echo $application_id . "application id has genre_id" . $genre_id . "with primary of " . $is_primary. "\n";
} else 
{
// no, application_id doesn't exist 
}

} //close reading of genre_application file
fclose($fp1);

which results in this output on screen and is exactly as I expected.

371515175application id has genre_id6006with primary of 0

371515175application id has genre_id6012with primary of 1

If I then add an IF statement as in the code below, it somehow changes the value of is_primary as shown by the screen display

$fp1 = fopen('genre_application','r');
if (!$fp) {echo 'ERROR: Unable to open file.'; exit;}


while (!feof($fp1)) {
    $line = stream_get_line($fp1,128,$eoldelimiter); //use 2048 if very long lines
if ($line[0] === '#') continue;  //Skip lines that start with # 
    $field = explode ($delimiter, $line);
list($export_date, $genre_id, $application_id, $is_primary ) = explode($delimiter, $line);

// does application_id exist? 
$application_id = mysql_real_escape_string($application_id); 
$query = "SELECT * FROM jos_mt_links WHERE link_id='$application_id';"; 
$res = mysql_query($query); 
if (mysql_num_rows($res) > 0 ) { 
if ($is_primary = '1')  echo $application_id . "application id has genre_id" . $genre_id . "with primary of " . $is_primary. "\n";
} else 
{
// no, application_id doesn't exist 
}

} //close reading of genre_application file
fclose($fp1);
?>

The code above results in the following screen display, which incorrectly has the first field with a primary of 1, when as can be seen by the previous screen display and the sample text file it should be 0

371515175application id has genre_id6006with primary of 1

371515175application id has genre_id6012with primary of 1

Can anyone explain what I am doing to make the variable change and how I should use the If correctly please?


You are assigning a value instead of comparing:

($is_primary = '1')

you need

($is_primary == '1')

or === for a type-safe comparison.

This is why some people like to write their comparisons like so:

('1' == $is_primary)

the mistake is impossible to make here because "1" can't be assigned anything.

Personally though, I think that over time and with growing practice, one will learn to spot the mistake.


You need to use:

if ($is_primary == '1')

NOT

if ($is_primary = '1')

Because "=" defines variable and returns true, but "==" actually compares and doesn't change anything.


The if-line has only one = (assign value) instead of two == (compare value).

Try this instead:

if ($is_primary == '1')


if ($is_primary = '1') You need to use the comparison operator ==, not the assignment operator =


if ($is_primary == '1') ....

NOT = BUT ==

Also consider removing all the stuff that bug the reading of your question for example somthing like:

// does application_id exist? 
$res = mysql_query($query); 
if (mysql_num_rows($res) > 0 ) { 
    echo  $is_primary. "\n";
} 

This is more readable and fit you purpose. You may even find yourself does kind of bug.


If you type if($x=10)...

Then you're telling it to attempt the operation $x=10 to which it will return a success (return true). This will always run the loop, and alter the value of x.

To test $x, use if($x==10)...

But note that to test for $x not equals 10 is if($x!=10)...

ie, one equals and one bang.


Chek out PHP comparison operators

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜