PHP array_count_values() Function

PHP array_count_values() function is “used to count all the values inside an array.” It calculates the frequency of all of the elements of an array. Syntax array array_count_values( $array ) Parameters $array: It accepts a single parameter $array. This parameter is the array for which we need to calculate the count of values present in it. … Read more

PHP strlen() Function

PHP strlen() function “returns the length of a given string.” It takes a string as a parameter and returns its length. Syntax strlen(string) Parameters  string: It is required and specifies the string to check. Return value It returns the length of the string, including all the whitespaces and special characters. Example 1: How to Use … Read more

PHP array_diff() Function

PHP array_diff()  function is “used to compare the values of two (or more) arrays, and returns the differences.” The array_diff() function returns all the entries in the first array, which is not present in any other arrays. Syntax array_diff(array1,array2,array3…); Parameters array1, array2, array3: The parameters array1 and array2 are required. From array3, the parameter is optional. The array1 parameter is the … Read more

How to Fix Composer is operating significantly slower curl php

Composer may be operating significantly slower than normal because “you do not have the PHP curl extension enabled.” To fix the Composer is operating significantly slower curl PHP issue, you can configure Composer to use curl. Here is the step-by-step guide to configure cURL in PHP. Open your php.ini file (you can find the location … Read more

PHP trim() Function: Remove Characters from String

PHP trim() function is “used to remove whitespace or any specific character from both the left and right sides of a string.” The ltrim() and rtrim() function remove whitespaces or other characters from the left and right sides of the string. However, using just the trim() function without specifying “l” or “r” removes characters from … Read more

PHP money_format() Function

PHP money_format() function returns a number formatted as a currency string. In the main string, the formatted number is inserted where there is a percentage sign (%). Syntax money_format(string, number) Parameters string: It specifies the string to be formatted and how to format the variables in it. number: The number to be inserted at the … Read more

PHP array_chunk() Function

PHP array_chunk() function is “used to split an array into parts or chunks of a provided size depending upon the parameters passed to the function.” The last chunk may contain fewer elements than the desired size. Syntax array_chunk(array,size,preserve_key); Parameters array: The parameter is required, which specifies an array to use. size: The parameter is required; … Read more

PHP number_format() Method

PHP number_format() function is  “used to format a number with grouped thousands.” Syntax number_format(number, decimals, decimalpoint, separator) Parameters number: The parameter is required, and the number is to be formatted. If no other parameters are set, the number will be formatted without the decimals and with the comma (,) as the thousands separator. decimals: It … Read more

PHP in_array() Function

PHP in_array() function is “used to check whether a given value exists in an array. It returns TRUE if the given value is found in the array and FALSE otherwise.” Syntax in_array(search, array, type) Parameters search: It is a required parameter, and it is the value that needs to be searched in an array. array: … Read more

PHP array_sum() Function

PHP array_sum() function is “used to calculate the sum of all the values in an array(one-dimensional and associative).” It returns 0 if the array is empty. The returned sum may be an integer or float. Syntax array_sum($array) Parameters $array: It is an array whose sum needs to be calculated. Return value It returns the sum obtained … Read more

PHP unset() Function

PHP unset() function is “used to unset a specified variable.” In other words, “the unset() function destroys the variables.” If you want to unset the global variable inside the function, we need to use the $GLOBALS array. To check if the variable is set in PHP, use the PHP isset() function. Syntax unset (var1, var2…. … Read more

PHP strcmp() Function

PHP strcmp() function is “used to compare two strings.” This function is case-sensitive, which points out that capital and small cases will be treated differently during comparison. The strcmp() function is case-sensitive, which points out that capital and small cases will be handled differently during the comparison. Syntax strcmp(string1, string2) Parameters This function accepts two … Read more

PHP array_reverse() Function

PHP array_reverse() function is “used to reverse the order of the elements in an array.” Syntax array_reverse(array, preserve) Parameters The array parameter is required, and it describes the array. It is an optional parameter specifying whether the function should preserve the array’s keys. Possible values: true false Return value It returns the reversed array. Example … Read more

PHP sort() Function

PHP sort() function is “used to sort an array in ascending order, i.e., smaller to greater.” It sorts the actual array, and hence changes are reflected in the original array. Syntax sort(array, sorttype) Parameters An array argument is required, and it specifies the array to sort. The sorttype is optional and specifies how to compare the array … Read more

PHP ksort() Function

PHP ksort() function is “used to sort an array in ascending order according to its key values.” It sorts so that the relationship between the indices and values is maintained. Syntax ksort(array, sorttype) Parameters The array parameter is required, and it tells the array to sort. The sorttype is optional and specifies how to compare the array of elements/items. … Read more

How to Check Whether a Variable is Empty in PHP

To check if a variable is empty in PHP, you can use the “empty()” function. The empty() function returns false if the variable exists and is not empty; otherwise, it returns true. Syntax bool empty ($var) Parameters  $var: This variable is being checked and is the required parameter. Below PHP 5.5, empty() only supports variables; … Read more

PHP isset() Function

PHP isset() function is “used to check if a variable is set or not.” The isset()  function returns TRUE if a given variable is set to any value, including the empty string. Syntax bool isset($var, mixed) Parameters $var: This parameter is used to store the variable’s value. mixed:  It indicates that a parameter may accept multiple. … Read more

PHP array_key_exists() Function

PHP array_key_exists() function is “used to check whether a specific key or index is present inside an array or not.” The function returns TRUE if the specified key is found in the array; otherwise, it returns FALSE. Syntax array_key_exists(key, array) Parameters key: It is the value of the key to be checked. array: It is … Read more

PHP array_product() Function

PHP array_product()  function is “used to find the products of all the elements in an array.” Syntax array_product($array) Parameters $array: It refers to the input array whose products of elements we wish to get. Return value The array_product() function returns an integer or float value depending on the nature of the elements of the array. … Read more

PHP array_udiff() Function

PHP array_udiff() function is “used for distinguishing between two or more arrays.” The function compares the different values of two or more arrays using user-defined function data comparison and returns the differences. Syntax array_udiff(array1, array2, array3, …, myfunction) Parameters The array1 parameter is required. The array to compare from. The array2 parameter is required, and an array … Read more

PHP array_fill_keys() Function

PHP array_fill_keys() function is “used to create a new array filled with the given keys and value provided as an array to the function.” Syntax array_fill_keys(keys, value) Parameters keys: It is an array of values used as keys. This parameter is the array consisting of keys that are to be used to create the new … Read more

PHP array_intersect_key() Function

PHP array_intersect_key() function is “used to create an array containing keys and values of the first array whose keys (i.e., from the first array) are present in all other arrays.” The array_intersect_key() returns an array containing all the entries of array1, which have keys in all the arguments. Syntax array_intersect_key(array1, array2, array3, …) Parameters The array1 parameter is … Read more

PHP array_intersect() Function

PHP array_intersect() function is “used to calculate the intersection of two or more arrays.” The function compares the values of two or more arrays and returns the matches. Syntax array_intersect(array1,array2,array3…); Parameters The array1 parameter is required, and the array to compare from. The array2 parameter is required, and the array to compare from. The array3 parameter is optional, and … Read more

PHP serialize() Function

PHP serialize() function is “used to serialize the input array.” The serialize() function accepts a single parameter, the data we want to serialize, and returns a serialized string. Syntax serialize(value) Parameters  value: This parameter is required, which is the value to be serialized. Return value It is a string that contains a byte-stream representation of … Read more

PHP array_column() Function

PHP array_column() function returns “the values from a single column in the input array.” Syntax array_column(array, column_key, index_key) Parameters array: This is a required parameter, and it specifies a multi-dimensional array (record-set) to use. As of PHP 7.0, the array can also be an array of objects. column_key: This parameter is required. The integer key or … Read more

PHP explode() Function

PHP explode() function is “used to break a string into an array.” It splits a string based on a string delimiter, i.e., it splits the string wherever the delimiter character occurs. Syntax explode(separator,string,limit) Parameters separator: It is required to specify where we break the string. string: The parameter is required. It is the string to split. limit: … Read more

PHP array_push() Function

PHP array_push() function is “used to push new elements into an array.” It takes a single element or an array of elements and appends it to the array. Syntax array_push(array, value1, value2…) Parameters array: This parameter is required to which we will add the value. value1: This parameter is also required, which is the value … Read more

PHP array_filter() Function

PHP array_filter() function is “used to filter the elements of an array using a user-defined function called a callback function.” It iterates over each value in the array, passing them to the user-defined function or the callback function. Syntax array_filter(array, callbackfunction, flag) Parameters array: It is required. Specifies the array to filter callbackfunction: It is … Read more

PHP implode() Function

PHP implode() function is “used to join the elements of an array.” To convert an array to a string in PHP, you can use the “implode()” function. For example, if we have an array of elements, we can use the implode() function to join them all to form one string. Syntax implode(separator,array) Parameters separator: It is … Read more

PHP array_flip() Function

PHP array_flip() function is “used to flip/exchange all keys with their associated values in an array.” Syntax array_flip(array); Parameters array: It specifies the array of key/value pairs to be flipped. Return value It returns the flipped array on success. NULL on failure. Example 1: How to Use array_flip() Function in PHP <?php $brands = [ ‘Louis … Read more

How to Convert an Object to Associative Array in PHP

Here are the two ways to convert an object to an array in PHP. Using json_encode() and json_decode() method. Type Casting object to an array. Method 1: Using the json_encode() and json_decode() method You can convert the PHP Object to an associative array using the json_encode and json_decode methods. Syntax $myArray = json_decode(json_encode($object), true); Example <?php class ST … Read more

PHP substr() Function

PHP substr() function is “used to extract a part of the string.” Syntax substr(string, start, length) Parameters string: It is required and specifies the string to return a part of. start: It is required, and it is the position where to start the extraction. The first character is at index 0. If the start is non-negative, the … Read more

PHP is_null() Function: How to Check If Variable is NULL

PHP is_null() function is “used to find whether a variable is NULL or not.” A unique NULL value represents the variable with no value. The NULL is the only possible value of type NULL. The variable is considered to be NULL if: It has been assigned a constant NULL. It has not been set to any value yet. It has been unset(). … Read more

PHP array_walk() Function

PHP array_walk() function is “used to walk through the entire array regardless of pointer position and applies a callback function or user-defined function to every element of the array.” Syntax array_walk(array, myfunction, parameter…) Parameters array: It is required and specifies an array. myfunction: It is required and the name of a user-defined function. parameter: It is optional … Read more

PHP array_keys() Function

PHP array_keys() function “returns either all the keys of an array or the subset of the keys.” Only the keys for that value are returned if a search value is specified. Otherwise, all the keys from an array are returned as the output. In addition, it determines if strict comparison (===) should be used during the search. … Read more

PHP array_shift() Function

PHP array_shift() function is “used to remove the first element from an array and returns the value of the removed element.” Syntax array_shift(array) Parameters array: It is an array parameter that is required, and it specifies an array. Return value The function returns the value of the shifted element from the array. Otherwise, NULL if the … Read more

PHP array_slice() Function

PHP array_slice() function is “used to fetch a part of an array by slicing through it, based on the user’s choice.” Syntax array_slice(array,start,length,preserve) Parameters array: This parameter is an input array. It is a required parameter.  start: This is a required parameter and a numeric value. Specifies where the function will start the slice: 0th … Read more

PHP str_repeat() Function

PHP str_repeat() function is “used to create a new string by repeating a given string a fixed number of times.” Syntax str_repeat(string, repeat) Parameters string: It is required and specifies a string to repeat. repeat: It is a required parameter specifying the number of times the string will be repeated. Must be greater or equal to … Read more

PHP array_values() Function

PHP array_values() function is “used to get an array of values from another array that may contain key-value pairs or just values.” Syntax array_values(array) Parameters array: It is an input array. Return value It returns an array with the fetched values, indexed with the numerical keys. Example 1: How to Use PHP array_values() function <?php … Read more

PHP array_splice() Function

PHP array_splice() function is “used to remove selected elements from an array and replaces it with new elements.” It returns an array with the removed elements. Syntax array_splice(array, start, length, array) Parameters array: It is the required parameter, and it specifies an array. start: It is the required parameter, a numeric value, and it specifies … Read more

PHP array_pop() Function

PHP array_pop() function is “used to delete or pop out and return the last element from an array passed to it as a parameter.” Syntax array_pop(array) Parameters array: It is required, which is the array whose elements will be removed. Return value It returns the last element of the array. NULL is returned if the … Read more

PHP array_unique() Function

PHP array_unique() function is “used to remove duplicate values from an array.” If there are multiple elements in the array with the same values, then the first appearing element will be kept, and all other occurrences of this element will be removed from the array. Syntax array_unique(array, [sortingType]) Parameters array: The array parameter is required, which specifies … Read more

PHP json_decode() Function

PHP json_decode() function is “used to decode a JSON string.” It converts a JSON-encoded string into a PHP variable. Syntax json_decode(string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]]) Parameters $json_string: The encoded string must be the UTF-8 encoded data. $assoc: It is the boolean type … Read more

PHP strtotime() Function

PHP strtotime() function is “used to convert an English textual date-time description to a UNIX timestamp.” Syntax strtotime(time, now); Parameters time:  It specifies a date/time string. now: It specifies the timestamp used to calculate the returned value. It is an optional parameter. Since the time/date is not static, the output will vary. Return value It … Read more

PHP array_reduce() Function

PHP array_reduce() function is “used to reduce the elements of an array into a single value that can be of float, integer or string value.” Syntax array_reduce(array, myfunction, initial_parameter) Parameters array: The parameter is required, and it specifies an array. myfunction: It is required and specifies the function’s name. initial_parameter: It is required and specifies … Read more

How to Create an Object in PHP

An object is an individual instance of the data structure defined by a class. We can define a class once and then make many objects that belong to it. Objects are also known as instances. Class in PHP Class is the programmer-defined data type, including local methods and local variables. Class is the collection of … Read more

PHP json_encode(): How to Convert PHP Array to JSON

PHP json_encode() function is “used to convert a value to JSON value”. It returns a string containing the JSON representation of the supplied value. If the parameter is an array or object, it will be serialized recursively. Syntax json_encode(value, options) Parameters value: It is a required parameter of type mixed. options: It is a Bitmask … Read more

How to Remove All White Spaces from a String in PHP

Here are 3 ways to remove all whitespace from a String in PHP, Using “str_replace()” function Using “str_ireplace()” function Using “preg_replace()” function Method 1: Using the str_replace() Method PHP str_replace() method is “used to replace all the occurrences of the search string (” “) by replacing string (“”) in the given string str.” Syntax str_replace($searchVal, … Read more

PHP array_replace() Function

PHP array_replace() function is “used to replace the values of an array (array1) with the values from another array (s) based on key/index.” Syntax array_replace(array1,array2,array3…) Parameters array1: This parameter is required. array2: This parameter is optional, and it specifies an array that will replace the values of array1. array3: This parameter is optional, specifying more … Read more

PHP array_map() Function

PHP array_map() function is “used to modify all the elements of one or more arrays according to user-made function.” Syntax array_map(myfunction,array1,array2,array3…) Parameters myfunction: It is required, and it is the name of the User Defined Function. array1: It is a required parameter, and it specifies an array. array2, array3: They are optional parameters, and they also specify … Read more