XML is a great format for data exchange. But JSON is used more and more for data exchanging and configuration. In my opinion JSON is a great, because it is more lightweight than XML. More and more apis work with JSON today. Recently I had to convert XML To JSON. Nothing special, just a simple XML String. In this article I’ll show you how to do this with only a few lines of code.
PHP
PHP: Return An include And Assign It To A Variable
The include statement is a simple way to include PHP code from another file.
The usage is quite simple. Just write something like this:
<?php include 'a_file.php'; |
If the file is successfully included it will return a TRUE or 1. If it failes to include the file it will return a FALSE. But sometimes you may need to get the content of an included file in order to store it in an variable. Unfortunately the include statement just returns TRUE or FALSE, as mentioned above.
PHP: How To Format Numbers
In PHP there is an easy way to format numbers with decimal separators and thousands separators an so on. A number like 123456.789 can be easily formated to something like 123,456.79 or 123.456,79. In this article I’ll show you how.
First of all, why do we need to format numbers? Well it’s obvious, that you can read a number like 123,45 very easily. But how about 9348394309.238? 😉 In this case, it will be very useful to add some thousands separators. Because then it will look like this: 9,348,394,309.238. And maybe we want to limit the decimals to two. So it will look like this: 9,348,394,309.24 (please note the rounded decimal). So this makes it way more readable.
PHP: Use Array Keys As Variables | extract()
Would you like to use an arrays key as a normal variable in PHP? Well, the extract() function is a great way to generate variables from an associative arrays keys. In this article I’ll show you how.
PHP: Convert An Array To JSON
Sometimes, when programming in PHP, you’ll need to convert an array to JSON. This enables you e.g. to retrieve data serverside through PHP and pass it to the client, which then can process this data with javascript. There are different ways to do that, e.g. via AJAX.
In this article I’ll show you, how easy it is to convert a PHP array into a JSON String.
PHP: Check If A Value Exists In An Array
Let’s say, you have a value like e.g. a name and you’ll find out, whether this value exists in an array of names or not. How could you do this? Well, it’s quite simple. Here I’ll show you how.
PHP: Find Out The Modification Time Of A File
Do you want to find out the modification time of a file in your PHP script? Well, in this article you’ll find out how easy this is. 😉
PHP: Quickly Generate An Array From Variables With The compact() Function
Do you want to build an array in PHP, which should contain existing variables? Well, there is an easy way to do this. PHP has a very cool function for this to do. You can use the variable’s name as the key and the variables value as the value.
PHP: How Can I Find Out, Whether A Number Is Odd Or Even With The Modulo Operator?
There is a simple way in PHP to find out, wheather a number is odd or even. All you need is the modulo operator.
What is the modulo operator?
The modulo operator in PHP is represented by an %. With this operator you can determine the rest of an interger division.
Here’s an example:
PHP: Detect Time Of Last Modification Of A File
Do you need to find out in PHP, when a file was modified? Then you should take a look at the PHP-function filemtime. This function expects a filename as parameter and returns the modification-time of this file.
Here is an example:
< ?php echo filemtime('anyfile.html'); ?> |
This PHP-script will output the timestamp of the last modification of the file anyfile.html.
You can also format this by using the date-function: