Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Articles on Trending Technologies
Technical articles with clear explanations and examples
How to create Web API service in PHP?
Web APIs in PHP allow different applications to communicate and exchange data over HTTP. SOAP and REST are the most widely used API architectures for building web services. Creating a Simple REST API Let's start with a basic PHP class that manages database entries ? Creating the API Endpoint To make this functionality accessible to external applications, create an API endpoint that acts as an interface ? Complete REST API Example Here's a more comprehensive API that handles multiple HTTP methods ? ...
Read MoreConvert spaces to dash and lowercase with PHP
In PHP, you can convert spaces to dashes and make text lowercase using a combination of str_replace() and strtolower() functions. This is commonly used for creating URL-friendly strings or slugs. Basic Approach The most straightforward method is to combine strtolower() with str_replace() to first convert the string to lowercase, then replace spaces with dashes ? hello-have-a-good-day-everyone Using preg_replace() for Multiple Spaces For handling multiple consecutive spaces, preg_replace() with regular expressions provides better control ? hello-have-multiple-spaces Complete URL Slug Function Here's ...
Read MorePHP file that should run once and delete itself. Is it possible?
Yes, it is possible to create a PHP file that deletes itself after execution. This can be achieved using PHP's unlink() function along with the __FILE__ magic constant. Simple Self-Deletion The most straightforward approach is to call unlink() at the end of your script ? Using Destructor Method A more robust approach uses a class destructor to ensure the file is deleted even if the script exits unexpectedly ? Key Considerations When implementing self-deleting scripts, keep these important points in mind ? File Permissions: ...
Read MoreParsing JSON array with PHP foreach
In PHP, you can parse JSON arrays using json_decode() and iterate through them with foreach. The json_decode() function converts JSON strings into PHP arrays or objects. Syntax json_decode($json_string, $associative = false) Parameters Parameter Description $json_string The JSON string to decode $associative When true, returns associative array instead of object Example Here's how to parse a JSON array and access its nested values using foreach ? Output This will produce the following output ? 3490abc ...
Read MoreHow to use RegexIterator in PHP?
RegexIterator is a built-in PHP class that filters iterators using regular expressions. It's particularly useful for filtering file listings, array elements, or any iterable data based on pattern matching. Basic File Filtering The most common use case is filtering files in a directory based on their names or paths −
Read MorePHP - How to use $timestamp to check if today is Monday or 1st of the month?
In PHP, you can use the date() function with a timestamp to check if today is Monday or the 1st of the month. The date() function formats a timestamp based on the specified format string. The timestamp parameter is optional and defaults to the current time if not provided. Syntax date(format, timestamp) Parameters format − Required. Specifies the format of the outputted date string timestamp − Optional. Specifies a Unix timestamp. Default is the current date and time Example Here's how to check if a given timestamp represents Monday ...
Read MoreCreate nested JSON object in PHP?
In PHP, you can create nested JSON objects using associative arrays with the json_encode() function. This allows you to build complex data structures that can be easily converted to JSON format. Creating Nested JSON Structure Here's how to create a nested JSON object using associative arrays ? {"client":{"build":"1.0", "name":"MyApp", "version":"1.0"}, "protocolVersion":4, "data":{"distributorId":"DIST001", "distributorPin":"PIN123", "locale":"en-US"}} Pretty Formatted JSON To make the JSON output more readable, use the JSON_PRETTY_PRINT flag ? { "client": { ...
Read MoreAdd PHP variable inside echo statement as href link address?
In PHP, you can include variables inside echo statements to create dynamic href link addresses. There are several approaches depending on whether you're using PHP within HTML or generating HTML from PHP. Method 1: Concatenation with Echo Use string concatenation to combine the variable with the HTML anchor tag ? Visit TutorialsPoint Conclusion All methods achieve the same result − embedding PHP variables in href attributes. Use concatenation for simple cases, variable interpolation for readability, and printf() for complex formatting with multiple variables.
Read MoreHow do I sort a multidimensional array by one of the fields of the inner array in PHP?
In PHP, you can sort a multidimensional array by one of the fields of the inner arrays using the usort() function with a custom comparison function, or using array_multisort() for simpler cases. Using usort() with Custom Comparison Function The usort() function sorts an array using a user-defined comparison function. This method provides the most flexibility for complex sorting logic ? Product B - $15 Product A - $25 Product C - $35 Using array_multisort() For simpler sorting by a single field, array_multisort() provides a more direct approach ? ...
Read MoreConvert all types of smart quotes with PHP
Smart quotes are special Unicode quotation marks that appear curved or angled, unlike regular straight quotes. In PHP, you can convert these various smart quote types to standard ASCII quotes using character mapping and string replacement ? Basic Smart Quote Conversion The following function converts common smart quotes to regular quotes using a character mapping array ?
Read More