PHP Exercises : Get the names of the functions of a module
29. List Module Functions
Write a PHP script to get the names of the functions of a module.
Note : Find XML, JSON functions
Sample Solution:
PHP Code:
<?php
// Get the names of all functions provided by the JSON extension
$json_extension_funcs = get_extension_funcs("JSON");
// Print the list of JSON extension functions
print_r($json_extension_funcs);
echo "\n";
// Get the names of all functions provided by the XML extension
$xml_extension_funcs = get_extension_funcs("XML");
// Print the list of XML extension functions
print_r($xml_extension_funcs);
echo "\n";
?>
Explanation:
- Get JSON Extension Functions:
- The function get_extension_funcs("JSON") retrieves an array of names for all functions provided by the JSON extension in PHP.
- The result is stored in the variable $json_extension_funcs.
- Print JSON Functions:
- The print_r($json_extension_funcs) function is called to output the array of JSON function names in a human-readable format.
- An additional echo "\n" adds a newline for better readability in the output.
- Get XML Extension Functions:
- Similarly, get_extension_funcs("XML") retrieves an array of names for all functions provided by the XML extension.
- This result is stored in the variable $xml_extension_funcs.
- Print XML Functions:
- The print_r($xml_extension_funcs) function outputs the array of XML function names.
- Again, echo "\n" adds a newline for better formatting in the output.
Output:
Array
(
[0] => json_encode
[1] => json_decode
[2] => json_last_error
[3] => json_last_error_msg
)
Array
(
[0] => xml_parser_create
[1] => xml_parser_create_ns
[2] => xml_set_object
[3] => xml_set_element_handler
[4] => xml_set_character_data_handler
[5] => xml_set_processing_instruction_handler
[6] => xml_set_default_handler
[7] => xml_set_unparsed_entity_decl_handler
[8] => xml_set_notation_decl_handler
[9] => xml_set_external_entity_ref_handler
[10] => xml_set_start_namespace_decl_handler
[11] => xml_set_end_namespace_decl_handler
[12] => xml_parse
[13] => xml_parse_into_struct
[14] => xml_get_error_code
[15] => xml_error_string
[16] => xml_get_current_line_number
[17] => xml_get_current_column_number
[18] => xml_get_current_byte_index
[19] => xml_parser_free
[20] => xml_parser_set_option
[21] => xml_parser_get_option
[22] => utf8_encode
[23] => utf8_decode
)
Flowchart:

For more Practice: Solve these Related Problems:
- Write a PHP script to list all functions available in a specific PHP module and output them alphabetically.
- Write a PHP script to count and display the number of functions provided by a given PHP extension.
- Write a PHP script to compare the function lists of two modules and display the overlapping functions.
- Write a PHP script to search and output module functions that start with a specified prefix.
Go to:
PREV : Get Temporary File Directory.
NEXT : Last Modification Time of Current Page.
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
