HOME       >>       Programming

Php: Lesson #3;functions PHP.


Well, the last lesson was on If...Else statements, this one will be on Functions.


Things you need to know

1.Basic PHP(Lesson 1)

2.I only speak English, sorry. So, everything will be in English terms.

3.I only put the stuff in quotes to be more organized. xD


Lesson #3;



Questions & Answers

Q1.What is a function?

A1.A function is code that we can access at any time, such as the e-mail function; mailto(email@something.com).

Q2.Why use functions?

A2.Would you rather use

<?phplots and lots of codemore codemore and more code?>
or function()?



Making functions

1.To create a function, simple type function function_name() {

what it does;


2.Say I wanted to make a function that says "Xisto is the best host ever deal with it!", and named Xisto(), it would look like this.

<?phpfunction Xisto() { echo "Xisto is the best host ever deal with it!"; }Xisto();?>[b]Output:[/b]Xisto is the best host ever deal with it!3.That was a fairly simple function, let's move onto something a bit more, not so simple.[/quote][quote][b]More complex functions[/b]1.Can have If...Else statements in it.2.Making a function say different things.[code]<html><body><?phpfunction host($abc) { echo $abc . " is a great web host!<br />"; }host("Xisto");host("Xisto");-host("Xisto - Web Hosting");?></body></html>

Xisto is a great web host!

Xisto is a great web host!

Xisto - Web Hosting is a great web host!

How to use your own collection of functions

1.Simply create a file called functions.php, or anything else, just remember it.

2.Make your own functions on that file, xD.

3.Here is my math_functions.php

<?php//addingfunction add($x,$y) { $total = $x + $y; echo $total ."<br />"; }//subtractingfunction subtract($x,$y) { $total = $x - $y; echo $total ."<br />"; }//multiplyingfunction multi($x,$y) { $total = $x * $y; echo $total ."<br />"; }//dividingfunction divide($x,$y) { $total = $x / $y; echo $total ."<br />"; }?>
4.To use that in a page, simply do

<html><body><?phpinclude("math_functions.php"); ?> <p>4+4=<?php add(4,4); ?><br />4-4=<?php subtract(4,4); ?><br />4*4=<?php multi(4,4); ?><br />4/4=<?phpdivide(4,4); ?><br /></p></body></html>
{had to separate them by ending the <?php over and over, the answer was not going where it should, lol}


4 + 4 = 8

4 - 4 = 0

4 * 4 = 16

4 / 4 = 1


Well, that's it. Next lesson will be on PHP forms, maybe.


Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.