PHP explode

Say you store a csv formatted string somewhere to represent an array. For example, Johnny has items “apple|orange|banana”. Then in your code, you may parse out the items as follows: $items = "apple|orange|banana"; // retrieve it from somewhere $my_arr = explode("|", $items); if (count($my_arr) > 0){ print "Johnny has something."; }else{ print "Johnny has nothing."; } It works fine as it seems. Well, actually, you will never see the “Johnny has nothing” print out. Surprisingly, even if $items is an empty string, explode will still return an array with one element. That array is: ...

November 1, 2012 · 1 min · birdchan

Formatting mysql datetime with php

Very useful, so posting here. Read the php date function page for more details. $mysql_datetime = date("Y-m-d H:i:s"); Ref: http://stackoverflow.com/questions/136782/format-mysql-datetime-with-php

October 23, 2012 · 1 min · birdchan

Representing a class in variable in PHP?

Say you wrote some code below for projectA. function do_task(){ // init my_init(); // calling a class function $v = class_A::some_method($p1, $p2, $p3, $p4, $p5, $p6, $p7); // return return $v; } Now, your boss loves it. Then he asks you to do a very similar thing for projectB. Naively, you may do: function do_task($project_type){ // init my_init(); // calling a class function if ($project_type == "projectA"){ $v = class_A::some_method($p1, $p2, $p3, $p4, $p5, $p6, $p7); }elseif ($project_type == "projectB"){ $v = class_B::some_method($p1, $p2, $p3, $p4, $p5, $p6, $p7); } // return return $v; } Um… it would work, but imagine if you will soon have projectC, and D, and on… and what if you need to add one more parameter to the some_method function? ...

July 23, 2012 · 2 min · birdchan

Using memcache in php

So now I assumed you already got memcache running. If not, check out my previous tutorial on how to do that. Now in your php project, include the following code somewhere in your init.php file. Or you can always make it object oriented if you like. # Connect to memcache: global $memcache; global $memcache_server_up; $memcache = new Memcache; $memcache_server_up = $memcache->connect('127.0.0.1', 11211); # check to see if memcache server is up function memcache_server_is_up(){ global $memcache_server_up; return $memcache_server_up; } # Gets key / value pair into memcache function getCache($key) { global $memcache; if (memcache_server_is_up()) { return $memcache->get($key); }else{ return ""; } } # Puts key / value pair into memcache function setCache($key, &$object, $timeout = 600) { global $memcache; if (memcache_server_is_up()) { return $memcache->set($key,$object,MEMCACHE_COMPRESSED,$timeout); } } My setup will work if you have only one memcache daemon running. If you have a few, then make your changes accordingly. ...

June 28, 2012 · 2 min · birdchan

Installing memcache on osx for php

Finally got memcache working! I am compiling the steps here in case I need to do it again. First install libevent. This is a dependency to memcached, so need to get it. cd /tmp curl -OL https://github.com/downloads/libevent/libevent/libevent-2.0.17-stable.tar.gz tar -xvzf libevent-2.0.17-stable.tar.gz cd libevent-2.0.17-stable* ./configure make sudo make install Then install memcached. # Compile memcached utility cd /tmp curl -O http://memcached.googlecode.com/files/memcached-1.4.13.tar.gz tar -xvzf memcached-1.4.13.tar.gz cd memcached-1.4.13* ./configure make sudo make install At this point, if everything goes well, the memcache daemon should be ready to run. You can try the following to see if memcached returns anything to you. ...

June 21, 2012 · 2 min · birdchan

storing blob into mysql through apache and php

This idea sounds easy and stuff. I agree. I assume you already got it working fine. But perhaps one day you may notice something is not working well, especially when you are dealing with bigger files. Below are a few things you want to look out for. Change your /etc/php.ini upload_max_filesize = 2M post_max_size = 8M memory_limit = 128M The default upload file size is 2M, which could be too small. If your upload file is larger than this, you will receive an empty upload file at your server side. So, increase this. While if you are using ajax to post-send, increase the post_max_size limit. Another thing to watch out for is memory_limit. If you somehow make copies of big blobs in your code, increasing PHP’s memory limit is also a good idea. ...

September 2, 2011 · 3 min · birdchan

PHP read from a mounted drive

Recently, I needed to read from a mounted drive on osx using Apache and PHP. I need to grab some file names in a certain directory using opendir or scandir, then dynamically populate a select dropdown box. The issue is, the default apache user is _www being in the _www group, while the mounted drive allows only the “staff” group to enter. In my case, I cannot change the mounted drive access permissions. ...

August 31, 2011 · 2 min · birdchan

Setting up mcrypt for php

I found a very detailed step-by-step guide to install the mcrypt php extension on snow leopard. It’s not as simple as doing an apt-get, so I am posting the link here for future reference. ;) http://michaelgracie.com/2009/09/23/plugging-mcrypt-into-php-on-mac-os-x-snow-leopard-10-6-1/

June 3, 2011 · 1 min · birdchan