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 More
Assembly language is a low-level programming language that provides a human-readable representation of machine code. The GNU Compiler Collection (GCC) allows us to convert C/C++ source code into assembly language for analysis and optimization purposes. Syntax gcc -S source_file.c gcc -S source_file.cpp Note: To use gcc, you need to install it on your system. On Ubuntu/Debian: sudo apt install gcc, on Windows: install MinGW or use WSL. Parameters -S − Generate assembly code and stop before assembling source_file − The C/C++ source file to convert Example: Simple C ... Read More
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 More
The free() function is used to deallocate memory that was allocated using malloc(), calloc(), or realloc(). The syntax of free() is simple − it only takes a pointer as an argument and deallocates the memory. Syntax void free(void *ptr); The interesting question is: how does free() know the size of the memory block to deallocate when it only receives a pointer? How free() Determines Memory Block Size When you allocate memory using dynamic memory allocation functions, the memory management system stores metadata about each allocated block. This metadata typically includes the size of ... Read More
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 More
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 More
In C, compound literals are a feature introduced in the C99 standard that allows you to create unnamed objects with automatic storage duration. This feature enables you to initialize arrays, structures, and unions directly in expressions without declaring a separate variable. Syntax (type_name) { initializer_list } Example 1: Compound Literal with Structure Here's how to use compound literals to create an unnamed structure object − #include struct point { int x; int y; }; void display_point(struct point pt) { ... Read More
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 More
In C programming, multiline macros allow you to define complex operations that span multiple lines. Unlike single-line macros, each line in a multiline macro must be terminated with a backslash '' character to indicate line continuation. When using multiline macros, it's recommended to enclose the entire macro definition in parentheses to avoid potential syntax errors. Syntax #define MACRO_NAME(parameters) ({\ statement1;\ statement2;\ statement3;\ }) Example 1: Basic Multiline Macro Here's an example demonstrating how to create and use a multiline macro for printing ... Read More
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 More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Economics & Finance