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.
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.
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.
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.
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.
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.
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.
In this code snippet, we’ll learn how to perform file downloads in PHP with the file_get_contents() function. The file_get_contents() function gets/downloads a file(locally or from the web) as a string. We can then modify the string and save it with file_put_contents().
In this code snippet, we’ll learn how to force file downloads in PHP.
In this code snippet, we’ll learn how to perform data validation in PHP. Data validation is used to make sure the input data is present and in the correct format for further processing. This can be performed either on the front or backend.