Jump to content
xisto Community
Sign in to follow this  
Archimedes

Php: Lesson #3;functions PHP.

Recommended Posts

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;

Functions!

 

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>
Output:

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}

Output:

4 + 4 = 8

4 - 4 = 0

4 * 4 = 16

4 / 4 = 1

 

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

Edited by Archimedes (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.