The Artisan Archive

The Power of the Singleton Design Pattern: Creating Global Objects in PHP

The Singleton design pattern is a creational design pattern that restricts the instantiation of a class to one object and provides a global point of access to that object. In other words, it ensures that a class has only one instance and provides a way to access it globally. In PHP, the Singleton pattern is widely used to create objects that need to be shared across multiple parts of an application. To implement the Singleton pattern in PHP, we need...

Continue reading...

CRUD Operations in PHP: A Beginner’s Guide

CRUD is an acronym that stands for Create, Read, Update, and Delete. It refers to the four basic operations that can be performed on data stored in a database. In this essay, we will explore how to implement CRUD functionality using PHP. PHP is a popular server-side scripting language that is widely used for developing dynamic web applications. It provides a rich set of functions and libraries that make it easy to work with databases and implement CRUD operations. Create...

Continue reading...

PHP OOP: Building Better Web Applications

Object-oriented programming (OOP) is a popular paradigm for developing software applications that focuses on creating objects that encapsulate data and behavior. In PHP, OOP is widely used to build complex and scalable web applications. In this essay, we will discuss the benefits of OOP in PHP and how it can improve your website’s SEO. Benefits of OOP in PHP There are several benefits to using OOP in PHP: How OOP can improve your website’s SEO Using OOP in PHP can...

Continue reading...

Mastering MVC Design Pattern in PHP: A Complete Guide for Building Scalable and Maintainable Web Applications

The Model-View-Controller (MVC) architectural pattern is widely used in web development, including in PHP. It provides a structured and modular approach to building complex web applications, allowing developers to separate the concerns of data, presentation, and business logic. In this essay, we will explore the key concepts of MVC in PHP and how it can be implemented in practice. The MVC pattern consists of three core components: In PHP, the MVC pattern can be implemented using various frameworks, such as...

Continue reading...

How to Sort Arrays in PHP

PHP is a popular server-side scripting language that is widely used for developing dynamic websites and web applications. One of the most common tasks when working with PHP is sorting arrays. In this essay, we’ll take a closer look at the various PHP sort methods available and their respective advantages and disadvantages. The sort() function is the most basic sorting function in PHP. It takes an array as input and sorts it in ascending order. This function is very simple...

Continue reading...

How to Sort a Table by a MySQL Table Column

Web development and content management is essentially the alpha and the omega. Manipulating data can be a rather daunting task and requires a good grasp of overall app flow in terms of how data changes when X things happen during its use. In this post we’re going to learn how to create a container that shows all those popular blog posts that we have in our app, sorted in ascending or descending fashion. So where do we start? The simplest...

Continue reading...

Working with Files in PHP (The Brief Way)

Files are essential to large scale web applications. Wherever we have some kind of resource management and planning, the idea to log changes and variations or create reports it’s crucial for providing optimal insight to sales, accounting and managing departments. Even from the developers’ perspective, being able to generate CSV or JSON files on demand for various purposes, will speed up development and boost productivity, leading to more features for other departments to leverage. PHP offers an very robust way...

Continue reading...

How to Submit a Form in PHP

Knowing how to manipulate forms in PHP it’s essentially the alpha and the omega in terms of data management and flow. If you cannot collect data from the user and send it from point A to point B, your app can be as good as a calculator. Sending or submitting form data capabilities to a specific script for handling can be found to a very basic login form up to a asynchronous API request that updates an active record of...

Continue reading...

Get JSON data from PHP file

Having a PHP file spit out JSON as its main function can be really useful when you want to execute an AJAX call so you can refresh some content on the page, without reloading the page. Since the development of AJAX calls, a long wished dream was fulfilled for almost every web artisan. Being able to fully change parts of the content on a page without reloading everything was huge and certainly made each project more instant, direct and user...

Continue reading...

What is a JS modal?

Modals offer an amazing advantage throughout your app since you can display data on demand that you want the user to see or change in seconds instead of letting the redirector send the user to an actual form to edit the data directly and thus submitting to an actual script. In case of a fully implemented web app, modals used side by side with AJAX or Asynchronous JavaScript and XML. AJAX is the main technique that the modal will be...

Continue reading...

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...

Difference between isset() and empty()

One of the most important aspects in PHP it’s validation. It is literally everywhere and in any kind of web app you are building or you will ever build. That is why it is crucial to know how to validate data and how to properly handle the logical conditions and guards. If you are a developer, you already know that are some conditions that even if they don’t break the code down by producing an error they do cause problems...

Continue reading...

Anonymous and Named Functions in PHP

Developing large scaled web applications using PHP can become really hectic really fast if not utilize the most important structures of all, functions. Functions provide us the ability to re-use segments of code instead of writing them over and over again. Using functions we write the code block once and we can call it again again without worrying about chaotic consequences. Functions, apart from compatibility and re-usability, can enable scalability and maintainability of the code base and sometimes, if used...

Continue reading...