Find Windows 32 or 64 bit using PHP

17,777

Solution 1

<?php
switch(PHP_INT_SIZE) {
    case 4:
        echo '32-bit version of PHP';
        break;
    case 8:
        echo '64-bit version of PHP';
        break;
    default:
        echo 'PHP_INT_SIZE is ' . PHP_INT_SIZE;
}

This code snippet will at-least tell you if a 32/64 bit version of PHP is running.

Solution 2

A slightly shorter and more robust way to get the number of bits.

    strlen(decbin(~0));

How this works:

The bitwise complement operator, the tilde, ~, flips every bit.

@see http://php.net/manual/en/language.operators.bitwise.php

Using this on 0 switches on every bit for an integer.

This gives you the largest number that your PHP install can handle.

Then using decbin() will give you a string representation of this number in its binary form

@see http://php.net/manual/en/function.decbin.php

and strlen will give you the count of bits.

Here is it in a usable function

function is64Bits() {
    return strlen(decbin(~0)) == 64;
}

Solution 3

If you have the COM extension installed (in php.ini) you can call the windows WMI service.

(Remember though that you event if you have a 64-bit processor, 64-bit OS and 64-bit PHP, your integers are still going to be 32-bit due to a limitation in x64-PHP on Windows.)

Anyway...

To check the OS:

function getOsArchitecture() {
    $wmi = new COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2');
    $wmi = $obj->ExecQuery('SELECT * FROM Win32_OperatingSystem');
    if (!is_object($wmi)) {
        throw new Exception('No access to WMI. Please enable DCOM in php.ini and allow the current user to access the WMI DCOM object.');
    }
    foreach($wmi as $os) {
        return $os->OSArchitecture;
    }
    return "Unknown";
}

or, check the physical processor:

function getProcessorArchitecture() {
    $wmi = new COM('winmgmts:{impersonationLevel=impersonate}//./root/cimv2');

    if (!is_object($wmi)) {
        throw new Exception('No access to WMI. Please enable DCOM in php.ini and allow the current user to access the WMI DCOM object.');
    }
    foreach($wmi->ExecQuery("SELECT Architecture FROM Win32_Processor") as $cpu) {
        # only need to check the first one (if there is more than one cpu at all)
        switch($cpu->Architecture) {
            case 0:
                return "x86";
            case 1:
                return "MIPS";
            case 2:
                return "Alpha";
            case 3:
                return "PowerPC";
            case 6:
                return "Itanium-based system";
            case 9:
                return "x64";
        }
    }
    return "Unknown";
}

Solution 4

You could write a function like this:

function is_32bit(){
  return PHP_INT_SIZE === 4;
}

Then you could use it like this:

if( is_32bit() ) {
    // do 32 bit stuffs
}
Share:
17,777

Related videos on Youtube

hjaffer2001
Author by

hjaffer2001

Kind, Loyal and faithful. Extremely well-developed senses and aesthetic appreciation for beauty. Flexible and open-minded. Likely to be original and creative. Enjoy the present moment.

Updated on June 01, 2022

Comments

  • hjaffer2001
    hjaffer2001 almost 2 years

    Is it possible to get the window processor bit?? I want to find the window processor bit using php?? I have coding to find the operating system and other properties. Kindly advice. Thanks - Haan

    • Arend
      Arend almost 13 years
    • Salman A
      Salman A almost 13 years
      Do you need to know whether the OS is 32/64 bit or the processor? A 64 bit processor could be running a 32 bit OS.
  • Admin
    Admin almost 13 years
    Note: this will tell you whether the client is running a 32/64 bit OS, not the server.
  • Rohan Verma
    Rohan Verma almost 13 years
    I think that is what he asked, he wants to know if his client is using a 32/64 bit OS to alter something.
  • breiti
    breiti about 12 years
    If you use this code in a function, you may want to throw an exception in the default case, as it is not safe to determine the if its 32 or 64 bit.
  • doublehelix
    doublehelix over 9 years
    Unfortunately, even when running 64-bit PHP on windows, integers are still limited to 32-bit, so you actually have no way of determining which type of PHP is installed (for Win only).You can however make an educated guess based on the limitation of the Operating system (e.g. if you're on 32-bit windows, you could not be running 64-bit PHP)
  • jdp
    jdp over 7 years
    What makes this more robust than PHP_INT_SIZE?
  • Daniel Rudy
    Daniel Rudy almost 6 years
    @doublehelix This is no longer correct. As of Win10.1709, echo PHP_INT_SIZE on a x64 platform reports 8 bytes. However, it also helps to be running a 64-bit version of PHP as well.
  • doublehelix
    doublehelix almost 6 years
    UPDATE: As of PHP 7.0 64-bit (8-byte) integers are now fully supported on the Windows platform. PHP 7 was not released until December 2015. Back in 2014 PHP 5 was not capable of this.
  • Álvaro González
    Álvaro González over 5 years
    This answer is technically incorrect because the size reported corresponds to the PHP process and it's possible to run 32-bit code in a 64-bit OS.
  • Álvaro González
    Álvaro González over 5 years
    Again, this reports the capability of the PHP process rather than OS.
  • sootsnoot
    sootsnoot over 3 years
    @jdp PHP_INT_SIZE was introduced in 5.0.5. decbin has been around since version 4.
  • Turab
    Turab about 2 years
    @sootsnoot PHP_INT_SIZE is around since v4.4. But nevertheless, no one should be using PHP v4 anyways.