PHP array_count_values() Function

PHP array_count_values() function is “used to count the occurrences of each unique value in an array.” Syntax array array_count_values($array ) Parameters $array(required): This parameter is the array for which we need to calculate the count of values present in it. Return value It returns an associative array with key-value pairs in which keys are the … Read more

PHP strlen() Function

PHP strlen() function “returns the length of a given string.”  Please note that strlen returns the number of bytes rather than the number of characters in a string. Syntax strlen(string) Parameters  string(required): It specifies the string to check. Return value It returns the length of the string, including all the whitespaces and special characters. If the … Read more

PHP array_diff() Function

PHP array_diff()  function is “used to compare the values of two (or more) arrays.” It returns the values in the first array that are not present in any of the 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 array to compare from, and 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. Visual Representation Here is the step-by-step guide to configure cURL in PHP. Open your php.ini file (you can find … Read more

PHP trim() Function: Remove Characters from String

PHP trim() function is “used to remove whitespace or any specific characters from both the left and right sides of a string.” Syntax trim($string, $charlist) Parameters $string(required): The string to be trimmed. $charlist(optional): It specifies the characters to be removed from the string. If not specified, it will default to trimming the following characters: “\0” … Read more

PHP array_chunk() Function

PHP array_chunk() function is “used to split an array into parts or chunks of new arrays“. The size of each chunk is determined by the size parameter that you pass to the function. The last chunk may contain fewer elements than the specified size. Syntax array_chunk($array,size,preserve_key); Parameters $array: The parameter is required, which specifies an … 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(required): It is 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(optional): It specifies the number … 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, strict) Parameters search(required): It is the value that needs to be searched in an array. $array(required): It is the array in which … 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 … Read more

PHP unset() Function

PHP unset() function is “used to unset a specified variable.” If unset() is called inside a user-defined function, it unsets the local variables. To unset a global variable inside the function, the $GLOBALS array needs to be used. Syntax unset ($var1, $var2…. ) Parameters $var1, $var2: It is the variable which is needed to be … Read more

PHP strcmp() Function

PHP strcmp() function is “used to compare two strings.” This function is case-sensitive and binary-safe(safe to use on binary data as well as regular text strings). Syntax strcmp($string1, $string2) Parameters $string1 (required): The first string. $string2 (required): The second string. Return Value The function returns the random integer value depending on the condition of the … Read more

PHP array_reverse() Function

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

PHP sort() Function

PHP sort() function is “used to sort an array in ascending order.”  Syntax sort($array, sorttype) Parameters $array($required): It specifies the array to sort. sorttype(optional): There are 6 sorting types which are described below SORT_REGULAR(Default) – compare items normally (don’t change types) SORT_NUMERIC – compare items numerically SORT_STRING – compare items as strings SORT_LOCALE_STRING – The … Read more

PHP ksort() Function

PHP ksort() function is “used to sort an array in ascending order based on its keys while maintaining the key-value relationships.” Syntax ksort($array, sorttype) Parameters $array(required): It is an input array. $sorttype(optional): There are 6 different sorting types which are discussed below  SORT_REGULAR(Default) – Compare elements normally without changing types  SORT_NUMERIC – Compare elements numerically.  SORT_STRING … Read more

PHP empty() Function

To check whether a variable is empty in PHP, you can use the “empty()” function.  Syntax bool empty ($var) Parameters  $var(required): This variable is being checked. Return Value It returns FALSE when $var exists and has a non-empty(non-zero value) Otherwise, it returns TRUE. These values are considered to be an empty value: “(an empty string) … Read more

PHP isset() Function

PHP isset() function is “used to check if a variable is set and not NULL.” Syntax bool isset($var, mixed) Parameters $var(required): This is the variable to be checked. mixed(optional):  It indicates that a parameter may accept multiple types. Return value It returns TRUE, and If the variable exists and is not NULL else, it returns … Read more

PHP array_key_exists() Function

PHP array_key_exists() function is “used to check whether a specified key or index exists in an array.” 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(required): It is the value of the key to be checked. array(required): It is an input array … Read more

PHP array_product() Function

PHP array_product()  function is “used to calculate the product of all elements in an array.” This function multiplies all the values in the array together and returns the result. Syntax array_product($array) Parameters $array: It refers to the input array. Return value It returns the product of all the values as an integer or a float. … Read more

PHP array_udiff() Function

PHP array_udiff() function “compares the different values of two or more arrays using the user-defined function for 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 to compare against. The array3 parameter is Optional and … Read more

PHP array_fill_keys() Function

PHP array_fill_keys() function is “used to create a new array filled with the specified keys and values.” Syntax array_fill_keys($keys, $value) Parameters $keys(required): It is an array of values that will become keys in the new array. If the keys array contains any illegal value, it is converted into the string and used. $value(required): It defines … Read more

PHP array_intersect_key() Function

PHP array_intersect_key() function is “used to compare the keys of two or more arrays, and returns an array containing the entries of the first array which have keys that are present in all the other arrays.“ Syntax array_intersect_key($array1, $array2, $array3, …) Parameters $array1(required): The first array is the array that the keys are going to … Read more

PHP array_intersect() Function

PHP array_intersect() function is “used to compute the intersection of two or more arrays.” This function returns a new array containing all the values from the first array that also exist in all the other arrays. Keep in mind that keys are preserved. Syntax array_intersect(array1,array2,array3…); Parameters array1(required): The array to compare from. array2(required): The array to … Read more

PHP serialize() Function

PHP serialize() function is used for “converting a value (like an array, object, or even a simple data type) into a storable representation of its value”. Syntax serialize(value) Parameters  value(required): This is the value to be serialized. Return value It is a string that contains a byte-stream representation of value. The string can be stored … 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(required):  A multi-dimensional array or an array of objects from which the column values will be extracted. $column_key(required): The integer key or the string key name of the column of values to return. The column_key parameter can … Read more

PHP explode() Function

PHP explode() function is “used to split a string into an array.” It splits a string based on a specified delimiter. Syntax explode(separator,$string,limit) Parameters separator(required): The boundary string. $string(required): The input string to split. limit(optional): It specifies the number of array elements to return. This can be any integer. Positive: If a positive value is … Read more

PHP array_push() Function

PHP array_push() function is “used to insert one or more elements to the end of an 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 optional(PHP version 7.3.0+), which is the value we will insert into the specified array.  Return value … Read more

PHP array_filter() Function

PHP array_filter() function is used to filter the elements of an array using a callback function(user-defined function). If the callback function returns true, the current element is included in the result array. Array keys are preserved. Syntax array_filter($array, callbackfunction, flag) Parameters array(required): It specifies the array to filter callbackfunction(optional): It evaluates each element. flag(optional): Introduced … Read more

PHP implode() Function

PHP implode() function is used to join the elements of an array into a string. implode() is an alias for the PHP join() function. Syntax implode(separator,$array) Parameters separator(optional): It specifies what to put between the array elements. The default is an empty string(“”). $array(required):  It is the array to join the string. Return value It returns … 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 a new array with flipped keys and values, NULL on failure. Visual RepresentationExample 1: How to Use array_flip() Function <?php $arr = [“Apple”,”Google”, … Read more

How to Convert an Object to Associative Array in PHP

Here are the two ways to convert an object to an associative 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 Syntax $myArray = json_decode(json_encode($object), true); Example <?php class ST { function __construct($mike, $eleven) { $this->var1 = $mike; $this->var2 = $eleven; } … Read more

PHP substr() Function

PHP substr() function is “used to return a part(portion) of a string.” Syntax substr($string, start, length) Parameters $string(required): It is an input string. start(required): It is the position where to start the extraction. The first character is at index 0. If the start is non-negative, the returned string will begin with the start‘th position in … 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 variable is considered to be NULL if: If it has been explicitly set to the NULL value. It has not been set to any value yet. It has been unset(). Syntax is_null( mixed $var … Read more

PHP array_walk() Function

PHP array_walk() function “applies a callback function(user-defined function) to every element of the array.” Syntax array_walk($array, callback, parameter…) Parameters $array(required): It is an input array. callback(required): It is the name of a user-defined function. parameter(optional): If it is supplied, it will be passed as the third parameter to the user function. Return value It returns … 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 … 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(required): It specifies an array. Return value This function returns the value of the shifted element from the array. Otherwise, null if the array is empty. Visual Representation Example 1: How to … Read more

PHP array_slice() Function

PHP array_slice() function is used to extract a portion of an array. Syntax array_slice(array,start,length,preserve) Parameters array(required): This parameter is an input array. start(required): It Specifies where the function will start the slice: 0th index = the first element. length(optional): It is a numeric value. If this value is set to a negative number, the function … Read more

PHP str_repeat() Function

PHP str_repeat() function is “used to repeat a string a specified number of times.“ Syntax str_repeat($string, $repeat) Parameters $string(required): It is the string to be repeated. $repeat(required): It specifies the number of times the string should be repeated. Must be greater or equal to 0. If $repeat is set to 0, the function will return … Read more

PHP array_values() Function

PHP array_values() function returns all the values from an array. This function indexes the array numerically. Syntax array_values($array) Parameters array(required): It is an input array. Return value This method returns an array with the fetched values. Visual Representation Example 1: How to Use array_values() function <?php $arr = [ ‘a’ => ‘Apple’, ‘b’ => ‘Microsoft’, … Read more

PHP array_splice() Function

PHP array_splice() function is used to remove selected elements from an array and replace it with new elements.  Syntax array_splice(array1, start, length, array2) Parameters array1(required): It specifies an array. start(required): It is a numeric value, and it specifies where the function will start removing elements: 0th index = the first element. length(optional): It is a numeric … Read more

PHP array_pop() Function

PHP array_pop() function is “used to remove the last element of an array.” It reduces the size of an array by one since the last item is removed from the array. Syntax array_pop($array) Parameters array(required): It is the array whose element will be removed. Return value It returns the last element of the array. null … 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. Important points to know: The keys of array are preserved. … 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. It’s the inverse operation of json_encode(). Syntax json_decode(string $json [, bool $assoc = FALSE [, int $depth = 512 [, int $options = 0 ]]]) Parameters $json(required): This is the JSON string to be decoded. It … Read more

PHP strtotime() Function

PHP strtotime() function is used to convert an English textual date-time description to a UNIX timestamp(the number of seconds since January 1 1970 00:00:00 UTC). Syntax strtotime($time, $now); Parameters $time(required):  It specifies a date/time string. $now(optional): It specifies the timestamp used to calculate the returned value. It is an optional parameter. Since the time/date is not … Read more

PHP array_reduce() Function

PHP array_reduce() function is used to reduce the elements of an array into a single value using a callback function(user-defined function). Syntax array_reduce($array, callaback, $initial_parameter) Parameters $array(required):  It specifies an input array. callback(required): It refers to the user-defined function that accepts values from the $array. $initial_parameter(optional): It is the initial value to pass to the … Read more

How to Create an Object in PHP

An object is an instance of a class. You create a class with properties and methods. Once the class is created, you can access the object’s properties and methods using the object variable. Define a Class Class is the programmer-defined data type, including local methods and local variables. It is the collection of objects. The … Read more

PHP json_encode() Function

PHP json_encode() function is “used to convert a PHP array or object into a JSON-formatted string”. Syntax json_encode($value, $options, $length) Parameters $value(required): It is the value to be encoded. $options(optional): It is a Bitmask comprising of JSON_HEX_QUOT, JSON_HEX_TAG, JSON_HEX_AMP,JSON_HEX_APOS,JSON_NUMERIC_CHECK,JSON_PRETTY_PRINT,JSON_UNESCAPED_SLASHES, JSON_FORCE_OBJECT,among others. $length(optional):  Sets the maximum depth, which must be greater than zero. Return Value This … 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 (” “) with the replacing string (“”) in the given string. Syntax str_replace($searchVal, … Read more

PHP array_replace() Function

PHP array_replace() function is used to replace the values of an array with the values from one or more arrays based on key/index. Points to remember: If a key from the first array exists in the second array, its value will be replaced by the value from the second array. If a key from the … 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-defined function. Syntax array_map($callback,$array1,$array2,$array3…) Parameters $callback(required): The callback function to run for each element in each array. $array1(required): It specifies an array to be modified. $array2, $array3(optional): They also specify the arrays to be modified. Return Value It … Read more

PHP count_chars() Function

PHP count_chars() function is used to count the number of times each ASCII character occurs in a string.  Syntax count_chars($string, return_mode); Parameters $string(required): It is the input string. return_mode(optional): This parameter defines an operation that needs to be performed on the string. It takes values 0, 1, 2, 3, 4. 0: If this mode is chosen, … Read more