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 read a single file inside a zip archive with PHP
To read a single file inside a zip archive, PHP provides a simple stream wrapper syntax that allows you to access files directly without extracting the entire archive. Syntax The basic syntax uses the zip:// stream wrapper − zip://path/to/archive.zip#filename_inside_zip Example Here's how to read a specific file from a zip archive using fopen() − Using file_get_contents() For simpler cases, you can use file_get_contents() directly − Key Points The # symbol separates the zip file path from the internal file path ...
Read MorePHP_CodeSniffer, PHPMD or PHP Depend
PHP offers several powerful code analysis tools to help maintain code quality and enforce coding standards. Three essential tools − PHP_CodeSniffer (phpcs), PHPMD, and PHP Depend (pdepend) − each serve different purposes in code quality assessment. PHP_CodeSniffer (phpcs) PHP_CodeSniffer tokenizes PHP, JavaScript, and CSS files to detect violations against predefined coding standards. It ensures code consistency and helps prevent common semantic errors. Installation: Install via Composer: composer global require "squizlabs/php_codesniffer=*" Example Usage Here's how to check a PHP file against PSR−12 coding standard ? Command to run PHP_CodeSniffer ...
Read MorePassing static methods as arguments in PHP
In PHP, you can pass static methods as arguments to functions using the same syntax employed by is_callable() and call_user_func(). This allows for flexible method calling and dynamic programming patterns. Syntax Static methods can be passed using several formats − // Array format: [ClassName, 'methodName'] // String format: 'ClassName::methodName' Example with Static Method Here's how to pass and call a static method as an argument − bool(true) Hello from static method! bool(true) 15 Passing to Custom Functions You can create functions that accept static ...
Read MorePHP string{0} vs. string[0];
In PHP, you can access individual characters in a string using two syntaxes: $string{0} and $string[0]. However, the curly brace syntax {} has been deprecated since PHP 7.4 and removed in PHP 8.0. Deprecated Syntax vs. Current Syntax The following example demonstrates both approaches − The output of the above code is − m m Why Use Square Brackets Square brackets [] are now the standard way to access string characters because they provide consistent syntax with array indexing. This makes PHP code more uniform and easier to ...
Read MorePass arguments from array in PHP to constructor
The Reflection API can be used to pass arguments from array to constructor. This is particularly useful when you need to dynamically instantiate classes with variable constructor parameters. ReflectionClass::newInstanceArgs The newInstanceArgs() method creates a new class instance from given arguments − public ReflectionClass::newInstanceArgs ([ array $args ] ) : object It creates a new instance of the class when the arguments are passed to the constructor. Here, args refers to the array of arguments that need to be passed to the class constructor. Example with Built-in Class Here's an example using PHP's ...
Read MoreEncrypting Passwords in PHP
Password encryption in PHP is crucial for securing user credentials. While older methods like MD5 and SHA-1 are vulnerable, modern PHP offers robust solutions including crypt() with SHA-256/SHA-512 and the newer password_hash() function. Using SHA-256 and SHA-512 with crypt() Due to Blowfish vulnerabilities before PHP 5.3.7, SHA-256 and SHA-512 are recommended alternatives. Both use a similar salt format − $5$ prefix for SHA-256 and $6$ prefix for SHA-512, with optional rounds parameter for multiple hashing ? SHA-256 (no rounds): $5$YourSaltyStringz$td0INaoVoMPD4kieVrkGE67siKj3N8.HSff8ep0Ybs8 SHA-512 (with rounds): $6$rounds=1000$YourSaltyStringz$A5UHscsEbSnPnaV6PmSF5T/MQK.Wc3klA.18c.gXG5pD0PVYSVr/7xwRu1XJyn8XpiMDNRTvpJm5S8DkmSywz1 The salt is 16 characters long ...
Read MoreChecking 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 More