Author: Catzie

  • PHP dynamic variable names tutorial

    I think it’s very convenient for us developers, being able to name our variables dynamically. I’ll show you how to do it in PHP. 🙂

    For example, we have variable $foo with “bar” as value like this:

    If we want to create a variable named based on the “value” of variable $foo, this is what we write:

    The echo should display: My variable name is bar.

    Notice that the second variable that we declared was written as $$foo with 2 dollar symbols before the variable name. This makes it a variable variable — a variable whose name is a variable. What happens is that it first gets the value of $foo, which is “bar” and then uses that value as the variable name. And so, in the example above, defining the variable as $$foo = "My variable name is bar." is equal to defining it as $bar = "My variable name is bar.".

    Making the PHP variable name more dynamic

    Now, what if you want to make your variable name more dynamic? I’ll give you another example.

    Here we are generating a random 1-digit number, we’ll use it later:

    The way that we’ll define the dynamic variables will be a bit different from the example earlier.

    And here we have an array of strings:

    What if we want to create variables whose names are based on the value of strings in the array, like $uno, $dos, and $tres?

    We do it like in the code below. Notice that I defined my dynamically named variable like ${$v} inside the foreach:

    Now let’s use the $numbers I showed you earlier. Pay attention to what I put inside the foreach.

    The foreach let us define variables whose names are $uno1, $dos2 and $tres3, which are concatenated values from arrays $numbers and $words.

    This is where the magic happens: ${$words[$x] . $numbers[$x]}

    We combined elements from $words and $numbers arrays, then turned them into variable names by enclosing them in ${}

    And that’s it! Use variable variables at your convenience. 🙂

  • PHP: get full URL of current page with GET params or without

    The following string variable’s value will get the full URL of the current page, including GET arguments/params, if present.

    To get the full URL of current page but WITHOUT the GET args/params, do this instead:

    The strtok function will return only the part of the URL string up to the character before question mark “?” which is used by GET args/params.

    strtok was suggested in this StackOverflow answer: http://stackoverflow.com/a/4643312

  • PHP: avoid backslash on json_encode

    Hey! 🙂 If you are encoding your Arrays to JSON in PHP using json_encode() and the function adds backslash before your slashes, I’m gonna show you a quick fix.

    This is my original code:

    And the result is:
    {"success":true,"message":"Please enter email\/phone number."}

    See the added backslash before the slash? Ugly, right? To avoid that, simply add the JSON_UNESCAPED_SLASHES argument to your json_encode() function usage like this:

    Try it!

  • Lazada owes me money. Their customer service people disregard my requests for assistance.

    Think twice before buying anything from Lazada. They can send you the wrong item, have you return the wrong item with a promise of refund, give you no refund, and never get back to you again even when you already paid!

    Long story short: I purchased an item last year (already paid) and Lazada sent me a wrong item. Lazada picked up the wrong item but has not given me the correct item I ordered or a refund. All transactions up to the time they picked up the wrong item was done through email, but when they acquired the wrong item, they stopped responding to my messages. It’s been about 2 months already.

    I got my hands full with what I do in my daily life right now, and the money that Lazada owes me doesn’t make me feel any better. Their customer service people are being rude too, by ignoring my messages.

    So yeah, Lazada, thank you for taking my money for nothing, and thank you for your poor customer service.

  • Android NetworkOnMainThreadException at StrictMode fix (with deprecated methods used)

    A JSON Parser developed by some members of the Android dev team. But a few weeks after completion of the parser, several methods became deprecated.

    And when we use that parser, we get errors. Part of the stack trace is as follows:

    Not sure if the NetworkOnMainThreadException is being thrown because of the deprecated methods but the following code is what prevents our app from getting the error.

  • Android/Java: get variable data type

    For some reason you might need to get the data type of your Java variable. Here’s an example of what you can try:

    Then do whatever you like with data_type, whose value should be “String”. 🙂

  • Get device’s IMEI in Android app development

    Here’s the content of my MainActivity class where I log the IMEI of my test device.

    You also need to add this permission to your AndroidManifest.xml file:

    Place it inside the <manifest></manifest> tag, but same level as <application></application>.

    Source: How to get the device’s IMEI/ESN programmatically in android?

  • Android: open web page in a browser

    To open a web page in an Android app, we need to create a new Intent and start a new Activity with it.

    Here’s a Java method that we can re-use:

    Just pass the URL of the web page you’d like to open to this method.

    Source: How to open a website when a Button is clicked in Android application?

  • Laravel 5: How to change Auth URL from “/auth/login” to just “login”:

    Just sharing a link telling us how to remove the “auth” in the URL for Laravel’s pre-made login system.

    You need to modify App\Http\Middleware\Authenticate::handle() method and change auth/login to /login.

    Then you need to add $loginPath property to your \App\Http\Controllers\Auth\AuthController class.

    Source: How to change default redirect URL of Laravel 5 Auth filter?

  • Android / Java: Get the name of method being executed

    If you want to trace the name of the method that’s being executed, the Java code snippet in this page might help you.

    This is especially handy when you are debugging by tracking every method that is executed. You can enclose these lines of codes in a function that you call in every method — that’s easier than typing the name for each method you’re tracking, right? 🙂

    The String variable methodName will contain the name of the method where you place the following lines of code: