Konfigurasi dan setup VIM editor sebagai IDE PHP di Ubuntu Linux

Pada post kali ini saya mencoba mencatat pengalaman saya dalam mengkonfigurasi Vim untuk saya gunakan sebagai IDE development tool PHP, Saya sendiri masih menggunakan Atom IDE untuk development sehari hari, tapi kadang kala saya malas keluar dari terminal dan menggunakan Vim untuk menyelesaikan beberapa pekerjaan yang sederhana.

Vim adalah text editor yang dibuat sebagai text editor yang fleksible dan bisa disetup sesuai dengan kebutuhan dan keperluan kita, dan pada catatan kali ini saya mencatat tentang konfigurasi dan setup Vim text editor untuk sebuah IDE pengembangan web berbasis PHP.

Instalasi Vim Plugin Manager (Pathogen dan atau Vundle)

Plugin manager yang sering digunakan Vim adalah Pathogen dan Vundle.

  1. Pathogen

    “Manage your 'runtimepath' with ease. In practical terms, pathogen.vim makes it super easy to install plugins and runtime files in their own private directories.”

    • Instalasi
      1. mkdir -p ~/.vim/autoload
      2. mkdir ~/.vim/bundle
      3. curl -LSso ~/.vim/autoload/pathogen.vim https://tpo.pe/pathogen.vim1.Instal-pathogen.png
      4. Tambahkan baris berikut ke .vimrc, bila belum punya file .vimrc bikin saja dulu yang baru di ~/.vimrc tau copy yag suda ada dengancp /usr/share/vim/vimrc ~/.vimrc2-create-vimrc
      5. Nah sekarang kita sudah memiliki ~/.vimrc, mari kita kembali ke proses instalasi pathogen dengan menambahkan bari berikut ke file .vimrc kita:execute pathogen#infect()3.infect vimrc.png
      6. Sekarang plugin-plugin yang ingin dinginstal dapat diekstrak ke subdirektori di bawah ~/.vim/bundel, dan mereka akan ditambahkan ke ‘runtimePath’. perhatikan contoh berikut, kita menjadikan instalasi vim-sensible  sebagai contoh pertama:
        1. cd ~/.vim/bundle
        2. git clone https://github.com/tpope/vim-sensible.git

          5-coba-install-sensible

  2. Vundle

    Vundle is inspired by Pathogen, it can do everything pathogen can do, it is compatible with pathogen, which means if a plugin declares it supporting pathogen, it supports vundle. Launch vim and run :BundleInstall , everything will be ready for you.

    • Instalasi
      1. cd ~/.vim/bundle
      2. git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim6-instalvundle
      3. Edit .vimrc dengan menambahkan perintah berikut di bagian paling atas
        set nocompatible             
        filetype off                  
        
        " set the runtime path to include Vundle and initialize
        set rtp+=~/.vim/bundle/Vundle.vim
        call vundle#begin()
        " let Vundle manage Vundle, required
        Plugin 'VundleVim/Vundle.vim'  
        " semua penambahan disini
        " All of your Plugins must be added before the following line
        call vundle#end()            " required
        filetype plugin indent on    " required
      4. untuk mengetes kita coba install command-t.git, dengan menambahkan line di bawah ini di ~/.vimrc kita:
        Plugin 'git://git.wincent.com/command-t.git'

        8-install-plugin-via-vundle

      5. Lalu execute perintah :PluginInstall9-plugin-installed
      6. Untuk melihat plugin yang terinstall gunakan :PluginInstall
      7. Nah, sekarang t iba waktunya kita menghapus Vim Plugin yang tadi kita install, caranya hapus saja line yang tadi kita tambahkan di vimrc
        • Dari :8-install-plugin-via-vundle
        • Jadi :10.PLUGINKOSONG.png
        • Lalu tinggal :PluginUpdate
        • 11-pluginupdate
        • 12.tampilandone.png
        • 12.removedplugin.png
  3. Konfigurasi Vim untuk PHP Development dengan Vundle

    1. Install PHP Syntax Highlighter StanAngeloff/php.vim13-php-vim

      • " Put at the very end of your .vimrc file.
        
        function! PhpSyntaxOverride()
          hi! def link phpDocTags  phpDefine
          hi! def link phpDocParam phpType
        endfunction
        
        augroup phpSyntaxOverride
          autocmd!
          autocmd FileType php call PhpSyntaxOverride()
        augroup END
      • lalu :PluginInstall
      • Hasilnya:
      • 14.php.vim.in.action.png
    2. Instalasi PHP linter joonty/vim-phpqa

      15.php-linter-install.png

      • lalu :PluginInstall
      • Hasilnya:
      • 16-php-code-linter
      • Shortcut:
        • Tambahkan line berikut di .vimrc supaya codesniffer bisa di toggle dengan ctrl+shift+s.
          autocmd FileType php map <C-S-s> :Php<CR>
          autocmd FileType php map <C-S-a> :Phpcs<CR>
    3. Instalasi Tree Explorer scrooloose/nerdtree

      17-nerdtree

      • Hasilnya
      • 19-nerdtree
      • bila mau close semua termasuk nerdtree gunakan :qa!
      • Shortcut:
        • Tambahkan line berikut di .vimrc supaya bisa di toggle dengan ctrl+\ seperti di atom.
          
          
          map <C-n> :NERDTreeToggle<CR>
    4. Instalasi Grepper vim-grepper

      • Hasilnya:
    5. Instalasi ctrlpvim/ctrlp.vim untuk mencari file dengan fuzzy finder31-ctrpvim.png
    6. sdf
  4. asdasd
  5. Cek vimrc file saya di github: https://github.com/yoesoff/vimrc

Golang Iteration/loop: Break & continue


package main

import "fmt"

var pows = []int{1, 2, 4, 8, 16, 32, 64, 128}
var users = []string{"yuna", "yuni", "yuka", "deisy", "Miyem", "Jhony", "Merry", "Toni"}

func main() {
	for x, value := range pows {

		fmt.Println(x, ":", value)

		if value >= 16 {
			// After x is greater or equal to 16 stop the iterate
			break
		}
	}

	for x, value := range pows {

		fmt.Println(x, ":", value)

		if value >= 16 {
			continue
		}

		// After x is greater or equal to 16 do  not print this
		fmt.Println("------")
	}

}

Golang Output

0 : 1

1 : 2

2 : 4

3 : 8

4 : 16

 

0 : 1

——

1 : 2

——

2 : 4

——

3 : 8

——

4 : 16

5 : 32

6 : 64

7 : 128

Success: process exited with code 0.

Golang Data Collection: “Foreach” iterates through all entries of an array using range

A “for” statement with a “range” clause iterates through all entries of an array, slice, string or map, or values received on a channel. For each entry it assigns iteration values to corresponding iteration variables and then executes the block.

there is no foreach in Golang, But using a for statement with range clause we can iterate through all entries of an array, slice, string or map, or values received on a channel.

The following example shows how to use the range operator in a for loop to implement a foreach loop in Golang:


package main

import "fmt"

var pows = []int{1, 2, 4, 8, 16, 32, 64, 128}
var users = []string{"yuna", "yuni", "yuka", "deisy", "Miyem", "Jhony", "Merry", "Toni"}

func main() {
	for x, value := range pows {
		fmt.Println(x, ":", value)
	}

	for x, user := range users {
		fmt.Println(x, ":", user)
	}
}

Go Output

0 : 1

1 : 2

2 : 4

3 : 8

4 : 16

5 : 32

6 : 64

7 : 128

0 : yuna

 

1 : yuni

2 : yuka

3 : deisy

4 : Miyem

5 : Jhony

6 : Merry

7 : Toni

Success: process exited with code 0.

gommu1.png

Golang: Collection

In this post I write down my experience regarding data collection in Golang, basically collection  is a grouping of some variable number of data items (Examples of collections include lists, sets, multisets, trees and graphs.).

Golang Array Example:

package main

import "fmt"

var (
	a [3]string
)

func main() {

	a[0] = "Hi"
	a[1] = "little"
	a[2] = "Buddy"

	fmt.Println(a[0], a[1])
	fmt.Printf("%s %s %s \n", a[0], a[1], a[2])
	fmt.Println(a)
}

Go Output:

Hi little

Hi little Buddy

[Hi little Buddy]

Golang Multi-dimensional arrays by example:


package main

import "fmt"

func main() {
	var a [2][3]string
	for i := 0; i < 2; i++ {
		for j := 0; j < 3; j++ {
			a[i][j] = fmt.Sprintf("row %d - column %d", i+1, j+1)
		}
	}

	for i := 0; i < 2; i++ {
		for j := 0; j < 3; j++ {
			fmt.Println(a[i][j])
		}
	}

	fmt.Println("------------------------")

	fmt.Printf("%q", a)
}

Go Output:

row 1 – column 1

row 1 – column 2

row 1 – column 3

row 2 – column 1

row 2 – column 2

row 2 – column 3

————————

[[“row 1 – column 1” “row 1 – column 2” “row 1 – column 3”] [“row 2 – column 1” “row 2 – column 2” “row 2 – column 3”]]

Set the array entries  while declare the array


package main

import "fmt"

func main() {

	a := []string{"aaaaa", "bbbbb"}
	a = append(a, "ccccc")

	fmt.Println(a[0]) // Index 0
	fmt.Println(a[1]) // Index 0
	fmt.Println(a[2]) // Index 0

	fmt.Printf("%s\n", a) // Print as string
	fmt.Printf("%q", a)   // Print as array collection
}

Go Output:

aaaaa

bbbbb

ccccc

[aaaaa bbbbb ccccc]

[“aaaaa” “bbbbb” “ccccc”]

Common errors related array in Golang:

  • Assign value to the undefined / invalid array index

./test.go:14: invalid array index 3 (out of bounds for 3-element array)

Error: process exited with code 2.

  • Assign value to undefined variable

./test.go:13: undefined: a

Error: process exited with code 2.

  • Index out of range

Just trying to define the array to be like this:


var (
	a []string
)

And the result is like this:

panic: runtime error: index out of range

goroutine 1 [running]:

panic(0x45afa0, 0xc42000a1e0)

/usr/local/go/src/runtime/panic.go:500 +0x1a1

main.main()

/home/yusuf/go-learn2/src/github.com/yoesoff/test_ide/test.go:9 +0x3a

Error: process exited with code 2.

Slicing a slice


package main

import "fmt"

func main() {
	mySlice := []int{11, 12, 13, 14, 15, 16}

	fmt.Println(mySlice[0])   // 11
	fmt.Println(mySlice[0:3]) // [11, 12,13]
	fmt.Println(mySlice[0:2]) // [11, 12]

	// Having same output with above code
	fmt.Println(mySlice[0])  // 11
	fmt.Println(mySlice[:3]) // [11, 12,13]
	fmt.Println(mySlice[:2]) // [11, 12]

	fmt.Println(mySlice[0:]) // [11 12 13 14 15 16]
	fmt.Println(mySlice[1:]) // [12 13 14 15 16]
	fmt.Println(mySlice[2:]) // [13 14 15 16]
	fmt.Println(mySlice[3:]) //[14 15 16]

}

Go Output:

11

[11 12 13]

[11 12]

11

[11 12 13]

[11 12]

[11 12 13 14 15 16]

[12 13 14 15 16]

[13 14 15 16]

[14 15 16]

Success: process exited with code 0.

Another Slicing  slice example


 package main

import "fmt"

func main() {
	mySlice := []int{11, 12, 13, 14, 15, 16}

	fmt.Println(mySlice[0:6])
	fmt.Println(mySlice[0:5])
	fmt.Println(mySlice[0:4])
	fmt.Println(mySlice[0:3])
	fmt.Println(mySlice[0:2])
	fmt.Println(mySlice[0:1])

	fmt.Println(mySlice[5:6])
	fmt.Println(mySlice[4:6])
	fmt.Println(mySlice[3:6])
	fmt.Println(mySlice[2:6])
	fmt.Println(mySlice[1:6])
	fmt.Println(mySlice[0:6])
}

Go Output:

[11 12 13 14 15 16]

[11 12 13 14 15]

[11 12 13 14]

[11 12 13]

[11 12]

[11]

[16]

[15 16]

[14 15 16]

[13 14 15 16]

[12 13 14 15 16]

[11 12 13 14 15 16]

Appending to a slice


package main

import "fmt"

func main() {
	// Code below won't work
	//	cities := []string{}
	//	cities[0] = "Santa Monica"

	// This code is working well
	cities := []string{}
	cities = append(cities, "Santa Monica")
	fmt.Println(cities)
}

Go Output:

[Santa Monica]

Success: process exited with code 0.

Append more


//append more
cities = append(cities, "San Diego", "Mountain View", "Jakarta", "Bali", "Trenggalek")
fmt.Printf("%q", cities)

Go Output:

[“Santa Monica” “San Diego” “Mountain View” “Jakarta” “Bali” “Trenggalek”]

Success: process exited with code 0.

check the length of a slice by using len()

cities = append(cities, "San Diego", "Mountain View", "Jakarta", "Bali", "Trenggalek")
fmt.Printf("%d", len(cities))

Go Output:

5

Another  exciting Example about slice

Go slices, usage and internals
Effective Go – slices
Append function documentation
Slice tricks
Effective Go – slices
Effective Go – two-dimensional slices
Go by example – slices

Common errors related slice in Golang:

  • slice bounds out of range

panic: runtime error: slice bounds out of range

goroutine 1 [running]:

panic(0x47a8e0, 0xc42000a1f0)

/usr/local/go/src/runtime/panic.go:500 +0x1a1

main.main()

/home/yusuf/go-learn2/src/github.com/yoesoff/hello/hello.go:20 +0x53

Error above caused by this:

mySlice := []int{11, 12, 13, 14, 15, 16}

fmt.Println(mySlice[3:12]) // [14 15 16 undefined …]

  • slice bounds out of range

panic: runtime error: slice bounds out of range

goroutine 1 [running]:

panic(0x47a8e0, 0xc42000a1f0)

/usr/local/go/src/runtime/panic.go:500 +0x1a1

main.main()

/home/yusuf/go-learn2/src/github.com/yoesoff/hello/hello.go:8 +0x53

Error: process exited with code 2.

mySlice := []int{11, 12, 13, 14, 15, 16}

fmt.Println(mySlice[0:7000])  // max is 6

PHP: Test the value of a variable using isset() vs empty() vs is_null()

Test the value of a variable using  isset() vs empty() vs is_null():

	$xx = '';
	$yy = [];
	$zz = null;

	echo "
<h2> is_null()</h2>
";

	var_dump(is_null($xx)); // False

	var_dump(is_null($yy)); // False

	var_dump(is_null($zz)); // True

	echo "
<h2> empty()</h2>
";
	var_dump(empty($xx)); // True

	var_dump(empty($yy)); // True

	var_dump(empty($zz)); // True

	echo "
<h2> isset()</h2>
";

	var_dump(isset($xx)); // True
	var_dump(isset($yy)); // True
	var_dump(isset($zz)); // False

Result of code

is_null()

bool(false) bool(false) bool(true)

empty()

bool(true) bool(true) bool(true)

isset()

bool(true) bool(true) bool(false)

Golang: The easy way to understand Golang Pointer thru example

Pointers reference a location in memory where a value is stored rather than the value itself. (They point to something else)

I write down my learning experiences about  Golang’s Pointer using easy way, here is my note:

The first step we have to understand  & and * operators:

 & and * Operators :

package main

import "fmt"

func do(x *int) {
	*x = 99 // Update x value
}

func main() {
	x := 5

	do(&x) // pass address of x

	fmt.Println(x) // The value updated through pointer
}

Output:

99

More example about pointer and operators

package main

import "fmt"

func do(x *int) {
	*x = 99
}

func main() {
	x := 5

	y := &x

	fmt.Println(&x) // Address of x
	fmt.Println(y)  // Address of x
	fmt.Println(&y) // Address of y that stores x Address

	do(y)

	fmt.Println(x)
}

Output:

0xc42000a3b0 // Address of x

0xc42000a3b0 // Address of x

0xc420032020 // Address of y that stores x Address

99  // x new value after updated thru pointer

Print x value thru pointer y:


package main

import "fmt"

func do(x *int) {
	*x = 99
}

func main() {
	x := 5

	y := &x

	fmt.Println(*y)
}

Output:

5

Another example: https://tour.golang.org/moretypes/1

Laravel 5 Pagination: Multiple Paginations in the same Page

In this note I write down my experience regarding multiple paginationin the same page using Laravel 5:

In your laravel controller:


//  Program Degree
$object1s = Object1::orderBy('name', 'desc')
                    ->paginate(2, ['*'], 'page_1s');

// Master Program
$object2s = Object2::orderBy('name', 'desc')
                    ->paginate(2, ['*'], 'page_2s');

// Online Course
$object3s = Object3::orderBy('name', 'desc')
                    ->paginate(2, ['*'], 'page_3s');

In your laravel view:

  // Nothing to change in the view

Now you will have following paginated urls in your application:

  1. http://localhost:8000/yourpage?page_1s=2
  2. http://localhost:8000/yourpage?page_2s=2
  3. http://localhost:8000/yourpage?page_3s=2
  4. for special purpose you can custom url to handle all paginations in same time become:
    1. http://localhost:8000/yourpage?page_1s=2&page_2s=3&page_3s=4