Code Snippets Exceptions

PHP Exceptions

In this code snippet, we’ll learn how to throw and handle an exception in PHP. Exceptions are used to indicate that an error has occurred within our code. For this demo, we’ll make a simple function that returns the sum of all the elements in a given array. But if the array is empty we’ll throw an exception. We’ll also check the data type of each element using gettype() to make sure the provided value is in fact a number, else an exception will be thrown.

PHP Code Snippets Make a PDF

PHP Making PDFs

In this code snippet, we’ll learn how to make a PDF file in PHP. We will use the FPDF library to generate a PDF. More specifically we’ll see how to create page headers/footers, add page numbers, write text, use cells, use multicell, inserting links and images.

Code Snippets Reading And Writing Excel Files

PHP Reading And Writing Excel Files

In this code snippet, we’ll learn how to read and write excel files in PHP. We will use the PHPSpreadsheet library to read/write Excel files and we’ll learn how to read cells, find a specific string in a row or column, write to cells, set cell styling and formatting(like setting the border, background color, etc) and we’ll see how to insert images.

Code Snippets Using DomDocument To Work With HTML

PHP Using DomDocument To Work With HTML

In this code snippet, we’ll see how to work with HTML using DomDocument in PHP. If you need to change HTML in code or extract some data from it(web scraping) you can use the DomDocuemnt class to help you with that. You simply create a new instance of it and load the HTML and then you can use its functions to access the DOM.

Code Snippets Global Exception Handler

PHP Global Exception Handler

In this code snippet, we’ll make a global exception handler in PHP. A global exception handler is used to catch any unhandled exception that might bubble up in your code. This way you can log the exception and let the user know there was an error instead of the program just crashing.

PHP Code Snippets APIs With The Slim Framework

PHP APIs With The Slim Framework

In this code snippet, we’ll make a REST API with the Slim framework in PHP. Slim is a framework that enables you to easily and quickly build REST APIs in PHP. I will show you how to make a POST and GET endpoint. Other types of requests follow the same logic. Official dodumentation here. We will also enable CORS so you can call the API from the browser.

PHP Code Snippets OOP and Objects

PHP Objects And OOP

In this code snippet, we’ll take a look at objects and basic of object oriented programming in PHP. I will just show how to make and use classes, interfaces, properties, … in PHP. I won’t go into OOP too much as I already did some other posts about that.

Advertisment ad adsense adlogger