Home » Search results for 'php array'

Using the PHP Array ‘implode’ Function, with Examples

php implode function

We’ve previously covered the explode() function in PHP – now it’s time for implode()! This tutorial will teach you how to merge values from an array into a single string using the PHP implode() function. Processing data for presentation to the end-user is one of the common PHP tasks. PHP is frequently used on Linux to display database query results to the user. If some of those values are stored in an array, you may want to merge them into a single string for ease of display or brevity … Read more

Categories PHP

Home » Search results for 'php array'

Safely Using PHP Variable Variables with Arrays [Examples]

PHP Variable Variables and Arrays

PHP variable variables are a powerful way to add flexibility to your code. This article explains the pitfalls and solutions to using them with arrays. Variable variables are a great way to simplify your code and make it more readable. However, when using them with arrays, you need to be careful. PHP Arrays and Variable Variables To use variable variables with PHP arrays, extra considerations need to be taken to resolve potential ambiguities in your code. Consider the following PHP code: To what exactly is the array key … Read more

Categories PHP

Home » Search results for 'php array'

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 » Search results for 'php array'

Merging/Combining Arrays in PHP with array_merge [Examples]

PHP array_merge

This article will explain how to merge arrays using the PHP array_merge function and provide some code examples. What is an Array? An array is a type of PHP variable that can hold other values, or references to other variables, in a list at a certain position. Arrays are really useful – allowing you to store a list of variables or values of any length in a single variable – ready to be iterated over, printed as the rows in a table, or otherwise processed. Arrays might … Read more

Categories PHP

Home » Search results for 'php array'

Print an Array in PHP using print_r and var_dump, With Examples

PHP Print Array

This article will show you several methods on how to print an array in PHP, with some examples. Arrays in PHP can be processed using foreach loops to interact with each element in the array. This article focuses on directly printing an array in full rather than each element individually. Using echo to Print an Array (It Won’t Work) You might think that you can just use the echo statement to print an array in PHP – this is not the case, as seen below: <?php // Define an … Read more

Categories PHP

Home » Search results for 'php array'

Multidimensional Arrays in PHP – How To Use, With Examples

PHP Multidimensional Arrays

This article will show you how to use multidimensional arrays in the PHP programming language, explaining what they are and how to use them. What is an Array? An array is a type of PHP variable that can hold multiple values. It’s a list of values you can loop through and perform operations on each individual value. For example, you might want to perform an action on a list of files. By storing that list as an array, you can loop through the file names … Read more

Categories PHP

Home » Search results for 'php array'

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

Home » Search results for 'php array'

How to Declare Variables in PHP (Constants, Static, Global)

PHP Declare Variable

This article shows you how to declare variables in PHP, including constant, static, and global variables. Code examples are provided. What is a Variable? In computer programming, a variable stores a value, giving it a name that can be used to access it. Once a variable has been created and a value has been assigned to it, the value can be retrieved using the name given to the variable. The value of a variable can be updated (unless the variable has been declared as a constant), and the … Read more

Categories PHP

Home » Search results for 'php array'

PHP Scopes in Functions, Loops, Modules, With Examples

PHP Variable Scopes

This article explains what variable scopes are, and how they work in the PHP programming language, with code examples. When programming in any language, you must be aware of how variables are scoped – otherwise, you risk unexpected behaviour, including incorrect calculations, or loss of data – detrimental outcomes for any application (especially if you are working with business records or financial data) that will result in angry users. What is a Variable? In computer programming, a variable stores a value, giving it a name that can … Read more

Categories PHP

Home » Search results for 'php array'

PHP $this Variable – What It Is, How To Use It [Examples]

PHP this

This article will explain the PHP $this variable – what it is, what it does, and how to use it – with code examples. PHP Objects and Classes PHP is an object-oriented programming language. Code and data can be stored in an object which represents the items your code represents with a set of properties and methods. For example, you might have objects that represent sales invoices, or cars, or pets, or people – each containing code and data relevant to the thing they represent. The properties or attributes of … Read more

Categories PHP