Home » Programming » PHP

Showing Errors in PHP [Tutorial]

Showing Errors in PHP

This tutorial explains how to use several methods for displaying errors in a PHP application for debugging purposes. Be Careful There are security considerations when displaying errors in any application. Be careful when and where errors are displayed – especially when building products for the web – as the errors may contain sensitive information. For example, if an application connects to a database and the database crashes, an error will be produced. That error may contain the username and password used to connect to the database. If … Read more

Categories PHP

Home » Programming » PHP

6 Best PHP Frameworks to Consider in 2021

Best PHP Frameworks

PHP frameworks speed up the development of complex applications greatly. This article features 6 of the most popular frameworks to consider in 2021. These frameworks provide varying scaffolding levels to get you started, from database operations, object handling, user authentication, and page templating. So you are free to build your application without having to constantly write and re-write the common functions that make your application work. They also come with some security benefits – using a sanitized database abstraction layer that has been written and … Read more

Categories PHP

Home » Programming » PHP

PHP shell_exec Function: How to Use It [With Examples]

PHP shell exec Function

This tutorial explains how to use the shell_exec function in PHP in order to execute code via the shell and return the output as a string. PHP is a versatile programming language for building server-side web applications, but sometimes you need to execute code from another environment and use the result in PHP. This can be achieved by using the shell_exec function to execute commands on the system’s shell hosting your PHP code and return the result as a string to PHP. When doing so, there are security … Read more

Categories PHP

Home » Programming » PHP

PHP var_dump() Function [With Examples]

PHP var dump Function

The PHP programming language includes various built-in variable types and allows you to create your own complex object classes. Third-party packages also come with their own object classes. So it’s useful to find out what type of variable a variable is, its value, and the type/value of any properties or values the variable may contain. The PHP var_dump() function returns a given variable or value’s properties, including the value and type. It works through the variable’s properties or value recursively doing the same. Read on to learn … Read more

Categories PHP

Home » Programming » PHP

PHP strtotime() Function – Converting Text to Timestamp [With Examples]

PHP strtotime

The PHP strtotime() function takes a string containing text and converts it to a Unix Timestamp. Find out how to use this function in this guide. A Unix timestamp is a number representing the number of seconds since 00:00:00 UTC on 1 January 1970. From it, you can build more complex Date/Time objects which can readily provide Dates/Minutes/Hours/Seconds or other information about the supplied time. strtotime Syntax strtotime ($datetime , $baseTimestamp) Note that: $datetime is your supplied text string (possibly) containing a date strtotime() supports the English language only See … Read more

Categories PHP

Home » Programming » PHP

PHP foreach Loop [With Examples]

PHP foreach Loop

Looping over arrays and iterating over an object’s attributes form the foundation of a lot of application logic. The foreach construct in PHP can do both and is a crucial tool for building your application logic. PHP foreach Syntax The syntax for a foreach construct in PHP is as follows: foreach (iterable_expression as $value) { # statement(s) } Or, if you also wish to access the array key as well: foreach (iterable_expression as $key => $value) { # statement(s) } Note that: iterable_expression is the variable or value to … Read more

Categories PHP

Home » Programming » PHP

PHP switch Statement [With Examples]

PHP switch Statement

As you get more confident with your PHP code, your code will get more and more complex. The PHP switch statement simplifies your PHP logic, replacing messy if statements when trying to make simple decisions based on the value of a variable. Read on to find out how to use it. PHP switch Syntax switch($v) { case $a: # Code to be executed if $v is equal to $a break; case $b: # Code to be executed if $v is equal to $b break; case $c: # Code to … Read more

Categories PHP

Home » Programming » PHP

How to Make a PHP Redirect to a Different Page [Quick & Easy]

How to Make a PHP Redirect

Being able to redirect the user to a different page in PHP can be useful if you’ve moved a page to a different location or want to send the user elsewhere – directing them to an access denied or error page instead of the page they were trying to access. This can be easily done using the PHP header() function. Using the PHP header() Function to Redirect to a Different Page To redirect the user to a different page, simply include the following PHP code: header(“Location: https://linuxscrew.com/”); … Read more

Categories PHP

Home » Programming » PHP

PHP trim() Function [With Examples]

PHP trim Function

You’ll want to process and tidy your user input before you do anything with it when building PHP apps. trim() is one of the tools available to do this – it removes all white space from the beginning and end of a string. Whitespace is things like spaces, tabs, and newlines which may be invisible to the user. PHP trim Syntax trim ( $string [, $character_mask = ” \t\n\r\0\x0B” ] ) Note that: By default, $string will be returned by trim() with all whitespace removed from the beginning and end, including: … Read more

Categories PHP

Home » Programming » PHP

PHP in_array Function: Check Array Values Exist

PHP in_array Function

The PHP in_array() function allows you to check for the presence of a value in an array, and take an option based on this result. Syntax in_array ( $needle , $haystack [, $strict = FALSE ] ) Note That: $needle can be a variable of any type (string, number, date, etc) $haystack should be an array (see here, here, and here for more about arrays) $strict is optional, and will ensure that the type of values is also compared when checking whether $needle is present If $strict is FALSE (the default behavior), the number … Read more

Categories PHP