This package implements some PHP functions by Golang. Please note that it's impossible to have 100% the same behaviour between PHP and Golang functions because their mechanisms are quite different.
Minimum go version requirement: 1.18
, if you are using lower version, please choose version v0.62
.
PHP function | Golang function |
---|---|
abs | Abs |
base_convert | BaseConvert |
bindec | Bindec |
decbin | Decbin |
dechex | Dechex |
hexdec | Hexdec |
decoct | Decoct |
octdec | Octdec |
ceil | Ceil |
floor | Floor |
pi | Pi |
mt_rand | MtRand |
rand | Rand |
round | Round |
PHP function | Golang function |
---|---|
getcwd | Getcwd |
chdir | Chdir |
scandir | Scandir |
PHP function | Golang function |
---|---|
getimagesize | GetImageSize |
getimagesizefromstring | GetImageSizeFromString |
PHP function | Golang function |
---|---|
gethostbyaddr | GetHostByAddr |
gethostbyname | GetHostByName |
gethostbynamel | GetHostByNamel |
gethostname | GetHostName |
ip2long | IP2Long |
long2ip | Long2IP |
PHP function | Golang function |
---|---|
json_decode | JSONDecode |
json_encode | JSONEncode |
PHP function | Golang function |
---|---|
random_bytes | RandomBytes |
PHP function | Golang function |
---|---|
getenv | Getenv |
putenv | Putenv |
memory_get_usage | MemoryGetUsage |
go get github.com/hyperjiang/php
import (
"fmt"
"github.com/hyperjiang/php"
)
// Date/Time functions
fmt.Println(php.Strtotime("2017-07-14 02:40:00")) // output: 1500000000
fmt.Println(php.Strtotime("2017-07-14T10:40:00+08:00")) // output: 1500000000
fmt.Println(php.Date("Y-m-d H:i:s", 1500000000)) // output: 2017-07-14 02:40:00
fmt.Println(php.Date("c", 1500000000)) // output: 2017-07-14T02:40:00+00:00
// String functions
str := "abcdef"
fmt.Println(php.Substr(str, 1, 0)) // bcdef
fmt.Println(php.Substr(str, 1, 3)) // bcd
fmt.Println(php.Substr(str, 0, 4)) // abcd
fmt.Println(php.Substr(str, -1, 1)) // f
fmt.Println(php.Substr(str, 0, -1)) // abcde
// Math functions
fmt.Println(php.Round(5.055, 2)) // 5.06
// Array functions
arr := []string{"1", "1", "2", "3", "a", "ab", "abc", "abc", "abc", "Abc"}
fmt.Println(php.ArrayUnique(arr)) // [abc Abc 1 2 3 a ab]
fmt.Println(php.InArray("a", arr)) // true
For more usage you can find it out from test files.