The Artisan Archive

How to create a JSON Object in PHP?

JSON or JavaScript Object Notation is a powerful format that’s been used mainly whenever we need to include some kind of dynamic data fetching within our website, using JavaScript. It’s the ideal format when working and communicating with application programming interfaces. To create a JSON object in PHP you simply call the json_encode() function by passing an array of your choice as parameter. For instance, suppose we have an array of objects that contain an id and a label.$arr =...

Continue reading...

How can I remove a specific item from an array?

Usually when working with arrays we are in need to remove a targeted element and execute a specific operation with it. In this post we’ll learn how to use the following function to remove an item by value from the designated array. To remove a specific item from an array you need to traverse the array, check each value with the one you want to remove and then create a new array with all the other elements. The resulting array...

Continue reading...

How to check if an array is empty in PHP?

To check if an array is empty in PHP your best bet is to count the number of items that contains. To do that you need to simply apply the count function on your array. Let’s try that using the code below: $arr = array();if (count($arr) == 0) { echo ’empty’;} The count function however may not be great for performance if the data is too large so you may need to use a safer way using the negation operator...

Continue reading...

What does date() do in PHP?

Talking from experience, being able to manipulate time as developer is a great asset that many beginners and even senior developers fail to invest the time and effort to learn how it can be used effectively. With pretty much every system, there’s the need to work with timestamps and dates to keep track application cycles and recursive operations that may be executed in a period of time. What date() does is to create a string of the current unix timestamp...

Continue reading...

What is a PHP class?

As a newbie developer I have struggled a lot in the past to understand what I’m going to share with you below and I hope I can save you some time and effort by giving you the tools to harness this exceptionally useful and robust principle, the class. A PHP class it’s a blueprint that instructs the creation of a certain type of object. Object’s characteristics are determined by its properties and methods that incorporates. Each object may differ upon...

Continue reading...

How does MVC pattern work?

Initially, the idea behind developing a web or software application was to simply separate the logic to functions or classes as the complexity increased, to produce a better abstract perspective of each component’s state and behavior. At some point just by creating functions and classes, engineers concluded that this approach wasn’t the answer, or at least the complete answer. Before we dive into specifics let’s provide some details regarding the creator of that particular design pattern. MVC was created by...

Continue reading...

What is stdClass in PHP?

As a developer, when you code web applications and especially large scaled ones, you are going to use the notion of the object at some point. All those conventional data structures may not cut it depending the use case and the issue that you are trying to solve. That is why at some point you may need to create your own data structure with the properties of your choice that are going to match your needs perfectly. You can achieve...

Continue reading...

Foreach in PHP

While developing web applications there are always routines that display some kind of information on the screen. Apart from displaying data, it is absolutely crucial to be able to modify it as well in a massive scale. Such functionality calls for the foreach structure. Foreach is essentially a structure that is responsible for traversing an iterable given some expression. With each iteration, foreach may execute a defined action to the data, either to modify the value, create a new iterable...

Continue reading...

How to use include and require in PHP

Developing a web application can be a really difficult thing to do. You need to plan each sub system separately, analyze their connections in terms of overall performance, map out models to use so you can implement persistence to store the data safely and you should absolutely endeavor to create a staggering and a robust user interface. The user is the heart of your application after all. In order to do all those things and actually produce something great for...

Continue reading...