Jiangyue Zhu

Impossible is Nothing

Leave a comment

Survivor Game–PHP Programming

I did a PHP program as a coding test for one technical interview. The test was to use PHP to implement a function to play the ‘Survivor’ game. The game rule is like this:

You are in a room with a circle of 100 chairs.  The chairs are numbered sequentially from 1 to 100.At some point in time, the person in chair #1 will be asked to leave.  The person in chair #2 will be skipped, and the person in chair #3 will be asked to leave. This pattern of skipping one person and asking the next to leave will keep going around the circle until there is one person left… the survivor.


So what the function needs to do is to find out the survivor (which chair the survivor is sitting in).

My codes are like this:

/* Function remove consists of two args. One is the integer array, the other is
 * the token to control removal: 1--remove 0--not remove
 * When there is only one element left in the array, then the survivor found
function survivor($array,$rmToken){

 * if only one left, return it. The survivor found!**/
 if(count($array) == 1){

 return $array;
foreach($array as $key => $element){
 * token equals to 1
 * Remove current element from array
 * set token to 0, continue loop


 $rmToken = 0; 


 * token equals to 0
 * skip this element
 * set token to 1, continue loop **/
 else { 

 $rmToken = 1; 


 }// end foreach

 return survivor($array,$rmToken); //recursive 


// create an array containing a range of elements, from 1 to 100.
 $arr = range(1,100);
$result = survivor($arr,1); //call survivor function, return result
echo "The survivor is sitting on #".array_pop($result);
 // print_r($result);

1 Comment

Learn How to set up Zend Framework

After considering several days, I planned to learn Zend Framework. Actually, I had generated this idea since I graduate in last December. But I was confused about the process of setup. It seems that Zend Framework is powerful, but at the same time, you need to tolerate the complicate installation procedure.

Recently, I came across some tutorials on the web, and I finally, finally got it set up. I tried it on NetBeans 7.0 and Zend Studio.

It would be much easier when you use Zend Studio which would automatically help you set up the architecture if you choose ‘File’->’New’->’Zend Framework Project’.

The project structure looks like this

The files under public/ directory are the web pages for this application. Also, it should be noted that the project contains ‘models’,’views’, and ‘controllers’ folders.

Previously, when I tried this,  I got error when I launched the browser with url access to index.php under application/public/ directory. I found out that it was the reason that the project missed the Zend library, which can be downloaded here. I downloaded the zip file and extract the contents under library/ directory to the project library/ directory. Then it works!

The index page would like this:

I accessed to this result by this url: http://localhost:8080/zend/public/, where zend is my project name.

Also, the url:  http://localhost:8080/zend/public/index would have the same result because index under public/ directory is the a default action within a controller called Index.

For NetBeans IDE, the process is a little bit complicated. It is easy to find the way to create a new PHP application. After setting some necessary parameters such as project name, php version…, you have the option to select one framework. But it would invoke a alert which says that a zend script is required. Use the ‘Browse’ option and go to the folder of zend framework which was downloaded before and select ‘zf.bat’ and at the same time, you need to make sure that the Zend library is in your ‘\php\PEAR’ directory. In my case, I copy the ‘library/’ contents to ‘C:\xampp\php\PEAR’

After this, don’t forget to click the ‘Register Provider’ button just below the zend script line.

And, All the stuffs are set!!

The project structure is the same as Zend Studio one. And also, the result of launching the project is also the same.

Now, I would spend some time to study Zend Framework MVC. I want to share a good tutorial which is in PDF format. I t can be downloaded in http://akrabat.com/wp-content/uploads/Getting-Started-with-Zend-Framework.pdf

Also, the author provides some resources such as the sample codes for this tutorial: http://akrabat.com/zend-framework-tutorial/