Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

Saturday, September 5, 2015

function for min number from array - manually

<?php

function min_number($array){
    $min_number = $array[0];
    $length = count($array);
    for($i = 0; $i < $length; $i++){
        if($min_number > $array[$i]){
           $min_number = $array[$i];
        }
    }
    return $min_number;
}

Sunday, August 30, 2015

findings frequency number from a file by using own explode code and frequency code - modularity approach

in functions.php
===========================

<?php
//for exploding a string to array
function myExplode($string)
{
    $length = strlen($string);
    $word = "";
    $words = array();
    for ($i = 0; $i < $length; $i++) {
        if (ord($string[$i]) == 13) {
            continue;
        }
        if ($string[$i] == " " || $i == $length - 1 || $string[$i] == "\n") {

            if ($i == $length - 1) {
                $word .= $string[$i];
            }
            if(!empty($word)){
                $words[] = $word;
            }
            $word = "";
        } else {
            $word .= $string[$i];
        }
    }
    return $words;
}




//for determine frequency
function frequency($words){
    $frequency = array();

    foreach ($words as $word) {
        if(array_key_exists($word, $frequency)){
            $frequency[$word] ++;
        } else {
            $frequency[$word] = 1;
        }
    }

    return $frequency;
}


in file.php
================

<?php
require 'functions.php';
$pointer = fopen('php.txt', 'r');
$string = fread($pointer, filesize('php.txt'));
$words = myExplode($string);
$frequency = frequency($words);
print_r($frequency);

css snippet for blogger code highlighting

code, .code {     display: block;     background: beige;     padding: 10px;     margin: 8px 15px; }