Software Engineering, Hacks and Solutions

Do you know the situation where you want to test a little change of a pre-tested code “on the fly” and recognize that it does not work any more? I had this situation several times. Therefore, I decided to create a “flood style” blog post where I want to summarize those moments. This “flood type” post should help to remember those situations in order to safe time and help others as well. Enjoy it!

PHP and file handling

There are two main ways to handle files with PHP. The first one are the f_* methods and the second one is a kind of summary of them. Lets start with the first one:

“a+” opens the file (creates if it does not exist) and appends the strings “string1” and “string2” to the end of “test.txt”.

feof() returns true when $handler points to the end of the file. fgets() reads the string and and moves the pointer.

There are a bunch of other file methods in PHP. For example, fpassthru(), fread() and finfo_file().

The second way consists of two methods that serve as a wrapper method for the methods above, like:

Both methods have more parameters, like passing a context resource, but they are optional.

But be aware of using the second way. file_get_contents() reads the whole content into the memory. While it is okay when you handle smaller files, it is definetly better to use the methods of the first way when reading larger files.

Reading and Writing from/to the console

The above demonstrated file handling creates so-called streams or resources. The fopen() method can also be used for reading/writing from/to the console or FTP, like:

PHP does not execute a PDO statement

You have a SQL query which works well in your SQL editor. You want to execute it with PHP and it does nothing – literally. Well, you have maybe a typo in your statement’s prepare and bind code and want to bind parameters that does not exist in your SQL code:

 

Do you see the difference? In the statement string, there is a parameter “:status”. But I want to bind a parameter “stats” which fails. Unfortunately, PDO does not throw an exception or warning! PHP dies silently in this case. There is no exception and no errors (at least not in my environment). So, remember:

PHP has no HTTP Basic Auth credentials

Wondering why $_SERVER[‘PHP_AUTH_USER’] is not set? Maybe you are using Apache in CGI/FastCGI mode. If you can not change it, add the following command to your apache conf:

Converting Bits to Bytes / Megabytes / Gigabytes

Do you remember how to convert bits to bytes, megabytes or gigabytes? Well, it was a good task for me to refresh this:

1 Byte 8 Bit
1 Kilobyte 1024 Byte
1 Megabyte 1024 Kilobyte
1 Gigabyte 1024 Megabyte
1 Terabyte 1024 Gigabyte
1 Petabyte 1024 Terabyte

Ok, so that is, if you have a 32 bit integer, you have 232 (or 4,294,967,296) distinct integers. A string character has 4 bytes. Therefore, the string “dogan” has 5 bytes.

If you have 1GB (or 8,589,934,592 bits) space, you can fit the 32 bit integers twice. The string “dogan” would fit 214,748,365 times in 1 gig 😁

Recursive Problem Solution

Solving recursive problems is an art in itself and requires a seperate blog post 🙂 However, I want to summarise a bit.

Before you start to think about the problem, STOP trying to capture the whole problem! You will definetly fail. It is definetly not possible to keep all the branches in your mind. Instead, do the following:

  1. Think about a base case/break condition
    • which condition has to be true in order to stop the recursion? This is usually something like “a collection has no more elements” or “x is greater than y”
  2. Do the desired operation
    • usually something like add one more element from the input to the result
  3. Call the method with different methods with different parameters
    • it is urgent that you call the method with different parameters since otherwise the method will do the same operations again and again. This will result in an infinite loop
    • different parameters means usually something like increasing a counter or slicing a collection

You will usually have an array or an object as your result (step 2). Try to pass the result object as an reference to each recursive call. Doing this, you do not need to merge the result value of each recursive call with your already available result. Further, you are operating on the same object and do not allocate memory on each call.

Get the difference of two arrays

Imagine you have two arrays and they are structured with an integer as the key and a boolean as the value (HTML checkboxes, for example). You want to know the difference between two arrays: Are there new key-value pairs? Do any key’s value changed? How do you do that? Here is my solution:

and the result:

Getting the second highest value in an array

I am a little bit proud about what the following code does 😄https://gist.github.com/doganoo/f2d7bc6533313b665c97db12ff5c97d8

Does array contain a sum

Sometimes we just need to know whether a (numeric) array can build with its given numbers a target sum. Let’s imagine we have the array [10,20,30,40,50] and the target sum 100. We can see that 10 + 40 + 50 and 20 + 30 + 50 equal both to 100. The following code demonstrates this:

Bootstrap Modal does not open a second time

The following code does not execute twice (after you have opened and closed the modal once and want to open it again):

If you get the following error in your JS console:

 

TypeError: $(…).modal is not a function with bootstrap Modal

It could possibly because you include all your JS stuff twice as stated here.

Throttling and the idea behind

Have you ever asked yourself how Facebook and/or Twitter load their timelines dynamicly without having any performance issues (Well, we’ll forget out all the servers around the world)? You are scrolling through your timeline and the content loads usually within a few seconds. How is that possible?The answer is “throttling”: On the web UI, any JavaScript code listens for an event (scrolling, clicking, etc). But each scroll event fires an action (content loading) which would cause many requests within a small amount of seconds. You can limit this by setting a timeout, like:

See the full example on GitHub.

Some keywords

I hear and read a couple of keywords repeadeatly. I want to summarize them here because most of them are just “fresh and fancy” buzzwords:

  • Polling: a process that tests repeadetly whether a condition is true or not. In computer science, it would be a threat/process that runs after defined time units.

Leave a Comment