1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)

PHP7 FUNCTIONS USES APPLICATION PROGRAMMING LESSON 5

PHP7 functions are very useful. There are predefined functions that is already programmed in PHP7 already that you can use. You can also create your own PHP7 functions to use in your applications. This tutorial is going to cover “PHP Functions” and how you can use them on your website and applications.

What Is a PHP Function? *

A PHP function is a piece of code that creates or displays a programming function. It is used to pull data and information from its local resources. This is how it got its name “PHP Functions”.

How Many PHP7 Functions Are There? *

There are thousands of PHP functions. You can see a full list of PHP7 functions from the PHP website here. We will be showing you how to use these functions in this tutorial.

How Do We Write a PHP7 Function? *

First you declare that it is a function followed by the name of the function. Then you will use open and close parentheses () followed by curly brackets {}. Then you will go inside the curly brackets and create the actual function whatever it may be. Here is an example of the syntax used.

<?php function foo(){echo 'this is a function';} ?>

How To Call A PHP7 Function? *

Call a PHP function just by using the name assigned to it followed by the parentheses then a semicolon. In the example above we would call the function like this.

<?php foo(); ?>

If you are using a predefined function already built in PHP7 call in the function according to its name. It may have additional arguments within the function.

Can PHP7 Functions Accept Parameters? *

Yes PHP7 functions can accept parameters. Parameters are additional arguments that are presented in the function. Parameters are usually PHP variables used in the function. For example if I were to name people in my family that all have the same last name I could create a function like this.

<?php function lastName($var){

echo $var.' McCullough';

}

Then I can display the different first name with the same last name like this.

<?php

lastName('Maximus');

lastName('Harley');

lastName('David');

It will print out on the screen like this.

Maximus McCullough

Harley McCullough

David McCullough

Show Me how To Use PHP7 Functions In A Website *

Using our examples from the previous lesson Assemble Basic Website in PHP. We will add a date php function so that it displays the current date on the page. We will then create our own simple PHP7 function to display some text.

Getting Started With PHP7 Functions *

If you have not already download the files from the last lesson. We will continue to develop this website application so that you can build off of what you learned. Make sure your XAMPP server is running on your local machine. Create a new file called functions.php. Insert this code into it.

function foo(){
echo 'This is coming from a php function.';
}

Open up your header.php file and insert this code at the bottom of the file.

<?php foo(); ?>

Refresh your website and you can see the screen display the results.

Parameters In PHP7 Functions *

Sometimes we will have to use a parameter in a PHP function. Open up the foo.php file and insert this code somewhere on the page.

<?php function lastName($var){ echo $var.' McCullough'; }; lastName('Maximus'); lastName('Harley'); lastName('David'); ?>

You see the first names called into the webpage in place of $var.

Multi Parameters in PHP7 Functions *

Remove the above function from the foo.php file and insert this code and run it.

<?php function lastName($fname, $year){
echo "$fname McCullough $year 
";
};

lastName('Maximus','1971');
lastName('Harley','2001');
lastName('David','2003'); ?>

The function is taking in 2 parameters. The variables of first name and date of birth display in the function.

Use A PHP Function In Our Project *

Open up your functions.php file and insert the following code.

<?php function headtag(){
echo '<html>
<head>
<title>My First Programming Website</title>
<link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">
<link rel="stylesheet" href="style.css" />
</head>';
} ?>

Now go to your header.php and remove the above code and replace it with this code.

<?php headtag(); ?>

Now this calls in your head tags with a PHP function.

Date() A PHP7 Function *

We can use the date function in PHP. Go to your header.php file and insert the date just below the body tag. Here is the code to use.

<?php echo date('M-d-Y');?>

You will notice that this will display todays date.

Conclusion on PHP7 Functions *

Functions come in real handy in your applications. You will develop your skills further as we go along. We will be creating more functions in the future. I hope you are enjoying these tutorials. Please like, share and subscribe for future tutorials and updates.

PHP7 FUNCTIONS USES APPLICATION PROGRAMMING LESSON 5 was last modified: November 15th, 2018 by Maximus Mccullough

php7-functions-and-parameters

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.