Php Connect Memcached
# PHP Connecting to Memcached Service
In the previous chapters, we have already introduced how to install the Memcached service. Next, we will introduce how PHP uses the Memcached service.
### PHP Memcache Extension Installation
The PHP Memcache extension package download address: [http://pecl.php.net/package/memcache](http://pecl.php.net/package/memcache), you can download the latest stable package.
wget http://pecl.php.net/get/memcache-2.2.7.tgz
tar -zxvf memcache-2.2.7.tgz
cd memcache-2.2.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install
**Note:** `/usr/local/php/` is the installation path of PHP, you need to adjust it according to the actual directory you installed.
After successful installation, it will show the location of your memcache.so extension, for example, mine:
Installing shared extensions: /usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/
Finally, we need to add this extension to PHP. Open your php.ini file and add the following content at the end:
extension_dir = "/usr/local/php/lib/php/extensions/no-debug-non-zts-20090626/"
extension = memcache.so
After adding, restart PHP. I use nginx+php-fpm process, so the command is as follows:
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
If it is Apache, use the following command:
/usr/local/apache2/bin/apachectl restart
Check the installation result
/usr/local/php/bin/php -m | grep memcache
If installed successfully, it will output: memcache.
Or check it by accessing the `phpinfo()` function in the browser, as shown below:

* * *
## PHP Connecting to Memcached
```php
connect('localhost', 11211) or die ("Could not connect"); //Connect to Memcached server
$memcache->set('key', 'test'); //Set a variable to memory, name is key, value is test
$get_value = $memcache->get('key'); //Get the value of key from memory
echo $get_value;
?>
For more PHP operations on Memcached, please refer to: [http://php.net/manual/zh/book.memcache.php](http://php.net/manual/zh/book.memcache.php)
YouTip