Functions in PHP - ProgrammerTech
Language:

Search

Functions in PHP

  • Share this:
Functions in PHP

Functions PHP

The function is a function within which we write any commands we want to be executed, and when this function php function is called, the commands within it will be executed. There are three types of functions in php which we will go over in detail.

Syntax function php

<?php
function name()
{
    //commands;
}
?>

Where the name is the name of the function and between the brackets {} we write the commands that we want to execute while calling this function.

 

Rules for creating a function in PHP

  • No spaces are allowed in the function name.
  • You are not allowed to use special shapes except for _.
  • Characters other than English characters are not allowed.
  • The function name is not allowed to start with a number.

 

The void function in PHP

This type of function does not return any value when called, it just executes the commands within it. Define the name of the function and the function is a function, so it is better to make the name related to the commands you want to write in that function. For example, we want to create a function about health, we call it health, so it is preferable to specify the name of the function. By function so that it is clear to the programmer and clear to others why you named this function.

<?php
function health(){
    echo "<h1>My health is excellent</h1>";
}
?>

Here, when running the code in the server, no command will be executed because we just created the function and did not call it. to call it like this:

<?php
function health(){
    echo "<h1>My health is excellent</h1>";
}
health();
?>

We only wrote the name of this function and called it only by its name, and you can also determine for yourself when this function can be called. At the moment we let it be called only when opening the php.

Functions in php Picture 1

 

And we can call it when someone presses a button of type submit. The important thing is that we know how to determine for ourselves when we should call the function. The function can also be called more than once. For example, we want to execute it twice. We call it twice as follows.

<?php
function health(){
    echo "<h1>My health is excellent</h1>";
}
health();
health();
?>

Here the existing commands will be executed twice when running the code in the server as follows.

Functions in php Picture 2

 

In short, the function is a function that you can define by yourself as the name of this function, and within it we write the commands that we want to execute when we call that function by its name.

Example

<?php
$kaissar = 'programmer tech';
function Team($kaissar)
{
     echo "<h3> $kaissar </h3>";
}
Team(10);
Team(12.333);
Team('Date');
Team(true);
echo $kaissar;
?>

We created a variable that holds data on a text in the name of our site but when we do. Create a function or function with the name Team and the function contains a variable inside it. But in this case we don't say it is a variable, it's just a parameter. Anything mentioned inside the function name is an argument. And as you can also notice that the parameter has the same name as the variable, but when giving data or values ​​to the parameter, it does not affect the variable. That is, the values ​​of the main variable have not changed, and the parameter can carry all kinds of data such as text and decimal numbers. And integers and also if it is logical or irrational and the values ​​are given to the operand while locking the function.

And the output will be on the server.

Functions in php Picture 3

 

All values ​​can be taken by the operand and in our case we the operand can take a decimal value. And also text and also integer and also the logical operator. And in the end, the kaissar variable, or the data that the variable holds, is printed.

 

The function is of type parameters in PHP

It is like the first type: void. We can only add conditions within this function. Conditions are variables that we will create when we call the function. We need to specify data for the variables that we created within the brackets in the function.

<?php
function print_data($name,$year){
}
print_data();
?>

When calling the function in this way, we will have a red line appear on the name of the function when calling, and this error is when we click on it to see the details. It will say that there is a parameter called name and a parameter called year for which we did not specify data to be called. And to specify data for them, we pass the data within the round brackets when we call it as follows.

<?php
function print_data($name,$year){
    echo "<h1>Name: " . $name . " , Year: " . $year . "</h1>";
}
print_data("Ahmed kaissar", 2001);
?>

We created a print function to coordinate the parameter passing and put it inside the h1 tag so that the size is large and clear. And at the bottom we called the function and passed the data to it, which is the name of Ahmed Kaissar and the year 2001.

And when you run this code in the server, the result will be the same.

Functions in php Picture 4

 

The idea of ​​this type of function is to pass data when we call the function. And every time we call the function we can pass different data as below we will pass different data.

<?php
function print_data($name,$year){
    echo "<h1>Name: " . $name . " , Year: " . $year . "</h1>";
}
print_data("Ahmed kaissar", 2001);
print_data("Muhammad Alush", 1994);
print_data("Saja Alsadig", 1995);
?>

We created a print function to coordinate the parameter passing and put it inside the h1 tag to make it big and clear. Below we call the function three times and pass different data each time we call it.

And when we run the code in the server, we will get the following results.

Functions in php Picture 5

 

This means that you can define for yourself which commands you want to be executed each time you call the function. And any data that is passed and that you print any element of the html and design it in css as you like later.

 

return function in PHP

The meaning of the word return in Arabic is to return or retrieve, it means that this type displays or returns data. Which is set when the function is called and later can be set by yourself. What do you want to do with the return value of this type of function.

In the same ways as before, we write the word function at the beginning and then we do. Determining the name of the function Since the function means a function, we specify the name of this function.

Syntax of the return function in PHP

<?php
function getText(){
    return
}
?>

Since getText is the name of the function and it can be called as you like, you can yourself specify the type of data you want to return, for example string or int and others...etc. The important thing is that you understand why we are using the return command and creating a function of type return.

<?php
function getText(){
    $text = "<h1>Ahmed kaissar</h1>";
    return $text;
}
getText();
?>

The data returned to us of type return can be stored in a new variable, let's say the following

<?php
function setText(){
    $text = "<h1>Ahmed kaissar</h1>";
    return $text;
}
$getText = setText();
echo $getText;
?>

We created a new variable, getText, and told the program that this variable had stored the value of the setText variable, and then we printed the new variable getText. Then we ran the code in the browser and the result was the same.

Functions in php Picture 6

 

We also noticed that the value of the first variable of the new variable was stored and the value was printed on the screen.. You can store the data in the return function within another variable. And also we can use it within a conditional sentence if statement, as in the following example

<?php
function setText(){
    $text = "<h1>Ahmed kaissar</h1>";
    return $text;
}
$getText = setText();
echo $getText;
if(setText() === "<h1>Ahmed kaissar</h1>"){
    echo "Commands executed";
}
?>

We set a condition that if the setText variable has the word Ahmed kaissar and it is inside the h1 tag, then print the word "command executed". When we run this code on the server, we will get the following result.

7-6.webp

 

We note that the condition is true and printed for us, the commands have been executed. If the condition is not met here, nothing will be printed.. This type can also have a parameter and pass the values ​​when called. Let's create another function of type return, this time with a parameter that means a variable within the round brackets in the function.

<?php
function setText(){
    $text = "<h1>Ahmed kaissar</h1>";
    return $text;
}
function setResult($number1 , $number2){
    return $number1 + $number2;
}
$getText = setText();
$getResult = setResult(23, 50);
echo $getResult;
echo $getText;
}
?>

We created a function called getResult and it had two parameters, they are number1 and number2, and the task of this function is to return the result of summing number1 with number2. Then we pass the value to the parameter when called, which is 23 and 50, then we print getResult and get the result 73 when it is run in the server.

Functions in php Picture 8

 

We note that he has added the product of the parametrine for us and we can convert the operation to multiply, subtract, divide .... etc.

Example

<?php
function Team($kaissar , $date)
{
    return $kaissar + $date;
}
echo Team(10 , 20);
?>

In the previous example, we created a function called Team, but this function contains two parameters. We also combined the parameter named kaissar with the other parameter named date and using . return, that is, we want to return that operation, but it did not take any values ​​from the parameters. We print the contents of the team function and assign values ​​to each parameter. The first parameter, kaissar, takes integer data, which is the number 10. The second parameter, date, also takes integer data. It is 20 so that the sum of the two coefficients is 30.

This is the output during the execution of the code.

Functions in php Picture 9

 

Example

<?php
function Team($data)
{
    return  array($data * $data, log($data) );
}
print_r(Team(23));
?>

Notice here what the indexed array Array contains? The parameter here contains from the data an integer which is 23 and then we multiply the parameter by itself. Inside an Array, we used log to return its decimal logarithm. In the previous example, it clearly occurs 23 * 23, and the result is 529, however. It is inside the matrix see the output at the top.

Functions in php Picture 10

 


Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com