Pages

Monday, April 27, 2015

conditional example in PHP

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test5</title>
</head>
<body>
 
   <?php


        // if else

        function check($your_number){
       
            if ($your_number < 50){
                return "too small";
            } else if ( $your_number <100) {
               return "medium";
            } else{
                return "too much babes";
            }
         
        }


        echo check(20);  echo "<br>";  //too small
        echo check(60);  echo "<br>";  //medium
        echo check(120);                   //too much babes





        // switch, break

        $grade = 'D';
         
         
            switch ($grade){
                case "A":
                echo " you got A grade";
                break;
         
                case "B":
                echo " not bad";
                break;
         
                case "C":
                echo "hmm not good";
                break;
         
                case "D":
                echo " holy sheet what a noob";
                break;
         
         
            }      // holy sheet what a noob


           
            //while
            $i=1;
            while ($i<=100){
                echo $i;
                echo "<br>";
                $i++;
            }   //  1 - 100

            //do while
            $iteration=101;
            do {echo $iteration;
                $iteration++;
                // kode bakal dieksekusi minimal sekali walaupun salah
               }
            while ($iteration <100);         // 101


            //for

            for ($jumlah=1;$jumlah<=5;$jumlah++){
                echo $jumlah;
                echo "<br>";
            }    // 1 - 5



            //foreach
            $array1 = array("satu","dua","tiga","empat");

              foreach ($array1 as $variabel){
                echo "$variabel <br>";          
              }   // satu dua tiga empat




    ?>
 
</body>
</html>

function to manipulate string example in PHP

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>php 3 ex</title>
</head>
<body>
 
 
   <?php
    // strlen() return length of string
    // str_word_count()  return character count of string
    // strrev()     return reverse of string
    // strpos()     search text from a string, return position of char. if not found return false
    // str_replace()  replace character in string


    echo strlen("toro tero");    // 9
    echo str_word_count("toro tero mgi");  //3
    echo strrev("gokil");    //likog
    echo strpos("hello guys im a developer", "develop");  //16
    echo str_replace ("here","over","hey you there please come here"); //hey you tover please come over


    ?>
 
</body>
</html>

php type data examples

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>test php2</title>
</head>
<body>
   
    <?php
    $x =1000;
    $y = "toro";
    $z = 123.231;
    $anArray = ["toro",1,2,3,4];
    $booleans = true;
    $kosong = null;



    //object

    class Human {
       function Human(){
            $this -> nama = "toro";  // set constructor
       }  
    }


 

php syntax & structure test

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>php</title>
</head>
<body>
    <?php
    echo "helo";
        $globVar =100;  //global scope
        $global2 = 10;



     
        function sum(){
            echo 5+2;
        }

        function tambah($x,$y){
            echo "<br>";
            echo $x +$y;
            //echo "global var". $globVar;   -> bakal error. butuh keyword global
            return $x +$y;
        }

Installing laravel

Laravel needs composer to manage some dependencies. So we need to go to composer  web first. And follow some instruction. If you find some issues when installing on windows platform try this solusion.

After composer installed, open command prompt on windows.

   start > cmd.exe > composer


if installation success you will find pictures like below when u type composer on command prompt.

composer instalation
composer install success



issues when installing composer

unable to find the socket transport ssl did you forget to enable it when you configured php


i recently found issues when installing composer.

Composer is required to install laravel (php framework).

unable to find the socket transport ssl did you forget to enable it when you configured php/

the issues above spawn when i'm trying to install composer with wampp at localhost. using apache version 2.2.2. To fix the problem we need to change php.ini configuration. usually found at wamp folder installation.

Mine is at C:\wamp\bin\php\php5.3.13\

find php.ini. open with text editor, find this line

;extension=php_openssl.dll

delete that semicolon into

          extension=php_openssl.dll


save, retry installation. done. cheers :)


Understanding pointer in C programming language

**dynamic memory allocation

manage memory.

get block of memory of any size and control using pointer.

pointer = location in memory.



ex:

#include <stdio.h>

main(){
int angka;
int* pointer;



angka = 5;


pointer = &angka;  //  & = alamat memory dari.


  printf( "angka:  %i\n", angka ); // 5
  printf( "alamat memory dr angka", &angka ); // 0xbfffc5c
  printf( "alamat pointer", pointer ); // 0xbfffc5c

  printf( "value pointer: %i\n", *pointer ); // 5
}



======
decimal vs hexadecimal


Decimal has 10 possible values for each digit:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9

Hexidecimal has 16 possible values for each digit:                
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, a, b, c, d, e, f


0xbfffc5c

0x -> prefix / menandai sebuah bilangan hexadecimal.  bffc5c -> angkanya
======

int* pointer  -> pointer declaration

*pointer ->  get pointer value.



======

** dynamic memory.

understanding memory allocation in C programming language

int Human    ->   4 bytes of memory.


memory bisa terdapat di 2 tempat :   data segment   /   stack.


jika dideklarasikan diluar function ( global var ), masuk ke data segment.
Selama program dijalankan , memory tetap akan digunakan.


stack ->  berisi variabel yang terdapat didalam function.
 sifatnya temporary.
 setelah selesai digunakan, memory dibebaskan dan dapat digunakan oleh program / function lain.


======

#include <stdio.h>

int number_of_worker = 10;


void addWorker(){
int stackWorker = 3;  -> memory berada di stack.
number_of_worker += stackWorker;
}


main(){
printf ("there is %i number of worker", number_of_worker);
addWorker();
printf ("there is %i number of worker", number_of_worker);
addWorker();
printf ("there is %i number of worker", number_of_worker);
}


c programming basic documentation

Basic C Concepts


**compiler creates useable programs from C source
**typed variables kind of data that a variable contains
**typed functions the kind of data returned from a function
**header files (.h) declare functions and variables in a separate file
**structs groups of related values
**enums lists of predefined values
**pointers aliases to other variables



compiled to binary program ( faster than scripting ).
need to porting to other os in more complex program.


without pointer C is identically to php




cth program  : hello.c

#include <stdio.h> main () { printf ("I'm a C program\n"); }

$ gcc hello.c -o hello
$ ./hello



=====
** typed variables.


in scripting, var can be declare and change to one or other type freely.

ex: $variable = 2; $variable = 1.618; $variable = 'A';

in c, we must declare the type of data first!

int variable1 = 2; float variable2 = 1.618; char variable3 = 'A';