Viewed   43 times

I realise the second one avoids the overhead of a function call (update, is actually a language construct), but it would be interesting to know if one is better than the other. I have been using unset() for most of my coding, but I've recently looked through a few respectable classes found off the net that use $var = null instead.

Is there a preferred one, and what is the reasoning?

 Answers

2

It was mentioned in the unset manual's page in 2009:

unset() does just what its name says - unset a variable. It does not force immediate memory freeing. PHP's garbage collector will do it when it see fits - by intention as soon, as those CPU cycles aren't needed anyway, or as late as before the script would run out of memory, whatever occurs first.

If you are doing $whatever = null; then you are rewriting variable's data. You might get memory freed / shrunk faster, but it may steal CPU cycles from the code that truly needs them sooner, resulting in a longer overall execution time.

(Since 2013, that unset man page don't include that section anymore)

Note that until php5.3, if you have two objects in circular reference, such as in a parent-child relationship, calling unset() on the parent object will not free the memory used for the parent reference in the child object. (Nor will the memory be freed when the parent object is garbage-collected.) (bug 33595)


The question "difference between unset and = null" details some differences:


unset($a) also removes $a from the symbol table; for example:

$a = str_repeat('hello world ', 100);
unset($a);
var_dump($a);

Outputs:

Notice: Undefined variable: a in xxx
NULL

But when $a = null is used:

$a = str_repeat('hello world ', 100);
$a = null;
var_dump($a);

Outputs:

NULL

It seems that $a = null is a bit faster than its unset() counterpart: updating a symbol table entry appears to be faster than removing it.


  • when you try to use a non-existent (unset) variable, an error will be triggered and the value for the variable expression will be null. (Because, what else should PHP do? Every expression needs to result in some value.)
  • A variable with null assigned to it is still a perfectly normal variable though.
Saturday, October 22, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
2

You can do this via exec:

exec('wmic memorychip get capacity', $totalMemory);
print_r($totalMemory);

This will print (on my machine having 2x2 and 2x4 bricks of RAM):

Array
(
    [0] => Capacity
    [1] => 4294967296
    [2] => 2147483648
    [3] => 4294967296
    [4] => 2147483648
    [5] =>
)

You can easily sum this by using

echo array_sum($totalMemory);

which will then give 12884901888. To turn this into Kilo-, Mega- or Gigabytes, divide by 1024 each, e.g.

echo array_sum($totalMemory) / 1024 / 1024 / 1024; // GB

Additional command line ways of querying total RAM can be found in

  • https://superuser.com/questions/315195/is-there-a-command-to-find-out-the-available-memory-in-windows

Another programmatic way would be through COM:

// connect to WMI
$wmi = new COM('WinMgmts:root/cimv2');

// Query this Computer for Total Physical RAM
$res = $wmi->ExecQuery('Select TotalPhysicalMemory from Win32_ComputerSystem');

// Fetch the first item from the results
$system = $res->ItemIndex(0);

// print the Total Physical RAM
printf(
    'Physical Memory: %d MB', 
    $system->TotalPhysicalMemory / 1024 /1024
);

For details on this COM example, please see:

  • http://php.net/manual/en/book.com.php
  • MSDN: Constructing a Moniker String
  • MSDN: Win32_ComputerSystem class

You can likely get this information from other Windows APIs, like the .NET API., as well.


There is also PECL extension to do this on Windows:

  • win32_ps_stat_mem — Retrieves statistics about the global memory utilization.

According to the documentation, it should return an array which contains (among others) a key named total_phys which corresponds to "The amount of total physical memory."

But since it's a PECL extension, you'd first have to install it on your machine.

Wednesday, October 26, 2022
 
2

The PHP shmop functions are just wrappers for the underlying POSIX functions, which doesn't seem to be available under windows.

From the PHP manual:

Note: Versions of Windows previous to Windows 2000 do not support shared memory. Under Windows, Shmop will only work when PHP is running as a web server module, such as Apache or IIS (CLI and CGI will not work).

Appearently PHP emulates this behaviour within apache, but since it isn't available in the stand alone binaries it will hardly integrate with the windows equivalents.

Friday, December 2, 2022
 
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :