October 22, 2011
Basic Php array and function usage tutorial

I was planning to post a tutorial about “How to develop a Facebook application” but one of my friends wanted me a simple php code and i thought posting it on my blog can be useful for people who’s searching basic tutorials about functions,arrays and date function usage with php.

Now let’s take a look what we’ll do. We’ll code a simple php file that takes system date and counts 4 days on it and puts them into a html form with month, year and hour limits that we decided before.

You see, its a simple tutorial. But its bit more complex then it seems. We have to check if its over a months last day when we’re counting next 4 days. If we passed next month we’ll add next month option to form. Maybe its last 4 days in a year so we have to pass next year or we have to check if february has 28 days or not.

Anyways lets start tutorial;

First we need an array that we’ll push months day limits

$monthDays=array(31,28,31,30,31,30,31,31,30,31,30,31);

Later we need three more arrays to push 5 days,months and years. We use arrays for months and years coz we can pass next month and next year

$days=array();

$months=array();

$years=array();

Now we can check if january has 28 days or not. We use php mod math function to check.

if(date(“Y”) % 4 == 0)

$months[1]=29;

Now we need 4 variables for starting day,month,year and for day limit of a month. We’ll get system date for these variables.

$startDay=date(“d”); 

$startMonth=date(“m”);

$startYear=date(“Y”);

$monthDayLimit=$monthDays[$startMonth-1]; 

Later we’ll code three functions, first for init arrays for days,month and years. We’ll push systeam day for first day, system month for first month, and system year for first year. Second function is for filling arrays. We’ll push next 4 days and maybe we’ll push next month and next year. And third one is for printing html form on screen.

Focus on we pass arrays and pass variables to functions in php. You can ask why i use functions for this basic and simple tutorial. It doesn’t metter how hard or simple problem that we faced. A professional coder use effective methods and dividing problems into simple parts with functions. 

initArrays($days,$months,$years,$startDay,$startMonth,$startYear); 

fillArrays($days,$months,$years,$startDay,$startMonth,$startYear,$monthDayLimit); 

createForm($days,$months,$years);

Now we can code functions.

First add first values to specified arrays. (System date)

function initArrays(&$da,&$ma,&$ya,$d,$m,$y)

{ array_push($da,$d);

array_push($ma,$m);

array_push($ya,$y);

}

Second add days next 4 days and if neccesary next month and next year to specified arrays

function fillArrays(&$da,&$ma,&$ya,$d,$m,$y,$l) 

{ $i=0;

for($i;$i<4;$i++) // counting next 4 days

{ ++$d; 

if($d>$l) // if we passed new month

{ $d=1; // make day 1;

array_push($da,$d); 

if(count($ma)<2) 

{ ++$m; // add new month to array

if($m>12) // we passed next year

{ $m=1; 

++$y; 

array_push($ma,$m);

array_push($ya,$y);

}

else

array_push($ma,$m);

}

}

else 

array_push($da,$d);

}

}

Third and finally print form on screen

function createForm(&$da,&$ma,&$ya) // html formun oluşturulduğu kısım.
{    $i=0; $k=0; $p=0;   
   
    echo ‘<form method=”post” action=”zaman.php”>
            <select name=”gun”>’;
            for($i;$i<count($da);$i++)
                echo ‘<option value=”’.$da[$i].’” >’.$da[$i].’</option>’;
    echo ‘</select>
        <select name=”ay”>’;
            for($k;$k<count($ma);$k++)
                echo ‘<option value=”’.$ma[$k].’” >’.$ma[$k].’</option>’;
               
    echo ‘    </select>
            <select name=”yil”>’;
            for($p;$p<count($ya);$p++)
                echo ‘<option value=”’.$ya[$p].’” >’.$ya[$p].’</option>’;
               
    echo ‘</select>
            <select name=”saat”>
            <option value=”00”>00</option>
            <option value=”03”>03</option>
            <option value=”06”>06</option>
            <option value=”09”>09</option>
            <option value=”12”>12</option>
            <option value=”15”>15</option>
            <option value=”18”>18</option>
            <option value=”21”>21</option>
            </select>
            <input type=”submit” value=”gönder” />
            </form>’;

    }


Now we learnt how to use arrays, functions and date function in php. And also we passed parameters arrays to functions.
I hope it was a useful tutorial for newbies on php.

  1. tyln posted this