4

I tried using <?php echo php_uname("m"); ?>, it returns i586 but I am on Windows 7 64 bit which I could see in My Computers Properties. So I am expecting x86_64 in output. Does any one know how to determine OS Architecture in PHP ?

I want the same thing for Mac OS X too. Any help would be appreciated.

user229044
  • 232,980
  • 40
  • 330
  • 338
Jigar D
  • 83
  • 1
  • 2
  • 5
  • 1
    Partly a duplicate of http://stackoverflow.com/questions/6303241/find-windows-32-or-64-bit-using-php – WWW Feb 10 '12 at 13:45
  • 1
    Just tested on my OsX and it worked fine... which version are you using? `php > print php_uname("m"); x86_64` – Peter Svensson Feb 10 '12 at 13:46
  • Duplicate of http://stackoverflow.com/questions/2353473/can-php-tell-if-the-server-os-it-64-bit – Leigh Feb 10 '12 at 13:56
  • @Leigh: I don't agree. He doesn't want to check the architecture of the **webserver**, but of the underlying OS. – Niklas B. Feb 10 '12 at 13:59
  • @NiklasB: The question title is: Can PHP tell if the server **os** it 64-bit. Did you read more than just the accepted answer? – Leigh Feb 10 '12 at 14:02
  • @Leigh: Thanks, no I didn't do that :) I'll try it out. – Niklas B. Feb 10 '12 at 14:04
  • @NiklasB: If I really had to do this, I'd probably use the WMI script method on first run, and cache the result. (detecting windows as the server software first of course). OSX is far simpler. – Leigh Feb 10 '12 at 14:05
  • @Crontab: I already tried that even that showed me 32 bit. – Jigar D Feb 10 '12 at 14:08
  • @JigarD: What does `system('wmic os get osarchitecture');` return for you? – Leigh Feb 10 '12 at 14:14
  • `PHP_INT_SIZE`, `intval($int)` are same nothing worked. And yes I want to check OS Architecture. In Mac, I can use `uname` in terminal so i think Mac would be easy. – Jigar D Feb 10 '12 at 14:17
  • @Leigh: Yes that worked, it shows 64-bit but I have 32 bit Intel PC beside me, same command there throws and error. Grr :X – Jigar D Feb 10 '12 at 14:27
  • I'm curious _why_ you want to determine this information. What does it matter if the OS is 32 or 64 compared to the running process? – sarnold Feb 11 '12 at 02:48
  • @sarnold: I am trying to run an script which runs only on 64 bit and not on 32 bit OS. So I should be able to hide it or throw an error when someone clicks it. – Jigar D Feb 11 '12 at 05:59

4 Answers4

10

Here is a php solution :)

echo strlen(decbin(~0));
alecsammon
  • 111
  • 1
  • 5
  • Fantastic answer! I'm not sure about 100% right detection, but for my current machine was detected as expected. – Artur Nov 16 '19 at 19:09
8

more simple

echo 8 * PHP_INT_SIZE;
user3665589
  • 81
  • 1
  • 1
1

php_uname checks the operating mode of PHP not of the OS.

So if your OS is 64 bit but php_uname is returning i586 that is because you are running the 32 bit version of PHP.

Knowing the architecture of PHP is arguably more important than knowing the architecture of the OS. For example, if you rely on the OS being 64 bit to make decisions in code, you may find yourself writing code that fails on 64 bit OS when PHP executing in 32 bit mode (as you are now). This is actually the precise situation you are in, you were expecting a 64 bit result but getting a result for 32 bit, which is because of the operating mode of PHP.

The real solution for your issue is to download and install the 64 bit version of PHP on your 64 bit OS in order to see the 64 bit results.


Here is a simple 1 line of code to detect if PHP is executing in 64 bit or 32 bit:

empty(strstr(php_uname("m"), '64')) ?  $php64bit = false : $php64bit = true;

After executing the line above $php64bit will be either true or false.

Here is a multi-line version of the same code:

// detect which version of PHP is executing the script (32 bit or 64 bit)
if(empty(strstr(php_uname("m"), '64'))){
  $php64bit = false;
}else{
  $php64bit = true;
}

This works by checking php_uname for the presence of 64 which would be found if PHP was executing in 64 bit mode.

Tim Penner
  • 3,551
  • 21
  • 36
1

My best guess is that even though your OS is 64bit, your Webserver is x86 and runs in WOW64-mode (32bit). If that's the case, it should be hard to figure out in pure PHP.

My suggestion (thanks to Leigh for linking to a similar question) is to use WMI:

$out = array();
exec("wmic cpu get DataWidth", $out);
$bits = strstr(implode("", $out), "64") ? 64 : 32;
echo $bits; // 32 or 64
Niklas B.
  • 92,950
  • 18
  • 194
  • 224
  • Like I said in above comment this throws an error in 32-bit intel. :( `ERROR: Code = 0x80041017 Description = Invalid query Facility = WMI`. I tried to run this in command prompt. I couldn't spot OSArchitecture in `wmic os` command – Jigar D Feb 10 '12 at 14:30
  • @Jigar: I added another approach, can you try that out on a 32-bit machine? – Niklas B. Feb 10 '12 at 14:34
  • I guess I will have to use this same code. An error will go in else condition which is 32 bit. But should I take the risk ? – Jigar D Feb 10 '12 at 14:36
  • @JigarD: I wouldn't take that risk. I added *yet* another approach which works on 64-bit, WOW64 and 32-bit for me. Try it out. – Niklas B. Feb 10 '12 at 14:43
  • Your other trick also works just fine. But again in my 32 bit pc if I change the condition specifying 32-bit. The output shown is 64-bit. I can use both your code but i feel its risky. – Jigar D Feb 10 '12 at 14:45
  • @Jogar: I don't get it. `$bits` will have the content 64 or 32, depending on which arch you are on. What's your problem, exactly? Doesn't that work? It does for me (and there are no errors!) – Niklas B. Feb 10 '12 at 14:47
  • Your `wmic` code works like a charm. Thanks. Its a risk because the same command throws an error on 32-bit Intel PC. Hope you understand now. Anyway I am using your code. Thanks again. – Jigar D Feb 11 '12 at 05:56
  • Be aware, this answer checks the _processor_, not _Windows_ as the question requests. On a test machine with a 64-bit processor but 32-bit Windows, this returns `64`. – Kit Grose Mar 09 '17 at 00:21
  • @KitGrose Indeed, careful with this approach – Niklas B. Mar 09 '17 at 16:12