Jump to content

PHP inconsistancy between FreeBSD and CentOS


jrpruitt

Recommended Posts

Can someone please explain why this simply script comes up with a different answer on CentOS than it does on FreeBSD?

 

$x1=-1639260211;

$x2=-1558980842;

$y = $x1 + $x2;

 

FreeBSD Result: $y: 1096726243

CentOS Result:  $y: -2147483648 

 

FreeBSD 5.4-RELEASE-p9 (KGIX-SMP) #0: PHP Version 5.1.6

CentOS release 5 (Final): PHP 5.2.5

 

 

 

Link to comment
Share on other sites

Ok, I verified both PHP.INI files have precision = 14 and also verified in the phpinfo() that precision is 14.

But the above script still comes up with incorrect values.

 

ref: 32bit vs 64bit.  This is SUPPOSE to be the 32 bit version of CentOS, but that's not saying someone didn't install the wrong version.

It would be a major oversite for such a "portable" language to be affected by something like this, so I'm thinking there must to be some way

to force PHP to use 32 bit ints, even on a 64 bit platform.

 

 

This problem was tracked down after hours of diagnosing why BLOWFISH.PHP was totally incompatible with the other system.

 

Is there some way to GLOBALLY force PHP to use 32 bit integers?

(and fix any other issues that might be affected by this)?

Link to comment
Share on other sites

I've verified it's 32 bit CentOS.

 

I've also discovered the problem is NOT in the addition... it's the %d in the prinf() causing the problem:

 

 

FreeBSD:

y = -1639260211 + -1558980842

echo y: -3198241053

printf y: 1096726243

 

 

CentOS:

y = -1639260211 + -1558980842

echo y: -3198241053

printf y: -2147483648

 

Identical Script on both servers:

<?php

  $x1=-1639260211;

  $x2=-1558980842;

  $y = $x1+$x2;

 

  echo "y = ".$x1." + ".$x2."<br />\n";

 

  echo "echo y: ".$y."<br />\n";

  printf("printf y: %14d<br />\n",$y);

?>

 

Note: the "<br />\n" allows this script to display correctly from either web page or CLI.

 

If anyone has a clue as to what might be causing this, please help.

Link to comment
Share on other sites

Ok, the %d is odd, but it was simply hiding my original problem, which is a problem with the ^ operator:

 

 

<?php

$x1=-1639260211;

$x2=-1558980842;

$x3=3888969200;

 

$y1 = $x1+$x2;

$y2 = ($y1 ^ $x3);

$y3 = ($x1+$x2) ^ $x3;

 

echo "y1: ".$y1."<br />\n";

echo "y2: ".$y2."<br />\n";

echo "y3: ".$y3."<br />\n";

?>

 

FreeBSD:

y1: -3198241053

y2: -1500364013

y3: -1500364013

 

CentOS:

y1: -3198241053

y2: 1741485552

y3: 1741485552

 

 

I've tested this on SEVEN different Servers:

 

Three servers with Redhat 9  and one with FreeBSD get the y2: -1500364013 result.

 

Two different servers with CentOS and also one Redhat Enterprise get the y2: 1741485552 result.

 

I'm I wrong in thinking PHP is portable?

 

 

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.