megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Increase memory limit in PHP

Last month, I was building a web application that collected a lot of data via form submissions. I wrote a method to export attachments and form data by zipping them all together.

It worked for a smaller archives but as the records grew larger, I got a fatal error which said that I've exhausted the PHP's memory limit.

So, I found an easy way to increase the memory limit to 1024MB (1GB) like this:

<?php
ini_set('memory_limit', '1024M');
?>

Before you write this stub in your script, make sure you have enough resources in your system or else, it'll go splat!

Increase execution time in PHP

Ever process a file that's larger than 2GB and got an error that said something like this:

Maximum execution time of 30 seconds exceeded

Though, PHP doesn't have an efficient way of processing files of large sizes, you can prevent your web application from timing out by adding this to your code:

<?php
// 300 seconds == 5 minutes
ini_set('max_execution_time', 300);
?>

Hopefully, some day, this might come in handy for you! 😜

Extract unique characters from a string

I thought of getting back into competitive programming again and started practicing my python coding-chops on Codewars.

Here's a neat trick on how to extract unique characters from a string:

from collections import OrderedDict

word = "HelloWorld"
uniq = ''.join(OrderedDict.fromkeys(word).keys())

print(uniq) # prints HeloWrd

Using OrderedDict allows you to preserve the order in which the keys are inserted as a normal dict doesn't track the order.

Hope you found this useful!

Hello, Go!

After reading a lot of articles about using Go as a programming language to write high-performing concurrent web applications and services yet maintain a clean codebase, I thought of starting to learn it to see what's it about and so far, it's been great!

This mini-tutorial will show you how to install Go on your system and write a simple "Hello, World" program.

Note: This tutorial will go through installing Go on Linux.

Install Go

Make sure you have the latest updates and upgrades on your Linux system before installing Go.

You need to download the binary file from their official package. Find the version that suits your OS and architecture.

cd /tmp
wget https://golang.org/dl/go1.14.6.linux-amd64.tar.gz

Extract the downloaded archive and install it in the /usr/local directory (as per the standards):

sudo tar -xvf go1.14.6.linux-amd64.tar.gz
sudo mv go /usr/local

Set up environment

Now, we need to set up three variables:

  • GOROOT: Location of where your Go package is installed.
  • GOPATH: Location of your work directory.'
  • PATH: Tells bash on where to look for programs that are being executed.

Open up your .bashrc file and add these lines at the end of the file:

export GOROOT=/usr/local/go
export GOPATH=$HOME/go
export PATH=$GOPATH/bin:$GOROOT/bin:$PATH

Save your .bashrc file and update the current shell session:

source ~/.bashrc

Verify your installation

Type the following command to ensure that your Go installation is successful:

go version go1.14.6 linux/amd64

Hello, world!

As per traditions, whenever you learn a programming language, you start off with a "Hello, world!" program, so here it is:

package main

import "fmt"

func main() {
    fmt.Printf("Hello, world!\n")
} 

Conclusion

I just started learning it today and at first, you might have the tendency to write code in Go the same way you write in any other language but beware, it doesn't work that way. It forces you to write good code including the way it formats the code using the gofmt tool.

Lots of cool things are coming!

Stay tuned for more.

How to delete files inside a zip file?

Ever compressed your project directory but forgot to delete that useless file or folder and turns out the compresed file is larger than it's supposed to be?

Here's a quick solution you can try:

If you want to delete a file inside a .zip file, try this:

zip --delete file.zip "file.ext"

And if you want to delete a folder, try this:

zip --delete file.zip "folder/*"

Hope this helps you out!

Find the difference between two arrays

Finding the difference between two sets i.e. Set A and Set B is basically returning a new set that contains values of Set A that don't exist in Set B or vice-versa.

Here's an example:

var a = [1,2,3,4,5];
var b = [1,1,2,3,4];

I wrote a shorter implementation using JavaScript's .filter() method:

function array_diff(a,b){
    return a.filter(i => b.indexOf(i) === -1);
}    

Once you execute this method, you'll get the following as a result:

var c = array_diff(a,b);
console.log(c) // returns [5]

BONUS: What does the .filter() method do?

This method returns a new array with elements that pass the conditions provided by a callback function.

If the conditions aren't passed, you'll receive an empty array.

In this article, (i => b.indexOf(i) === -1) is considered as the callback function in which the i refers to the index of the current element of array A and is then used as a parameter to check if the element doesn't exist in array B.

The neat thing about this method is that it doesn't mutate on the array that it's being called from.

Read more about Array.prototype.filter() on Mozilla's developer documentation.

How to duplicate tables in MySQL?

This trick comes in handy whenever you wanted to reuse a table, perform data migrations or maybe even take a backup of the table before any of your experiments mess up your data.

Executing the following query will help you create a new table with the structure of the old table:

CREATE TABLE schema.new_table LIKE schema.old_table;

If you want the data as well, try this:

CREATE TABLE schema.new_table LIKE schema.old_table;
INSERT schema.new_table SELECT * FROM schema.old_table;

You can use this query to copy tables from one schema to another schema too. Hope this helps you out! 😄

Create a unique multidimensional array using a key index

Let's say, you have a multidimensional array like this:

<?php
$cars = [
    [
        "id" => 1,
        "name" => "Mercedes Benz",
        "color" => "Black"
    ],
    [
        "id" => 2,
        "name" => "Toyota",
        "color" => "Red"
    ],
    [
        "id" => 3,
        "name" => "Toyota",
        "color" => "White"
    ],
    [
        "id" => 4,
        "name" => "Nissan",
        "color" => "Grey"
    ],
];
?>

And say, you want to be able to return unique cars by name or color, use this method:

<?php
function multi_array_unique($array, $key) {
    $i = 0;
    $temp_array = [];
    $key_array = [];

    foreach($array as $val) {
        // insert only unique keys
        if (!in_array($val[$key], $key_array)) {
            $key_array[$i] = $val[$key];
            $temp_array[$i] = $val;
        }
        $i++;
    }
    return $temp_array;
}
?>

Call this method from anywhere in your code like this:

<?php
$unique_cars = multi_array_unique($cars, "name");
dd($unique_cars);
?>

And now, you'll get something like this:

<?php
[
    [
        "id" => 1,
        "name" => "Mercedes Benz",
        "color" => "Black"
    ],
    [
        "id" => 2,
        "name" => "Toyota",
        "color" => "Red"
    ],
    [
        "id" => 4,
        "name" => "Nissan",
        "color" => "Grey"
    ],
];
?>

You might wonder why I tried this instead of PHP's array_unique() method and the reason I didn't use it is because it doesn't support multidimensional arrays.

Hope you found this useful!