In PHP, using the $_SERVER variable is the easiest way to get the full URL.
Using $_SERVER Variable
$_SERVER is an array containing information such as headers, paths, and script locations.
The following example should cover whatever you are trying...
In PHP, there are 3 ways to delete an element from an array.
Using unset Function
The unset(mixed $var, mixed ...$vars): void function destroys the specified variables. For example,
$arr = ["a", "b", "d"];
unset($arr[1]);
print_r($arr);
Arr...
In Golang, using the len function is the easiest way to count the items in the map structure.
Using len Function
The built-in function len take arguments of various types and return a result of type int. The implementation guarantees that the result...
In Python, there are 3 ways to reverse a string.
Using slicing
Slice notation takes the form [start:stop:step].
#!/usr/bin/python3
s = "Hello World"
print(s[::-1])
# syntax
# a[start:stop] # items start through stop-1
# a[start:] # items...
In Python, there are 2 ways to check if an object is of a given type.
Using isinstance Method
The isinstance(object, classinfo) method returns True if the object argument is an instance of the classinfo argument, or of a (direct, indirect, or virtua...
In Rust, using the position function is the easiest way to find the index of an element in a vector.
Using position Method
The position() method searches for an element in an iterator, returning its index.
position() is short-circuiting; in other ...
In Rust, there are 2 ways to index a string.
Using as_bytes Function
If the enum is C-like, then you can create a static array of each of the variants and return an iterator of references to them:
fn main() {
let s = "abc";
let b: u8 = s.as...
In Rust, there are 2 ways to iterate through the values of an enum.
Using a static array
If the enum is C-like, then you can create a static array of each of the variants and return an iterator of references to them:
use self::Direction::*;
use...
In Golang, there are 2 ways to unpack array as arguments.
Using Variadic Functions
Variadic functions can be called with any number of trailing arguments. Here’s a function that will take an arbitrary number of ints as arguments. For example,
p...
In Golang, there are 3 ways to check the equality of two slices.
Using bytes.Equal Function
Equal reports whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice.
For example,
package main...
In Golang, there are 3 ways to get a slice of keys from a map.Using For Range
The easiest way to get keys of map in Golang:
package main
import "fmt"
func main() {
m := map[string]int{
"a": 1,
"b": 1,
"c": 1,
}
// minimize ...
In Golang, there are 2 ways to convert byte array to string.Using string Function
The easiest way to convert []byte to string in Golang:
package main
import "fmt"
func main() {
b := []byte{'a', 'b', 'c'}
s := string(b[:])
fmt.Printf("%...
In Python, there are 3 ways to split a list into equally sized chunks.
Using List Comprehension
List comprehensions provide a concise way to create lists. The following example:
#!/usr/bin/python3
# -*- coding: utf8 -*-
# initialize
lst = ['a'] *...
In Rust, there are 3 ways to convert string to json.
Using Serde JSON
Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.
Adding dependencies
In Cargo.toml file we’ll add this information.
[de...
3 ways to initialize a Python Array .Initialize a list with values
If you want to initialize a list of any number of elements where all elements are filled with any values, you can use the * operator as follows.
#!/usr/bin/env python3
arr = ['H'] *...