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
Programming Articles
Page 1051 of 2547
Checking memory_limit in PHP
The 'memory_limit' is the maximum amount of server memory that a single PHP script is allowed to use. You can check the current memory limit using ini_get('memory_limit') and convert it to bytes for comparison. Basic Memory Limit Check Here's how to get and display the current memory limit ? Current memory limit: 128M Converting Memory Limit to Bytes To compare memory limits, you need to convert values like "64M" or "512K" to bytes ? Memory limit: 128M In bytes: 134, 217, 728 bytes ...
Read MoreSimplest way to detect client locale in PHP
PHP provides a function beginning from 5.3.0 to parse the $_SERVER['HTTP_ACCEPT_LANGUAGE'] variable into a locale using the Locale::acceptFromHttp() method. Note: This requires the PHP intl extension to be installed. You can install it using sudo apt-get install php-intl on Ubuntu/Debian systems. Example The following example shows how to detect client locale ? Output This will produce the following output ? en_US How It Works The $_SERVER['HTTP_ACCEPT_LANGUAGE'] variable contains the Accept-Language HTTP header sent by the client's browser. Most browsers submit this header based on ...
Read MoreReading/Writing a MS Word file in PHP
Microsoft strongly advises against using COM objects for Office document automation, stating: "Microsoft does not currently recommend or support the Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment." Fortunately, .docx files can be created and manipulated without COM objects since they have XML foundations. Libraries like PHPWord and PhpSpreadsheet provide excellent solutions for working with Word documents in PHP. Installation Install PHPWord using Composer: composer require phpoffice/phpword ...
Read MorePHP: recreate and display an image from binary data
In PHP, you can recreate and display an image from binary data using data URIs. This technique embeds image data directly into HTML without requiring external image files. Data URI Format Data URIs follow this format − data:[][;charset=][;base64], Creating a Data URI Function Here's a PHP function that converts an image file to a data URI ? Displaying Image from Binary Data You can display the image directly in HTML using the data URI ?
Read MoreConvert ASCII TO UTF-8 Encoding in PHP?
In PHP, you can convert ASCII to UTF-8 encoding using built-in functions like iconv() and mb_convert_encoding(). Both functions provide different approaches for character encoding conversion. Using iconv() Function The iconv() function converts strings between different character encodings. It requires specifying the source and target encodings − Original ASCII: Hello World UTF-8 Encoded: Hello World Example with Special Characters Original: café To UTF-8: café Using mb_convert_encoding() Function The mb_convert_encoding() function provides automatic encoding detection and conversion − ...
Read MoreDownload file through an AJAX call in PHP
While AJAX isn't ideal for file downloads due to browser security restrictions, you can trigger file downloads from PHP using JavaScript redirection methods like window.location or by creating downloadable links dynamically. Using window.location for File Download The simplest approach is to redirect the browser to a PHP script that serves the file ? function downloadFile(fileId) { window.location.href = '/download.php?file=' + fileId; } The corresponding PHP download script ?
Read MoreParse HTML with PHP's HTML DOMDocument
PHP's DOMDocument class provides powerful tools for parsing and manipulating HTML content. You can extract specific elements using XPath queries or DOM traversal methods. Example The following example shows how to extract text from nested elements using XPath −
Read MoreHow can I get PHP to display the headers it received from a browser?
PHP provides several methods to display HTTP headers received from a browser. You can access individual headers through the $_SERVER superglobal or retrieve all headers using the getallheaders() function. Using $_SERVER Superglobal To access a specific header, use the $_SERVER array with the header name prefixed by HTTP_ ? Using getallheaders() Function The getallheaders() function returns all HTTP request headers as an associative array ? Alternative Method Using $_SERVER You can also extract all HTTP headers by filtering the $_SERVER array ? ...
Read MoreHow to get the length of longest string in a PHP array
In PHP, you can find the length of the longest string in an array by combining array_map() and max() functions. The array_map() function applies strlen() to each element, and max() returns the highest value. Syntax $max_length = max(array_map('strlen', $array)); Example Here's how to find the longest string length in a PHP array − Length of longest string: 8 How It Works The process involves two steps: array_map('strlen', $array) creates a new array containing the length of each string max() function finds the maximum ...
Read MoreReturning two values from a function in PHP
In PHP, you cannot directly return two variables from a function, but you can achieve this by returning them in an array or using other data structures. This approach allows you to return multiple values and access them efficiently. Method 1: Using Indexed Array The most common approach is to return an array containing the values − Name: John Age: 25 Method 2: Using Associative Array For better readability, use associative arrays with meaningful keys − Area: 15 Perimeter: 16 Method ...
Read More