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 6 of 81
How to Remove Extension from String in PHP
In PHP, removing the file extension from a string is a common task when working with filenames. There are several effective methods to accomplish this, each with its own advantages depending on your specific needs. Using the pathinfo() Function The pathinfo() function is the most straightforward and reliable method to remove file extensions ? exampleFile The pathinfo() function with PATHINFO_FILENAME constant returns the filename without the extension. This method is safe and handles edge cases well. Using substr() and strrpos() Functions This method finds the last dot position ...
Read MoreHow to Recursively Delete a Directory and its Entire Contents (files + sub dirs) in PHP
In PHP, recursively deleting a directory and its entire contents requires careful handling of files and subdirectories. PHP provides several approaches to accomplish this task using built-in functions and classes. Method 1: Using rmdir() and unlink() Functions This method uses scandir() to list directory contents, then recursively processes each item − This approach first checks if the path is a directory, scans its contents, and recursively deletes subdirectories while unlinking files directly. Method 2: Using glob() Function The glob() function provides a cleaner way to retrieve directory contents using pattern matching ...
Read MoreHow to Read if a Checkbox is Checked in PHP
In PHP, you can check if a checkbox is checked by using several methods depending on how your form is structured. When a checkbox is checked, its value is included in the form submission data; when unchecked, it's not sent at all. Using isset() Function The most reliable method is using isset() to check if the checkbox value exists in the submitted data − Subscribe to newsletter Using empty() Function You can also use empty() for a ...
Read MoreHow to Read any Request Header in PHP
PHP provides several methods to read HTTP request headers, which contain important information about the client's request such as user agent, content type, and authentication details. Understanding how to access these headers is essential for web development tasks like content negotiation, security checks, and API development. Using $_SERVER Superglobal The most common method is using the $_SERVER superglobal array. Header names are prefixed with "HTTP_", converted to uppercase, and hyphens are replaced with underscores − Using getallheaders() Function The getallheaders() function returns all request headers as an associative array. This method preserves ...
Read MoreHow to Log Errors and Warnings into a File in PHP
In PHP, logging errors and warnings to files is essential for debugging and monitoring applications. PHP provides built-in functions like error_log() and ini_set() to capture and store error messages for later analysis. Method 1 − Using error_log() Function The error_log() function sends error messages to the server's error log or a specified file ? Syntax error_log(string $message, int $messageType = 0, string $destination = '', string $extraHeaders = ''); Parameters $message: The error message or data to be logged (string) $messageType: (Optional) The ...
Read MoreHow to Pass PHP Variables by Reference
In PHP, you can pass variables by reference instead of by value using the ampersand (&) symbol. This allows you to modify the original variable within a function or method. There are primarily two ways to pass PHP variables by reference: Using the ampersand in the function/method declaration Using the ampersand when passing the variable to a function/method Using the Ampersand in the Function/Method Declaration To pass variables by reference using the ampersand in the function/method declaration, you need to include the ampersand symbol before the parameter ...
Read MoreHow to get the Last n Characters of a PHP String
In PHP, you can extract the last n characters of a string using the substr() function with a negative start position. This approach counts from the end of the string backwards. Using substr() Function The substr() function extracts a portion of a string based on the starting position and optional length. To get the last n characters, use a negative starting position. Syntax substr(string $string, int $start, ?int $length = null): string|false Parameters: $string − The input string from which to extract characters $start ...
Read MoreHow To Get Multiple Selected Values Of Select Box In PHP
In PHP, when you have a multiple select box (dropdown) that allows users to select more than one option, you need to handle the selected values as an array. This is accomplished by setting the name attribute with square brackets and using PHP's superglobal arrays to retrieve the values. Method 1: Using $_POST with Array Name Attribute The most common approach is to set the name attribute of the select element as an array using square brackets. This tells PHP to treat the submitted values as an array − HTML Form Structure ...
Read MoreHow to Generate a Random, Unique, Alphanumeric String in PHP
Generating random, unique, alphanumeric strings in PHP is a common requirement for creating secure tokens, session IDs, passwords, or unique identifiers. PHP offers several built−in functions to accomplish this task with different levels of security and customization. Method 1 − Using str_shuffle() Function The str_shuffle() function randomly shuffles all characters in a string. This method allows full control over the character set used ? lEXq1dNm koSCdUgDpbLJ Method 2 − Using md5() Function The md5() function creates a 32−character hexadecimal hash from a unique identifier. This method produces lowercase alphanumeric ...
Read MoreHow to check if URL Contain Certain String using PHP
In PHP, you can check if a URL contains a certain string using built−in functions like strpos() for simple substring matching or preg_match() for pattern matching with regular expressions. Using strpos() Function The strpos() function finds the position of the first occurrence of a substring within a string. It returns the starting index if found, or false if the substring doesn't exist. Syntax int strpos( $string, $substring ) $string: The text where searching is performed. $substring: The pattern or substring to be searched. Example The URL ...
Read More