|
|
|
|
![]() ![]() |
Jul 31 2008, 04:17 PM
Post
#1
|
|
|
Newbie [ Level 1 ] Group: Members Posts: 8 Joined: 22-July 08 From: Pitcairn, PA Member No.: 31,591 |
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 CODE <?php lots and lots of code more code more 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 "Astahost is the best host ever deal with it!", and named astahost(), it would look like this. CODE <?php function astahost() { echo "Astahost is the best host ever deal with it!"; } astahost(); ?> [b]Output:[/b] Astahost 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> <?php function host($abc) { echo $abc . " is a great web host!<br />"; } host("AstaHost"); host("Trap17"); - host("Computing Host"); ?> </body> </html> Output: AstaHost is a great web host! Trap17 is a great web host! Computing Host is a great web host![/quote] [quote] 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 CODE <?php //adding function add($x,$y) { $total = $x + $y; echo $total ."<br />"; } //subtracting function subtract($x,$y) { $total = $x - $y; echo $total ."<br />"; } //multiplying function multi($x,$y) { $total = $x * $y; echo $total ."<br />"; } //dividing function divide($x,$y) { $total = $x / $y; echo $total ."<br />"; } ?> 4.To use that in a page, simply do CODE <html> {had to separate them by ending the <?php over and over, the answer was not going where it should, lol}<body> <?php include("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=<?php divide(4,4); ?><br /> </p> </body> </html> 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. This post has been edited by Archimedes: Aug 4 2008, 12:24 AM |
|
|
|
![]() ![]() |
Similar Topics
| Topics | Topics | |
|---|---|---|
|
|
|
|
Lo-Fi Version | Time is now: 22nd November 2008 - 02:58 PM |