{"id":7263,"date":"2019-10-20T14:25:00","date_gmt":"2019-10-20T14:25:00","guid":{"rendered":"https:\/\/www.devopsschool.com\/blog\/?p=7263"},"modified":"2021-11-16T05:45:25","modified_gmt":"2021-11-16T05:45:25","slug":"complete-reference-guide-of-php-functions","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/complete-reference-guide-of-php-functions\/","title":{"rendered":"Complete reference guide of PHP Functions!"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">PHP Built-in Functions<\/h2>\n\n\n\n<p>A function is a self-contained block of code that performs a specific task.<\/p>\n\n\n\n<p>PHP has a huge collection of internal or built-in functions that you can call directly within your PHP scripts to perform a specific task, like&nbsp;<code>gettype()<\/code>,&nbsp;<code>print_r()<\/code>,&nbsp;<code>var_dump<\/code>, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">PHP User-Defined Functions<\/h2>\n\n\n\n<p>In addition to the built-in functions, PHP also allows you to define your own functions. It is a way to create reusable code packages that perform specific tasks and can be kept and maintained separately form main program. Here are some advantages of using functions:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><strong>Functions reduces the repetition of code within a program<\/strong>&nbsp;\u2014 Function allows you to extract commonly used block of code into a single component. Now you can perform the same task by calling this function wherever you want within your script without having to copy and paste the same block of code again and again.<\/li><li><strong>Functions makes the code much easier to maintain<\/strong>&nbsp;\u2014 Since a function created once can be used many times, so any changes made inside a function automatically implemented at all the places without touching the several files.<\/li><li><strong>Functions makes it easier to eliminate the errors<\/strong>&nbsp;\u2014 When the program is subdivided into functions, if any error occur you know exactly what function causing the error and where to find it. Therefore, fixing errors becomes much easier.<\/li><li><strong>Functions can be reused in other application<\/strong>&nbsp;\u2014 Because a function is separated from the rest of the script, it&#8217;s easy to reuse the same function in other applications just by including the php file containing those functions.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Creating and Invoking Functions<\/h2>\n\n\n\n<p>The basic syntax of creating a custom function can be give with:<br><strong>function&nbsp;functionName(){<br>&nbsp;&nbsp;&nbsp;&nbsp;\/\/ Code to be executed<br>}<\/strong><\/p>\n\n\n\n<p>The declaration of a user-defined function start with the word&nbsp;<code>function<\/code>, followed by the name of the function you want to create followed by parentheses i.e.&nbsp;<code>()<\/code>&nbsp;and finally place your function&#8217;s code between curly brackets&nbsp;<code>{}<\/code>.<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\">Functions with Parameters<\/h2>\n\n\n\n<p>You can specify parameters when you define your function to accept input values at run time. The parameters work like placeholder variables within a function; they&#8217;re replaced at run time by the values (known as argument) provided to the function at the time of invocation.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">myFunc<\/span><span class=\"hljs-params\">($oneParameter, $anotherParameter)<\/span><\/span>{\n    <span class=\"hljs-comment\">\/\/ Code to be executed<\/span>\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You can define as many parameters as you like. However for each parameter you specify, a corresponding argument needs to be passed to the function when it is called.<\/p>\n\n\n\n<p>The&nbsp;<code>getSum()<\/code>&nbsp;function in following example takes two integer values as arguments, simply add them together and then display the result in the browser.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Functions with Optional Parameters and Default Values<\/h2>\n\n\n\n<p>You can also create functions with optional parameters \u2014 just insert the parameter name, followed by an equals (<code>=<\/code>) sign, followed by a default value, like this.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">Hello, world!<span class=\"hljs-string\">\"; } \/\/ Calling function customFont(\"<\/span>Arial<span class=\"hljs-string\">\", 2); customFont(\"<\/span>Times<span class=\"hljs-string\">\", 3); customFont(\"<\/span>Courier<span class=\"hljs-string\">\"); ?&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n<div class=\"epyt-gallery\" data-currpage=\"1\" id=\"epyt_gallery_94402\"><figure class=\"wp-block-embed wp-block-embed-youtube is-type-video is-provider-youtube epyt-figure\"><div class=\"wp-block-embed__wrapper\"><iframe loading=\"lazy\"  id=\"_ytid_68245\"  width=\"760\" height=\"427\"  data-origwidth=\"760\" data-origheight=\"427\" src=\"https:\/\/www.youtube.com\/embed\/?enablejsapi=1&#038;autoplay=0&#038;cc_load_policy=0&#038;cc_lang_pref=&#038;iv_load_policy=1&#038;loop=0&#038;rel=1&#038;fs=1&#038;playsinline=0&#038;autohide=2&#038;theme=dark&#038;color=red&#038;controls=1&#038;disablekb=0&#038;\" class=\"__youtube_prefs__  no-lazyload\" title=\"YouTube player\"  data-epytgalleryid=\"epyt_gallery_94402\"  allow=\"fullscreen; accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen data-no-lazy=\"1\" data-skipgform_ajax_framebjll=\"\"><\/iframe><\/div><\/figure><div class=\"epyt-gallery-list\"><div>Sorry, there was a YouTube error.<\/div><\/div><\/div>","protected":false},"excerpt":{"rendered":"<p>PHP Built-in Functions A function is a self-contained block of code that performs a specific task. PHP has a huge collection of internal or built-in functions that you can call directly within your PHP scripts to perform a specific task, like&nbsp;gettype(),&nbsp;print_r(),&nbsp;var_dump, etc. PHP User-Defined Functions In addition to the built-in functions, PHP also allows you&#8230;<\/p>\n","protected":false},"author":14,"featured_media":7343,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","_joinchat":[],"footnotes":""},"categories":[5150],"tags":[5173,5428,5429],"class_list":["post-7263","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-php","tag-php-data-object","tag-php-function","tag-php-syntex"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7263","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=7263"}],"version-history":[{"count":8,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7263\/revisions"}],"predecessor-version":[{"id":25375,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/7263\/revisions\/25375"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media\/7343"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=7263"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=7263"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=7263"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}