PHP

PHP is a widely-used server-side scripting language that powers millions of websites and web applications, enabling dynamic content creation, database connectivity, and flexible web development

Check if an email address is valid in PHP

The filter_var() function can filter a variable with a specified filter.

In order to validate an email address the FILTER_VALIDATE_EMAIL filter must be used. It's part of the PHP validation filters group. You can see more filters here: https://www.php.net/manual/en/filter.filters.validate.php

In general, this validates e-mail addresses against the addr-specsyntax in » RFC 822, with the exceptions that comments and whitespace folding and dotless domain names are not supported.

        
    

How to split a string in PHP from an array based on the new line characters

Sometimes a string, especially when it is submitted through a textarea, is not easy to be separated based on its paragraphs. When a user for example hits the Enter button in a textarea then we probably need to add the necessary <p> and </p> before and after each section of the submitted text. Using the preg_split('/\r\n|\r|\n/', $text)  you can recognise the new lines contained in the $text variable. As a result you can easily loop over the array and handle each row separately.

Here is an example where a PHP string containing a text with line breaks can be split into an array.

        
    

Removes an element from the beginning of an array

array_shift(array &$array): mixed

Accepts only one parameter, the array by reference

Note: This function will reset() the array pointer of the input array after use.

Returns the shifted element.

Returns null if the array is empty

        
    

Prepend one or more elements at the beginning of a PHP array.

 

array_unshift(array &$array, mixed ...$values): int

array_unshift() accepts at least 2 params (since PHP version 7.3 the function can be called with only one parameter)

The first parameter is the array called by reference, new values will be added.  The next parameters are the values to added to the array.

Returns the new number of elements in the array.