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
Server Side Programming Articles
Page 1060 of 2109
exit() function in PHP
The exit() function in PHP prints a message and terminates the current script execution immediately. It can accept an optional message parameter to display before exiting. Syntax exit(msg) Parameters msg − Optional. The message to display before exiting the script. Can be a string or an integer exit status code. Return Value The exit() function does not return any value as it terminates script execution. Example 1: Basic Usage Here's a simple example showing how exit() works − Before exit Script terminated! Example 2: File Operation with Error Handling The exit() function is commonly used for error handling when operations fail −
Read Moredie() function in PHP
The die() function in PHP prints a message and immediately exits the current script. It's commonly used for error handling when critical operations fail. Syntax die(message) Parameters message − Optional. The message to display before terminating the script execution. Return Value The die() function does not return any value as it terminates the script execution. Example 1: Basic Usage Here's a simple example demonstrating die() with a custom message − Script started Script terminated! Example 2: Error Handling with File Operations The die() function is frequently used with file operations for error handling −
Read Moredefine() function in PHP
The define() function defines a constant in PHP. Constants are global variables whose values cannot be changed during script execution. Syntax define(const_name, value, case_insensitive) Parameters const_name − The name of the constant (string). value − The value of the constant (scalar value). case_insensitive − Optional. Whether the constant name should be case-insensitive (default: false). Return Value The define() function returns true on success or false on failure. Examples Basic Example The following example defines a constant and accesses it directly ? ...
Read MoreFILTER_CALLBACK constant in PHP
The FILTER_CALLBACK constant calls a user−defined function to filter the value. This constant is used with filter_var() to apply custom filtering logic by passing a callback function. Syntax filter_var($value, FILTER_CALLBACK, array("options" => "callback_function")); Parameters The callback function is specified in the options array and receives the input value as a parameter. The function should return the filtered value. Example 1: Using Built−in Function This example converts a string to lowercase using PHP's built−in strtolower() function ? demo text! Example 2: Using Custom Function ...
Read MoreFILTER_SANITIZE_URL constant in PHP
The FILTER_SANITIZE_URL constant removes all illegal URL characters from a string, keeping only characters that are valid in URLs. This filter is useful for cleaning user input before using it in URL contexts. Allowed Characters The FILTER_SANITIZE_URL filter allows the following characters − $-_.+!*'(), {}|\^~[]`"> Original: https://example.com/search?q=hello world&type=web Sanitized: https://example.com/search?q=helloworld&type=web Key Points Spaces are removed from the input string Non-ASCII characters are stripped out Valid URL characters like ://, ?, &, and = are preserved This filter does not validate URLs, only sanitizes them Conclusion The ...
Read MoreFILTER_SANITIZE_STRING constant in PHP
The FILTER_SANITIZE_STRING constant removes HTML tags and encodes special characters from a string. This filter is useful for cleaning user input to prevent XSS attacks. Note: As of PHP 8.1.0, FILTER_SANITIZE_STRING is deprecated. Use htmlspecialchars() or custom validation instead. Syntax filter_var($value, FILTER_SANITIZE_STRING, $flags) Flags FILTER_FLAG_NO_ENCODE_QUOTES − Do not encode quotes FILTER_FLAG_STRIP_LOW − Removes characters with ASCII value less than 32 FILTER_FLAG_STRIP_HIGH − Removes characters with ASCII value greater than 127 FILTER_FLAG_ENCODE_LOW − Encodes characters with ASCII value less than 32 FILTER_FLAG_ENCODE_HIGH − Encodes characters with ASCII value greater than 127 FILTER_FLAG_ENCODE_AMP − ...
Read MoreFILTER_SANITIZE_SPECIAL_CHARS constant in PHP
The FILTER_SANITIZE_SPECIAL_CHARS constant filter HTML-escapes special characters like , &, and quotes to prevent XSS attacks and ensure safe display in HTML. Syntax filter_var($value, FILTER_SANITIZE_SPECIAL_CHARS, $flags) Flags FILTER_FLAG_STRIP_LOW − Strip characters with ASCII value below 32 FILTER_FLAG_STRIP_HIGH − Strip characters with ASCII value above 127 FILTER_FLAG_ENCODE_HIGH − Encode characters with ASCII value above 127 Return Value Returns the sanitized string with special characters HTML-encoded, or FALSE on failure. Example Here's how to sanitize a string containing HTML special characters ?
Read MoreFILTER_SANITIZE_NUMBER_INT constant in PHP
The FILTER_SANITIZE_NUMBER_INT constant removes all characters except digits, plus and minus signs from a string. It's commonly used to clean user input before processing numeric data. Syntax filter_var($value, FILTER_SANITIZE_NUMBER_INT) Parameters The filter_var() function with FILTER_SANITIZE_NUMBER_INT accepts − $value − The string to be sanitized FILTER_SANITIZE_NUMBER_INT − The filter constant that removes illegal characters Return Value Returns a string containing only digits (0-9), plus (+), and minus (-) signs. All other characters are removed. Example Here's how to sanitize a string containing mixed characters ?
Read MoreFILTER_SANITIZE_NUMBER_FLOAT constant in PHP
The FILTER_SANITIZE_NUMBER_FLOAT constant removes all illegal characters from a float number, keeping only digits, plus/minus signs, and optionally decimal points, thousand separators, or scientific notation based on the flags used. Syntax filter_var($value, FILTER_SANITIZE_NUMBER_FLOAT, $flags) Flags FILTER_FLAG_ALLOW_FRACTION − Allows decimal point (.) for fractions FILTER_FLAG_ALLOW_THOUSAND − Allows comma (, ) as thousand separator FILTER_FLAG_ALLOW_SCIENTIFIC − Allows e/E for scientific notation Return Value Returns the sanitized string with only valid float characters, or FALSE on failure. Example 1: Using ...
Read MoreFILTER_SANITIZE_ENCODED constant in PHP
The FILTER_SANITIZE_ENCODED constant in PHP is used with the filter_var() function to URL-encode special characters in a string. This is particularly useful for sanitizing URLs and ensuring they contain only valid characters. Syntax filter_var($value, FILTER_SANITIZE_ENCODED, $flags) Flags and Options FILTER_FLAG_STRIP_LOW − Remove characters with ASCII value less than 32 FILTER_FLAG_STRIP_HIGH − Remove characters with ASCII value greater than 127 FILTER_FLAG_ENCODE_LOW − Encode characters with ASCII value less than 32 FILTER_FLAG_ENCODE_HIGH − Encode characters with ASCII value greater ...
Read More