• 12 December

    Remove Last Character from String in PHP

    This article explains different ways to remove the last character from a string in PHP Use the substr() function To remove last character from String in PHP, use of the substr() function and pass a negative length value as argument. Here is an example on how to use the substr() function to achieve the operation […]

  • 12 December

    Check if Variable is Array in PHP

    Using the is_array() function To check if variable is array in PHP, use is_array() function. The is_array() function is a built-in function in PHP that takes a variable as an argument and returns a Boolean value indicating whether the variable is an array. Here is an example of how to use this function: [crayon-63db704f8aadd242919071/] Output […]

  • 12 December

    Count Lines in File in PHP

    To count the number of lines in a file in PHP, you can use one of the following approaches: Use the file() function This function reads the entire file into an array, with each line of the file as an element in the array. Then, use the count() function to count the number of elements […]

  • 12 December

    Split String by Comma in PHP

    This article explains how to split a string by comma in PHP using different approaches. Use the explode() function To split string by comma, we can make use of the explode function. This function splits the string by the specified delimiter and returns an array of substrings. Here is example of how to split string […]

  • 08 December

    Format Number to 2 Decimal Places in PHP

    Using the round() function To format a number to 2 decimal places in PHP, you can use the round() function and pass the number and 2 as arguments to the round() function. Here is an example of how to use the round() function to format a number to 2 decimal places: [crayon-63db704f8bf23360127813/] Output [crayon-63db704f8bf2a603151778/] round() […]

  • 08 December

    Check if Array is Empty in PHP

    Use empty() function To check if an array is empty in PHP, you can use the empty() function. This function accepts an array as its argument, and it returns true if the array is empty and false if the array is not empty. Here is an example of how to use the empty() function to […]

  • 05 December

    Replace Space with Underscore in PHP

    Using str_replace() Method Use the str_replace() function to replace with underscore in PHP, for example: str_replace(" " , "_", $str). str_replaces() returns new String with old character space( ) replaced by underscore(_) [crayon-63db704f8cbc6384331936/] [crayon-63db704f8cbcc411109638/] We used str_replace() to replace space with underscore in PHP. str_replace() replaces all instances of search string with replacement String. It […]

  • Extract numbers from String in PHP
    04 December

    Extract Numbers From String in PHP

    Using filter_var() method We can use the filter_var() method which filters a variable based on supplied filter flag or ID. To extract numbers from the string variable, we can pass the FILTER_SANITIZE_NUMBER_INT id. Let’s see an example using this method. [crayon-63db704f8d0f5840574208/] [crayon-63db704f8d0fb088208199/] This approach isn’t efficient for certain use case, and simply returns the numbers […]

  • 04 December

    Create Array of Objects in PHP

    An array can contain a sequence of elements of different data types. From strings to integers to floats to objects. In this post, we will discuss the different ways to how to create an array of objects in PHP. Using [] syntax We can use the short array syntax – [] – to create an […]