I was annoyed by this warning when using shm_get_var():
Warning: shm_get_var() [function.shm-get-var]: variable key 2 doesn't exist in PATH_TO_FILE on line 64.
You can suppress the warning by changing the error reporting level. To do this for just the page in question, include the following line:
<?php
error_reporting(E_ERROR);
?>
For more info: http://us.php.net/error_reporting
shm_get_var
(PHP 4, PHP 5)
shm_get_var — Devuelve una variable de la memoria compartida
Descripción
shm_get_var() devuelve la variable con la llave variable_key dada. La variable, queda presente en la memoria compartida.
shm_get_var
php at pharse dot com
03-Dec-2008 08:05
03-Dec-2008 08:05
ricardophp at yahoo dot com dot br
27-Sep-2008 09:18
27-Sep-2008 09:18
A fully functional sample ...
<?php
echo "<PRE>\n";
define("FOPEN_RESOURCE", 1);
$shm_id = shm_attach(FOPEN_RESOURCE);
if ($shm_id === false) {
exit("Fail to attach shared memory.\n");
}
$fopen_resource = fopen("/tmp/phpSharedMemory.bin", "w");
$a = array("Teste1", 1);
if (!shm_put_var($shm_id, $a, $a)) {
exit("Failed to put var 1 in shared memory $shm_id.\n");
}
echo "F: ".$a[0].":".$a[1]."\n";
$pid = pcntl_fork();
if($pid == -1) {
die("could not fork\n");
}
else if ($pid) {
$a = array("Teste2", 3);
if (!shm_put_var($shm_id, $a, $a)) {
exit("Failed to put var 1 in shared memory $shm_id.\n");
}
echo "P1: ".$a[0].":".$a[1]."\n";
} else {
sleep(2);
$a = shm_get_var($shm_id, $a);
echo "P2: ".$a[0].":".$a[1]."\n";
}
pcntl_wait($status);
exit();
?>
JM
14-May-2005 10:50
14-May-2005 10:50
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!
06-May-2005 09:01
You will still receive a notice use @:
if(!defined(@shm_get_var($mutex, $mutex_key))) {
shm_put_var($mutex, $mutex_key, 0);
}
Bob Van Zant
18-Feb-2005 08:05
18-Feb-2005 08:05
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 at free-source dot com
05-Dec-2004 10:47
05-Dec-2004 10:47
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.
