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
Check if a PHP cookie exists and if not set its value
In PHP, cookies are available only on subsequent page loads after being set. However, you can check if a cookie exists and set it if it doesn't using isset() with the $_COOKIE superglobal. Understanding Cookie Behavior According to the PHP manual: "Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays." Cookies are response headers sent to the browser, and the browser must send them back with the next request, making them available only on the second page load. Checking and Setting Cookies You can ...
Read MoreHow to import csv file in PHP?
In PHP, you can import and read CSV files using the built-in fgetcsv() function. This function reads a line from a file pointer and parses it for CSV fields, making it easy to process CSV data row by row. Syntax The fgetcsv() function has the following syntax − fgetcsv($handle, $length, $delimiter, $enclosure, $escape) Parameters $handle − File pointer resource $length − Maximum line length to read $delimiter − Field delimiter (default: comma) $enclosure − Field enclosure character (default: double quote) $escape − Escape character (default: backslash) Example The following ...
Read MoreHow to validate domain name in PHP?
In PHP, you can validate domain names using regular expressions with the preg_match() function. A valid domain name must follow specific rules: contain only alphanumeric characters and hyphens, have proper length limits, and follow DNS naming conventions. Example Here's a function to validate domain names ? The output of the above code is ? Domain: tutorialspoint.com - Valid Domain: sub.example.org - Valid Domain: test-site.net - Valid Domain: https://tutorialspoint.com - Invalid Domain: -invalid.com - Invalid Domain: toolongaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.com - Invalid How It Works The validation function uses three regular expression ...
Read MoreHow to install Imagick/imagemagick PHP extension on Windows 10?
ImageMagick is a powerful image manipulation library, and the Imagick PHP extension provides an interface to use ImageMagick functions within PHP scripts. Installing it on Windows 10 requires downloading the appropriate binaries and configuring PHP properly. Prerequisites: Ensure you have PHP installed on Windows 10 with a web server like XAMPP, WAMP, or standalone Apache. Step 1: Download ImageMagick First, download the ImageMagick binary for Windows from the official website ? Visit https://imagemagick.org/script/download.php#windows Download the appropriate version matching your PHP architecture (x86 or x64) Install ImageMagick to the default directory (usually C:\Program Files\ImageMagick) ...
Read MoreGetting size in memory of an object in PHP?
In PHP, you can determine the memory size of an object by using the memory_get_usage() function before and after object creation. This approach measures the difference in memory consumption to estimate the object's size. Basic Memory Measurement The most straightforward method involves capturing memory usage before and after object instantiation − Object memory size: 440 bytes Current memory usage: 2097720 bytes Memory Measurement with Real Memory For more accurate results, use the real_usage parameter to get actual memory allocated by the system − ...
Read MorePHP Call methods of objects in array using array_map?
In PHP, you can call methods of objects in an array using array_map() function. This is useful when you need to extract the same property or call the same method on multiple objects. Using Anonymous Functions (PHP 5.3+) The most straightforward approach uses anonymous functions − Array ( [0] => Laptop [1] => Mouse [2] => Keyboard ) Array ( [0] => 999.99 [1] => 25.5 [2] => ...
Read MoreIs it possible to have a HTML SELECT/OPTION value as NULL using PHP?
In HTML forms, SELECT/OPTION elements cannot have a truly NULL value when submitted via POST or GET. HTTP form data is always transmitted as strings, so the closest you can get to NULL is an empty string, which you can then convert to NULL in your PHP code. Understanding Form Data Transmission When a form is submitted, all values are sent as strings. Even if you set an option value to be empty, PHP receives it as an empty string (''). HTML Form Example ...
Read MorePHP equivalent of friend or internal
PHP doesn't natively support friend classes like C++, but it can be simulated using magic methods __get() and __set() along with debug_backtrace(). This approach allows specific classes to access private properties, though it's considered a workaround rather than best practice. Simulating Friend Classes Here's how to implement friend-like behavior by checking the calling class in the backtrace ? This is private data Alternative Approach: Protected Properties A cleaner approach is using protected properties with inheritance ? Accessible to subclasses Key Points ...
Read MoreHow do I include a php.ini file in another php.ini file?
While PHP doesn't support directly including one php.ini file within another, you can achieve modular configuration through PHP's additional configuration directories feature. This approach allows you to split configurations across multiple .ini files. Using Configuration Scan Directory When compiling PHP from source, you can specify an additional directory for configuration files using the following compile option − --with-config-file-scan-dir=PATH The PATH parameter specifies the directory where PHP will scan for additional .ini files during startup. How It Works During PHP initialization, the engine will − First load the main php.ini file ...
Read MoreTracking Memory Usage in PHP
The memory_get_usage() function can be used to track the memory usage in PHP. The 'malloc' function is not used for every block required, instead a big chunk of system memory is allocated and the environment variable is changed and managed internally. The two different types of memory usages are − The memory required by the engine from OS (the real usage) The amount of memory that was actually used by the application (internal usage) The above mentioned memory usage can be tracked using memory_get_usage(). This function returns both real and actual memory used depending on ...
Read More