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


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




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

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


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.





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. 




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);




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;



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

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

{ $m=1; 













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”>’;
                echo ‘<option value=”’.$da[$i].’” >’.$da[$i].’</option>’;
    echo ‘</select>
        <select name=”ay”>’;
                echo ‘<option value=”’.$ma[$k].’” >’.$ma[$k].’</option>’;
    echo ‘    </select>
            <select name=”yil”>’;
                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>
            <input type=”submit” value=”gönder” />


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