Home » Programming

Checking for Inequality in Python: ‘!=’ vs ‘is not’ [Examples]

Python Not Equal

Should you use the != operator or the ‘is not’ statement when comparing values in Python? Read on to find out! The != Comparison Operator The != operator can be used to compare two variables or statements’ return values. If they are not equal, the statement will return TRUE. Python is forgiving when it comes to comparing variables of different types. So if the type of the variables being compared is different, but the value is the same, it will return TRUE (rather than throwing an error) as they are not equal in both type and value. myNumber … Read more

Home » Programming

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

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

Implementing a Queue Data Structure in JavaScript [Examples]

JavaScript Queue

A queue is a commonly used data structure in programming. Here’s how to implement and use a queue in JavaScript. JavaScript doesn’t include a data structure specifically called a queue – but that doesn’t mean the functionality isn’t there. JavaScript arrays can be used in the same way – it’s just the terminology that’s a bit different. Rather than duplicating array functionality for queues, queue functionality exists in the array functions of JavaScript. What is a Queue Data Structure? A queue is a sequence of items in a specific order. Items can be enqueued (added … Read more

Home » Programming

The Best Way to Declare an Array in JavaScript

JavaScript Declare Array

There are several ways to declare an array in JavaScript. This article will show you which is the best. What is an Array? An array is a type of variable that holds an ordered list of values. Each value has a position in the array, called the index. The index can be used to access each value in the array. Indexes are integer values, which start counting from 0 (zero) for the first item in the array. The Literal Constructor – Declaring an Array using Square Brackets (The Best … Read more

Home » Programming

Merge Arrays in JavaScript with concat() and push() [Examples]

JavaScript Join Merge Concatenate Arrays

This tutorial will show you the correct ways to merge two or more arrays in JavaScript with the concat method – and provide a warning about how not to merge arrays. Merging/Joining Arrays in JavaScript with the concat()* Method The concat() method exists with one purpose – to merge arrays. concat() Method Syntax array.concat(ARRAYS) Note that: array is the first array of the arrays to be joined ARRAYS should be a comma-separated list of arrays to be added to the first array They will be joined in the order of appearance concat() returns a new array – … Read more

Home » Programming

How to Randomize/Shuffle an Array in JavaScript [Examples]

JavaScript Randomize Shuffle Array

This article will show you a few ways, both easy and complex, to randomize/shuffle an array in JavaScript. The Quick and Dirty Way to Randomize an Array If you aren’t too concerned about the probabilities involved, this code is a quick way to randomize the contents of an array: // Function to return a shuffled copy of a given array function shuffle(array) { var shuffled = […array]; // Make a copy of the array to be shuffled so that the original is not altered by … Read more

Home » Programming

How to Reverse an Array In JavaScript

JavaScript Reverse Array

This short article will show you how to reverse an array in the JavaScript programming language. What is an Array? An array is a type of variable that holds an ordered list of values. Each value has a position in the array, called the index – and can be accessed by using that index. As an array is ordered, the position matters. Values within arrays will be processed by loops and other data structures in order – so being able to reverse that order is beneficial. Reversing an … Read more

Home » Programming

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

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