Skip to main content

How to Make a Function in PHP



Today I am going to share how to make or declare a function in PHP. Declaring a function is not so critical. We create function to use it repeatedly. There are many advantages of using function. We will learn that by creating lots of function one by one. Now let's start to know what is function, how to declare it and what is the necessary of that.

Function:

A function is block of statements that we can use in our program as much as we need and as long as we need. There are many built in functions in PHP. But we will learn how to create or declare user defined functions.

Function Signature:  In function signature there are four things that we should follow. These are: the 'function' keyword, function name, function parameters and the infamous curly brace. Now we will learn what they are and how they looks like.
                      
Very simple example of declaring a function.
 1. Keyword: In PHP, a function is declared with "function" keyword. We must place it at the beginning of a function body to declare the function. This is the first mandatory and valid way of declaring a function.

 2. Function Name: After placing the 'function' keyword just give one space (or more if you like but that is not a standard way) and put the function name. We can start with underscore or any alphabet and digit like A-Z or a-z or _A-Z or _a-z or _0-9. Don't use any digit at beginning of a function name.  But It is better to put the function name relevant to the function's responsibility. 

3. Parameter:  Parameters are sometimes called argument. So don't worry about this mixture. There is a technical difference between argument and parameter but you don't need to worry about that for now. I will discuss about that later. This can contain multiple parameter. In this signature we have to place those variable that are needed in our code to get the result. This parameter is like inputting something in a code. For example: ($arr, $str, $p etc.).

4. Curly Brace: A function starts with opening curly brace( { ) and ends with closing curly brace( } ). Under this brace we have to create code to get the result. In this field we can make any instruction to get our result. In PHP, We can print our result by "echo" statement or  using "return statement" inside the statement. If we don't put one of them we don't get any result when we call the function.We should remember that, when a php function get "return statement" it will never run any code after that. It will just give the result  before the return statement. I will discuss later while I will start coding. 

After completing these we have to call the function . To call a function we have to place the function name and after that place the value of the parameter.

Now I show you how to call a function. I call the function that I created at the top.

                                         myfunction();  we must put value[$input] of the parameter that is relevant to the code.

This is way of calling a function. It can be called in variable, any where we can call that in our code. We can also echo it directly. We will learn it by coding. 










 

Comments

Popular posts from this blog

Meta is Updating Its Monetization Rules – Effective on August 31, 2025

Meta has announced updates that will take effect on August 31, 2025 , including the shutdown of Facebook’s current video monetization tools, such as In-Stream Ads , Ads on Reels , and the Performance Bonus . What’s Changing? All New Video Uploads Will Become Reels Meta is transitioning all video formats— short, long, and live —into the Reels format. The 90-second limit is being removed, and the Video tab will be rebranded as Reels . Monetization Tools Ending by August 31, 2025 Creators will no longer earn from In-Stream Ads , Ads on Reels , or the Performance Bonus associated with those formats. Introducing the Content Monetization Program Meta is merging all monetization tools into a new system called the Content Monetization Program , which allows creators to earn from a wider range of content types, not just videos. Next Steps for Creators Keep an eye on your Monetization tab or Meta Business Suite dashboard for an ...

১০ টি কার্যকরী ডিজিটাল মার্কেটিং চ্যানেল ও এদের ব্যবহারবিধি

এখানে ১০ টি অনলাইন মার্কেটিং চ্যানেল নিয়ে পর্যায়ক্রমে আলোচনা করবো যে গুলো ব্যবহার করে আপনি আপনার ব্যবসাকে উন্নত করতে পারবেন। ডিজিটাল মার্কেটিং চ্যানেল কী ? ডিজিটাল মার্কেটিং চ্যানেল বা ডিজিটাল এডভার্টাইজিং চ্যানেল হচ্ছে অনলাইনের বিভিন্ন মাধ্যম ও পদ্ধতি ব্যবহার করে ব্র্যান্ড , পণ্য বা সেবা গ্রাহকের কাছে পৌঁছে দেয়া। ডিজিটাল মার্কেটিং চ্যানেলের মধ্যে ওয়েবসাইট , সোশ্যাল মিডিয়া , ইমেইল , কনটেন্ট মার্কেটিং , SEO, PPC উল্লেখযোগ্য। এই মাধ্যম গুলো আপনার গ্রাহকদের সাথে সম্পৃক্ত করে ব্যবসার প্রসার ঘটায়। চলুন দেখে নেয়া যাক গুরুত্বপূর্ণ মার্কেটিং চ্যানেলগুলোর কার্যক্রম।   1. সোশ্যাল মিডিয়া মার্কেটিং (SMM): সোশ্যাল মিডিয়া মার্কেটিং হচ্ছে সামাজিক যোগাযোগ মাধ্যম যেমন ফেইসবুক , লিংকডইন , টুইটার , ইনস্টাগ্রাম , পিন্টারেস্ট ইত্যাদি ব্যবহার করে ব্র্যান্ড , পণ্য ও সেবার প্রচারণা করা। এটি এখন সবচাইতে গুরুত্বপূর্ণ একটি মার্কেটিং টুল। কারণ  মিলিয়ন মিলিয়ন সম্ভাব্য ক্রেতার কাছে পৌঁছানো যাবে...

Concept about a Web Browser and Search Engine.

Web browser and search engine always conflict me. A few minute before i just make myself clear about the web browser and search engine.Now i am going to share with you. What is a web browser: A web browser is a software application that allows the user to access ,retrieve and view information on Internet.We can also say that Web browser is a tool that can access a Search Engine.Browser facilities is search for a specific website. The most popular Web Browsers are Google Chrome, Opera, Safari, Firefox, Internet Explorer. when a user inputing a url suppose http,htttps,ftp or files ,the browser show us information on the screen.A web Browser can reached a user to the Search Engine.Make it clear with an example: when we  use Firefox we write www.google.com and we get a new page that is google web page. We know Firefox is a browser and Google.com is a search engine. What is a Search Engine : Search  Engine is a on-line  search machine that provide the u...