TimesCoding

Learn, Practice, and Excel with our Coding Tutorials!

Add HTTP Protocol if not Exist with URL

June 7, 2022 1 Min read PHP

 The below function is used to add HTTP protocol to the URL if not available. You can add “http://” or “https://” by using the function.

<?php

function addHttpProtocol($url, $scheme){
	return parse_url($url, PHP_URL_SCHEME) === null ? $scheme . $url : $url;
}
	
	echo addHttpProtocol('timescoding.com', 'http://');
	//result:  
	//  http://timescoding.com
	
	echo addHttpProtocol('timescoding.com', 'https://');
	//result:  
	//  https://timescoding.com

?>
Related Post:
URL Exist or Not Exist Check PHP − We can easily check a given URL or Link is active or not. By using below PHP function we can easily get whether the given.