How to create optional arguments in PHP ? Last Updated : 27 Apr, 2020 Comments Improve Suggest changes Like Article Like Report Arguments that do not stop the function from working even though there is nothing passed to it are known as optional arguments. Arguments whose presence is completely optional, their value will be taken by the program if provided. But if no value is given for a certain argument, the program will not halt. These arguments are given rise by usually providing the function with a default value for the parameter. Now, if the function is given with the value of the argument during the call, the given arguments overwrite the default one and the program continues normally. But if the call doesn't provide the function with any value, the program continues normally with the default ones. The idea here is to create such optional arguments within are program. This will as mentioned will be carried out using the default value. Flow chart: Although the concept is pretty straight-forward but here is a flow chart to understand better. Below examples illustrate the optional arguments in PHP. Example 1: php <?php function travel($place = "Sweden") { return "Traveling to $place.\n"; } echo travel(); echo travel("Australia"); echo travel("Tokyo"); ?> Output: Travelling to Sweden. Traveling to Australia. Traveling to Tokyo. Example 2: In this example it confirms that the timer is set. php <?php function timer($hour=00, $min=00, $sec=00) { $time="$hour : $min : $sec."; return "Timer set for $time \n"; } echo timer(); echo timer(10, 30, ); echo timer(00, 20, 40); ?> Output: Timer set for 0 : 0 : 0. Timer set for 10 : 30 : 0. Timer set for 0 : 20 : 40. Comment More infoAdvertise with us Next Article How to create optional arguments in PHP ? V vanshikagoyal43 Follow Improve Article Tags : Web Technologies PHP PHP Programs PHP-Misc Similar Reads How to Define an Empty Object in PHP ? An object is an instance of a class, and we can create objects using various approaches. Defining an empty object typically involves creating an instance without specifying a class definition explicitly. In this article, we will explore different approaches to defining an empty object in PHP, these 1 min read How to Create an Array of Given Size in PHP? This article will show you how to create an array of given size in PHP. PHP arrays are versatile data structures that can hold multiple values. Sometimes, it's necessary to create an array of a specific size, either filled with default values or left empty. Table of Content Using array_fill() Functi 3 min read How to Pass an Array into a Function in PHP ? This article will show you how to pass an array to function in PHP. When working with arrays, it is essential to understand how to pass them into functions for effective code organization and re-usability. This article explores various approaches to pass arrays into PHP functions, covering different 2 min read How to write comments in PHP ? Comments are non-executable lines of text in the code that are ignored by the PHP interpreter. Comments are an essential part of any programming language. It help developers to understand the code, provide explanations, and make the codebase more maintainable. Types of Comments in PHPPHP supports tw 1 min read How to Add Elements to the End of an Array in PHP? In PHP, arrays are versatile data structures that can hold multiple values. Often, you may need to add elements to the end of an array, whether it's for building a list, appending data, or other purposes. PHP provides several ways to achieve this. In this article, we will explore different approache 3 min read How to mimic multiple constructors in PHP ? Constructors are special member functions for initial settings of newly created object instances from a class. In PHP, a constructor is a method named __construct(), which is called by the keyword new after creating the object. Constructors can also accept arguments, in which case, when the new stat 4 min read Best way to initialize empty array in PHP Arrays in PHP: Use array() Function to create an array in PHP. There are three types of array supported in PHP: Indexed arrays: Arrays having a numeric index. Associative arrays: Arrays having named keys. Multidimensional arrays: It contains one or more array in particular array. Note: Why it is alw 3 min read How to get parameters from a URL string in PHP? The parameters from a URL string can be retrieved in PHP using parse_url() and parse_str() functions.Note: The page URL and the parameters are separated by the ? character.parse_url() FunctionThe parse_url() function is used to return the components of a URL by parsing it. It parses a URL and return 2 min read How to Check a Value Exist at Certain Index in an Array in PHP? Given an array and index, the task is to check whether a value exists or not at a certain index in an array in PHP. Below are the approaches to check if a value exists at a certain index or not in an array in PHP:Table of ContentUsing isset() FunctionUsing array_key_exists() FunctionUsing key_exists 3 min read PHP | filter_input_array() Function The filter_input_array() function is an inbuilt function in PHP which is used to get external variables (e.g. from form input) and filters them if it is specified. This function is similar to filter_input() function but the only difference is filter_input() filters a single value but in filter_input 3 min read Like