PHP Code Snippets Data Validation

PHP Data Validation

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.

Code Snippets Dirname

PHP dirname() Function

In this code snippet, we’ll learn about the dirname() function in PHP. The dirname() function takes in a path string and gives you the parent directory of it. A second parameter can be added where you can specify how many levels up you want to go.

Code Snippets Request Variables

PHP Request Variables

In this code snippet, we’ll learn how to get HTTP request variables in PHP. When an HTTP request is made to the PHP backend the data associated with the request(POST body, GET url parameters, cookies, …) will get stored as superglobal variables available from everywhere in the code. Here we’ll see how to access some of these values.

PHP Code Snippets Pass By reference

PHP Pass By Reference

In this code snippet, we’ll learn how to pass and work with variables by reference in PHP. Variables can be passed either by value or by reference. Usually, we do it by value meaning you pass just the value and not the variable itself. So, any changes made locally inside the function scope won’t be seen outside of the function scope.

PHP Code Snippets Working With Strings

PHP Working With Strings

In this code snippet, we’ll learn how to work with strings in PHP. More specifically we’ll learn how to concatenate(join) strings, trim white spaces, make a string lower or upper case, get its length, check if it contains a substring and get its position, replace a substring, remove tags and slashes, split a string into an array on a specific character and join a string array back into a string.

PHP Code Snippets Working With Arrays

PHP Working With Arrays

In this code snippet, we’ll learn how to work with arrays in PHP. More specifically we’ll learn how to merge arrays, add/remove elements, reverse the array, remove duplicates, search the array, find its length, make associative arrays and get their keys. Associative arrays are use strings for indexes instead of numbers.

PHP Code Snippets JSON

PHP Working With JSON

In this code snippet, we’ll learn how to work with JSON in PHP. You can use json_encode() to serialize and json_decode() to deserialize JSON. json_decode() also takes a second parameter(true/false) where you can specify if you want the JSON to deserialized to an associative array or to an object. In this post, you can see a practical example of JSON serialization/deserialization.

Advertisment ad adsense adlogger