
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
PHP xml_set_start_namespace_decl_handler() Function
The PHP XML Parser xml_set_start_namespace_decl_handler() function is used to define a custom handler for the start of namespace declarations in an XML document.
It is part of the PHP XML Parser extension and is enabled when a new namespace prefix is added in the XML. This allows you to process or react to namespace information dynamically while parsing. The function needs an XML parser resource and a callback function to handle the namespace events. It makes simpler to handle XML documents that use namespaces properly.
Syntax
Below is the syntax of the PHP XML Parser xml_set_start_namespace_decl_handler() function −
true xml_set_start_namespace_decl_handler ( XMLParser $xml_parser, callable|string|null $handler )
Parameters
Here are the parameters of the xml_set_start_namespace_decl_handler() function −
$xml_parser: (Required) It is a reference to the XML parser to use inside the object.
$handler: (Required) It is used to specifies name of function that must exist when xml_parse().
Handler Function Signature
The signature of the handler is as follows −
void handler ( XMLParser $xml_parser, string|false $prefix, string $uri )
Parameters
Below are the parameters of the handler function −
$xml_parser: Specify a variable containing the XML parser calling the handler.
$prefix: It is the prefix string used to reference the namespace within an XML object. False if no prefix exists.
$uri: Uniform Resource Identifier (URI) of namespace.
Return Value
The xml_set_start_namespace_decl_handler() function returns TRUE on success. And FALSE on failure.
PHP Version
First introduced in core PHP 4.0.5, the xml_set_start_namespace_decl_handler() function continues to function easily in PHP 5, PHP 7, and PHP 8.
Example 1
Here is the basic example of the PHP XML Parser xml_set_start_namespace_decl_handler() function. This program adds a simple handler to collect namespace definitions in an XML file.
<?php // Basic Namespace Handling Example function startNamespaceHandler($parser, $prefix, $uri) { echo "Namespace declared: Prefix = $prefix, URI = $uri\n"; } // Create an XML parser $parser = xml_parser_create_ns(); // Set the namespace handler xml_set_start_namespace_decl_handler($parser, 'startNamespaceHandler'); // Parse a simple XML with a namespace $xml = '<root xmlns:ns="http://tutorialspoint.com/ns"><ns:child>Hello</ns:child></root>'; xml_parse($parser, $xml); // Free the parser xml_parser_free($parser); ?>
Output
Here is the outcome of the following code −
Namespace declared: Prefix = ns, URI = http://tutorialspoint.com/ns
Example 2
In the below PHP code we will use the xml_set_start_namespace_decl_handler() function and show how to dynamically manage several namespaces by logging the URIs and namespace prefixes in an associative array.
<?php // Store namespace declarations $namespaceData = []; function startNamespaceHandler($parser, $prefix, $uri) { global $namespaceData; $namespaceData[$prefix] = $uri; echo "Namespace added: Prefix = $prefix, URI = $uri\n"; } $parser = xml_parser_create_ns(); xml_set_start_namespace_decl_handler($parser, 'startNamespaceHandler'); $xml = '<root xmlns:x="http://tutorialspoint.com/x" xmlns:y="http://tutorialspoint.com/y"> <x:child>Hello</x:child> <y:child>World</y:child> </root>'; xml_parse($parser, $xml); xml_parser_free($parser); print_r($namespaceData); ?>
Output
This will generate the below output −
Namespace added: Prefix = x, URI = http://tutorialspoint.com/x Namespace added: Prefix = y, URI = http://tutorialspoint.com/y Array ( [x] => http://tutorialspoint.com/x [y] => http://tutorialspoint.com/y )
Example 3
Now the below code uses the xml_set_start_namespace_decl_handler() function and checks namespaces against a preset list of valid URIs to detect incorrect namespaces at the time of processing.
<?php // Allowed namespace URIs $allowedNamespaces = ["http://tutorialspoint.com/ns1", "http://tutorialspoint.com/ns2"]; function startNamespaceHandler($parser, $prefix, $uri) { global $allowedNamespaces; if (!in_array($uri, $allowedNamespaces)) { echo "Invalid namespace: URI = $uri\n"; } else { echo "Valid namespace: Prefix = $prefix, URI = $uri\n"; } } $parser = xml_parser_create_ns(); xml_set_start_namespace_decl_handler($parser, 'startNamespaceHandler'); $xml = '<root xmlns:valid="http://tutorialspoint.com/ns1" xmlns:invalid="http://unknown.com/ns"> <valid:child>Hello</valid:child> <invalid:child>World</invalid:child> </root>'; xml_parse($parser, $xml); xml_parser_free($parser); ?>
Output
This will create the below output −
Valid namespace: Prefix = valid, URI = http://tutorialspoint.com/ns1 Invalid namespace: URI = http://unknown.com/ns