Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Semaphore, Shared Memory and IPC Functions : shm_get_var

shm_get_var

Returns a variable from shared memory (PHP 4, PHP 5)
mixed shm_get_var ( int shm_identifier, int variable_key )


Code Examples / Notes » shm_get_var

06-may-2005 10:01

You will still receive a notice use @:
if(!defined(@shm_get_var($mutex, $mutex_key))) {
      shm_put_var($mutex, $mutex_key, 0);
}


jm

To follow up on the posts by anonymous, Bob Van Zant and chris at free-source dot com below (or, as must people inexplicably write, above) regarding the PHP warning and FALSE that shm_get_var returns if the variable key doesn't exist:
My tests (with PHP4.3.4) show that defined() is useless here. Because the function defined(string) checks whether the constant whose name is string exists, the code
<?php
if ( defined(@shm_get_var($mutex, $mutex_key)) {
  ...
}
?>
acts the same ("..." does not get executed) whether the variable is defined or not--unless $mutex_key happens to identify a valid string that happens to be the name of a constant. :)
Rather,
<?php
if ( @shm_get_var($mutex, $mutex_key) === FALSE ) {
  ...
}
?>
works, provided the object that was stored isn't actually FALSE (via <?php shm_put_var($mutex, $mutex_key, FALSE); ?>)
It would be nice to have a completely air-tight solution, though.  D'oh!


bob van zant

This seems to work fine to detect the lack of presence of a key in shared memory and then init it to 0 when found:
if(!defined(shm_get_var($mutex, $mutex_key))) {
       shm_put_var($mutex, $mutex_key, 0);
}


chris

if the variable_key asked for does not exist php generates a warning and shm_get_var() will return bool(false).  there doesn't seem to be a clean way to test if a key exists.

Change Language


Follow Navioo On Twitter
ftok
msg_get_queue
msg_receive
msg_remove_queue
msg_send
msg_set_queue
msg_stat_queue
sem_acquire
sem_get
sem_release
sem_remove
shm_attach
shm_detach
shm_get_var
shm_put_var
shm_remove_var
shm_remove
eXTReMe Tracker