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!

Do we really need to use pointers?

Did a little digging on whether it's useful to make use of pointers in your application

The topic about pointers isn't new as you're probably introduced to this concept in your first computer science class. I started learning Go and once again, I came across the concept of using Pointers and wanted to finally understand, why are they used and do we really need to use it?

Frankly, I don't really remember using pointers when I was developing web applications in PHP and JavaScript but it was quite a refresher to get back on track again.

Well, what is a pointer?

In it's basic form, a pointer is a variable that contains the memory address of another variable.

For example, you have a variable named x that contains some data, in this case, we'll store an integer:

package main

import "fmt"

func main() {
    x := 10
    fmt.Println(x) // prints 10
}

The code above prints the variable x by referring to it's name. We can do the same using pointers by referring to it's memory address via another variable that points to variable x.

To do this, we must use & operator to get the address and * to return the value stored in the address of variable x like this:

package main

import "fmt"

func main() {
    x := 10
    p := &x
    fmt.Println(x)   // prints 10
    fmt.Println(*p)  // also, prints 10
}

Hmm, that was pretty straight forward.

So, why was it created?

In the modern era, most high-level programming languages are capable of handling memory allocation (like Java or C#), automatically.

But when C was developed, computers weren't as powerful as today but that also meant the programmers must create systems that made use of memory and speed, efficiently. Which is why some tasks do require it when programming low-level embedded systems or microcontrollers.

Why use them?

Whenever you pass a variable to a function, you're basically creating a copy of the variable. But if you're using pointers, you just have to pass the address of the variable and in most cases, a pointer is much smaller in size than the value being pointed at.

It allows you share data and it's quite appropriate when it comes to modifying the data being passed to it.

To some extent, it might seem efficient but there are some tradeoffs, when you're going to talk about optimization.

Do pointers really optimize your application?

I was thinking if pointers are used to optimize applications, I mean, you don't have to duplicate data and it saves memory, so why not?

Turns out that there are a few points that I came across:

  1. Accessing values can use up memory though not so much but it can add up.
  2. Data will be placed on top of heap stack which increases the memory overhead and can be cleaned up the garbage collector.

Most programmers tend to avoid using it in order to make their codebase less complex and reduce the increased work for the garbage collector.

Apart from that, there are some concerns when it comes to the security of the application implying that it could be unsafe when using pointers.

Takeaway

Pointers are useful but don't think of using it, blindly, by assuming that it might give you a boost in performance.

Hope you found this article useful!

Readings

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! 😄

Find the number of uppercase and lowercase letters in a string

The usual approach of counting the number of uppercase and lower letters in a string is done by using a counter and a loop:

var upperCount = 0;
var lowerCount = 0;
var str = "AbCdEfGhiJkL";
for(var i=0; i<str.length; i++){
    if(str[i] == str[i].toUpperCase()){
        upperCount++;
    }
    else{
        lowerCount++;
    }
}

That's nice but here's shorter and faster implementation:

var str = "AbCdEfGhiJkL";
var lowerCount = str.length - str.replace(/[A-Z]/g, '').length;
var upperCount = str.length - str.replace(/[a-z]/g, '').length;

The lowerCount variable is taking the difference of the lengths between the original string and the string with lowercase letters only because the .replace() method replaced the pattern of uppercase letters [A-Z] with empty spaces. The upperCount variable does the opposite of what the lowerCount variable does.

Hope you found this trick useful!

Replace all occurrences found on a string using Regular Expressions

Let's say you have the following string and you must replace all occurrences of "Hello" with "Bye":

var str = "Hello Hello Hello World";

You may think of using the .replace() method to solve this problem:

var newStr = str.replace("Hello", "Bye")

But unfortunately, it only replaces the first occurrence in the string:

console.log(newStr); // returns "Bye Hello Hello World" as the output.

Using the power of Regular Expressions, you can replace all occurrences in one go:

function replaceAll(str, search, replace){
    var re = new RegExp(search, "g");
    return str.replace(re, replace);
}    

Now, when you execute the following, you'll get a string that replaced all occurrences:

var newStr = replaceAll(str, "Hello", "Bye");
console.log(newStr); // returns "Bye Bye Bye World" as the output.

Until next time, then!