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
PHP Articles
Page 49 of 81
Passing 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 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 More