PHP | getservbyname() Function Last Updated : 30 Aug, 2019 Comments Improve Suggest changes Like Article Like Report The getservbyname() function is an inbuilt function in PHP which returns the port number for given protocol and Internet service. Syntax: int getservbyname( string $service, string $protocol ) Parameters: This function accepts two parameters as mentioned above and described below: $protocol: It is required parameter. It specifies protocol name, like tcp, udp etc in string format. $service: It is required parameter. It specifies the Internet service name, like http int string format. Return Value: This function returns the port number on success or False if service or protocol not found. Note: This function is available for PHP 4.0.0 and newer version. Below programs illustrate the getservbyname() function in PHP: Program 1: php <?php // Use getservbyname() function to get // port number associated with an // Internet service and protocol $portnum = getservbyname("http", "tcp"); // Display the result echo $portnum; ?> Output: 80 Program 2: This program checks multiple services. php <?php // Create an array of services $services = array("ftp", "ssh", "telnet", "http", "https"); // Loop run for each services foreach( $services as $index) { // Use getservbyname() function to get // the port number associated with an // Internet service and protocol echo getservbyname($index, "tcp") . ": " . $index . "<br>"; } ?> Output: 21: ftp 22: ssh 23: telnet 80: http 443: https Reference: https://www.php.net/manual/en/function.getservbyname.php Create Quiz Comment G gekcho Follow 0 Improve G gekcho Follow 0 Improve Article Tags : Web Technologies PHP PHP-function Explore BasicsPHP Syntax4 min readPHP Variables5 min readPHP | Functions6 min readPHP Loops4 min readArrayPHP Arrays5 min readPHP Associative Arrays4 min readMultidimensional arrays in PHP5 min readSorting Arrays in PHP4 min readOOPs & InterfacesPHP Classes2 min readPHP | Constructors and Destructors5 min readPHP Access Modifiers4 min readMultiple Inheritance in PHP4 min readMySQL DatabasePHP | MySQL Database Introduction4 min readPHP Database connection2 min readPHP | MySQL ( Creating Database )3 min readPHP | MySQL ( Creating Table )3 min readPHP AdvancePHP Superglobals6 min readPHP | Regular Expressions12 min readPHP Form Handling4 min readPHP File Handling4 min readPHP | Uploading File3 min readPHP Cookies9 min readPHP | Sessions7 min read Like