Archive for April 2013

add one arry to another array or append array in php

<?php
echo '<b>the first array :</b>';
$num1=array(1,2,3,4,5,6,7,8,9,10);
foreach($num1 as $num)
{
echo ' '.$num;
}
echo '<br><b>the second array :</b>';
$num2=array(11,12,13,14,15,16,17,18,19,20);
foreach($num2 as $num)
{
echo ' '.$num;
}
echo '<br><br><b>append the first array to the second array : </b>';

$num3 = array_merge($num1, $num2);

foreach($num3 as $num)
{
echo ' '.$num;
}

?>

output :

the first array : 1 2 3 4 5 6 7 8 9 10
the second array : 11 12 13 14 15 16 17 18 19 20

append the first array to the second array : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Why to use a daemon ?

In a standard web context, i.e. with a server executing PHP scripts to response clients' requests, every action asked by the clients is executed during the time the server build the response. For example sending a email to a lot of recipients will cause the execution of the following actions :

  • Get every email addresses the mailing should go to.
  • Foreach email addresses send the mail
These actions, more precisely the one sending the mail, can take some time, and repeating it hundreds of times could cause a timeout to the client's request. What happens then ? The emails not send before the timeout will never be sent, and the system will remain in an knwow state.

Asynchronous calls

To avoid this bad behaviour, a method called asynchronous call can be used. The principle is the following : insead of doing the whole action during the response, the action is put into a queue and is performed by another process which is independant of the client's request, process call "daemon".

The daemon will then look at the queue to check if there is some action to take, and do them until the queue is empty. When the queue is empty, it will wait until some new job come into the queue.

Asychronous calls are then implemented with two parts :
  • A queue which will contain the jobs to do
  • A daemon which will do the jobs in the queue.
This two parts will be largely used later in the concept and in the implementation.

switch in php

<?php

$input="1";

switch($input)
{
case 1       : echo 'one';
break;

case 2       : echo 'two';
break;

case 3       : echo 'three';
break;

case 4       :echo 'four';
break;

case 5       : echo 'five';
break;

case 'hello' : echo 'hello how are you?';
break;

default:
echo '<b>required number or string is not found</b>';
break;
}
?>

output :
one
=============================================

input : 2
output: two


input :3
output:three

input :4
output:four

input : hello
output: hello how are you?



Getting Started

I'm currently an ASP.NET developer in my day job, but I decided to spend some of my spare time learning PHP.  Some of my motivations for doing this are that the PHP development stack is free and the hosting options are much cheaper.  Since I'd love to someday run my own web development business this is appealing to me.  There are also many popular websites that run on PHP, such as Facebook. 

In order to get started with PHP, you will need a php web development stack. I chose to use the XAMPP stack after doing some research.  The reason I chose to use XAMPP is because I will be developing in a windows 7 environment, but I may want to deploy to a LAMP host.  XAMPP is an acronymn:

X: represents the server.  It is x because XAMPP is platform independent.
A: stands for Apache, which is the web server. If you are coming from an ASP.net background, you can think of Apache as the IIS of the PHP world.
M: stands for MySql, which is an open source database commonly used by PHP web applications
P: stands for PHP. 
P: stands for Perl.

To install XAMPP, you can download it from the apache friends website or you can follow the instructions from Murach's website

I did run into some issues getting Apache to run after installing XAMP.  This appears to be an issue specific to Windows 7. I will cover the resolution to that issue in my next post.

I highly recommend Murach's book PHP and MySQL as a guide to learning PHP: