Home » Programming » PHP

Modulus (Mod), Modulo and PHP – Explanation and Code Examples

PHP mod Modulus Modulo

This article will explain (in the simplest terms possible) what modulus and modulo mean and how they are calculated in the PHP programming language. Modulus and Modulo are NOT the Same Things! There’s a bit of confusion about this, and it seems that in some places, the terms are used interchangeably – this is wrong (and confused me too – hence this article)! What is Modulus? In mathematics, the modulus is the absolute value of a number. It’s the non-negative value of a number – or its distance from 0 (zero). … Read more

Categories PHP

Home » Programming » PHP

How to Compare Dates in PHP, With Examples

PHP Compare Dates

This tutorial will show you the best way to compare dates in the PHP programming language – and provide code examples. It’s essential to be able to accurately compare dates when building your app. For example, you may wish to compare dates for reporting purposes – retrieving a list of transactions from a shop for a given reporting period, or you may want to have your user enter a fake birthday into your webpage so that they can pretend that they are over 18. Anyway, … Read more

Categories PHP

Home » Programming » PHP

Using the PHP ‘exit’ (AKA ‘die’) Function to Terminate your Script [Examples]

PHP exit die

This short article will explain the PHP exit function and how it is used to quit/exit/terminate a PHP script. PHP exit Function Syntax The syntax for the PHP exit function is as follows: exit(STATUS) Note that: exit will terminate the execution of the script at the point it is called STATUS is an optional integer or string value containing a status code or exit message If an integer value with a status code, the script will return this code as the exit status if running on the command line Exit status should be 0 for … Read more

Categories PHP

Home » Programming » PHP

How to Use the PHP *strstr()* Function to Search a String

PHP strstr()

This tutorial will show you how to use the PHP strstr() function to search and return a portion of a string, with examples. What is the strstr() Function For? The strstr() function searches a string and returns the matching portion of the string and everything that follows or precedes it. If the string does not contain a match for the search, a value of false is returned. It’s a hard one to visualize – the examples below will show you precisely what this means. If you’re simply looking to confirm whether … Read more

Categories PHP

Home » Programming » PHP

How to Round Numbers (Integer, Decimals) in PHP with the round() Function

PHP round()

This article will show you how to round numbers in PHP with the round() function, including rounding to the nearest integer or a set number of decimal places. Why Round Numbers? There are various reasons you may want to round numbers. You don’t need incredible accuracy for many day-to-day calculations. You probably don’t need to know your height to several thousandths of a centimeter if converting from feet – knowing to the nearest whole centimeter is perhaps enough. If you’re calculating tax, you only need to know … Read more

Categories PHP

Home » Programming » PHP

PHP: Process Each Item In An Array with array_map() [Examples]

PHP array_map

This article will show you how to process a PHP array using array_map() to run a function on each item in the array. Why use array_map()? Arrays store a sequence of values in a single variable. If you’re storing those values, you probably want to do something with them at some point. Say, for example, you had an array of numbers, and you wanted to calculate the square of each number to be stored in a new array. You could create an empty array, loop through the original array calculating … Read more

Categories PHP

Home » Programming » PHP

Rounding Numbers Down with the PHP floor() Function, with Examples

PHP floor()

This short tutorial will cover how to use the PHP floor() function to round numbers DOWN – and provide some code examples. The PHP floor() function rounds a number down (and only down!) to the nearest integer. To round a number up to the nearest integer, use the ceil() function instead. To round a number normally, or for more options when rounding a number, use the round() function. PHP floor() Function Syntax The syntax for the PHP floor() function is as follows: floor($NUMBER) Note that: $NUMBER is the float or integer number you wish to perform the floor() operation on floor() will return a float type number regardless of … Read more

Categories PHP

Home » Programming » PHP

Rounding Numbers Up with the PHP ceil() Function, with Examples

PHP ceil()

This short tutorial will cover how to use the PHP ceil() function to round numbers UP – and provide some code examples. The PHP ceil() function rounds a number up (and only up!) to the nearest integer. To round a number down to the nearest integer, use the floor() function instead. To round a number normally, or for more options when rounding a number, use the round() function. PHP ceil() Function Syntax The syntax for the PHP ceil() function is as follows: ceil($NUMBER) Note that: $NUMBER is the float or integer number you wish to perform the ceil() operation on ceil() will return a float type number regardless of … Read more

Categories PHP

Home » Programming » PHP

How to Use the PHP ‘while’ Loop, With Examples

PHP while

This tutorial will teach you how to use the while statement to build loops in the PHP programming language and show some examples. The while loop is the most straightforward kind of loop in PHP. The code inside the loop will execute for as long as the while condition is true. Change the condition, and the loop will exit. while loops are a fundamental part of PHP (and loops are an essential part of computer programming in general) – so it’s good to know how they work and what they can be … Read more

Categories PHP

Home » Programming » PHP

How to Use the PHP ‘do while’ Loop, With Examples

PHP do while

This tutorial will teach you how to use the do while statement to build loops in the PHP programming language, and show some examples. The do while loop is one of the simplest types of loop in PHP. The code inside the loop will execute for as long as the do while condition is true. Change the condition, and the loop will exit. do while loops are a fundamental part of PHP (and loops are an essential part of computer programming in general) – so it’s good to get to know how they … Read more

Categories PHP