PHP/Functions/Definition
Содержание
- 1 Accessing a Global Variable from Within a Function
- 2 A Function Requiring Two Arguments
- 3 A function that calculates sales tax
- 4 A Function to Build Query Strings
- 5 A Simple User Function
- 6 Build and then call a function
- 7 Call the function
- 8 Create a User Function
- 9 Create function from string
- 10 Creating and calling a function that accepts arguments
- 11 Declaring a Function
- 12 HELLO!
- 12.1 Declaring a Simple Function
- 12.2 Define constant in a function
- 12.3 Defining functions before or after calling them
- 12.4 Function printing text on a Web Page
- 12.5 global key word in a function
- 12.6 nesting functions
- 12.7 Using nested functions
- 12.8 Variables Defined Outside Functions Are Inaccessible from Within a Function by Default
Accessing a Global Variable from Within a Function
<source lang="html4strict">
<?php
$GLOBALS["user"] = "myusername"; $GLOBALS["pass"] = "mypassword"; function validatelogin ($username, $password){ if (strcmp ($username, $GLOBALS["user"]) == 0 && strcmp ($password, $GLOBALS["pass"]) == 0){ return true; } else { return false; } } if (validatelogin ("myusername","mypassword")){ echo "You are logged in correctly"; } else { echo "You have an incorrect username and/or password"; }
?>
</source>
A Function Requiring Two Arguments
<source lang="html4strict">
<html> <head><title></title></head> <body> <?php
function headingWrap( $txt, $size ) { print "<h$size>$txt</h$size>"; } headingWrap("A", 1); headingWrap("B",2); headingWrap("C",3);
?> </body> </html>
</source>
A function that calculates sales tax
<source lang="html4strict">
<? $price = 4.9; $tax = .04; function calculate_cost($tax, $price) {
$sales_tax = $tax; return $price + ($price * $sales_tax);
} $total_cost = calculate_cost ($tax, $price); $total_cost = round($total_cost, 2); print "Total cost: ".$total_cost; ?>
</source>
A Function to Build Query Strings
<source lang="html4strict">
<html> <head> <title>Using http_build_query() to Build Query Strings</title> </head> <body> <?php $q = array (
"name" => "Tom", "interest" => "Movie", "homepage" => "http://www.example.ru" );
$query = http_build_query( $q ); print $query; ?>
<a href="anotherpage.php?<?php print $query ?>">Go!</a>
</body> </html>
</source>
A Simple User Function
<source lang="html4strict">
function foo( ) {
return 1; } print foo( ); </source>
Build and then call a function
<source lang="html4strict">
<?php
function helloworld (){ echo "Hello World!
"; } helloworld(); function saysomething ($something){ echo $something . "
"; } Saysomething ("Hello World!"); //This would output "Hello World!" function addvalues ($firstvalue, $secondvalue){ return $firstvalue + $secondvalue; } $newvalue = addvalues (1,3); echo $newvalue;
?>
</source>
Call the function
<source lang="html4strict">
<?php function hi( ){
echo ("Hello from function-land!");
} hi( ); ?>
</source>
Create a User Function
<source lang="html4strict">
<html> <head> <title>Create a User Function</title> <?php function myFunction($company) {
print("Welcome to $company
");} ?> </head> <body>
Start
<?php $company = "A"; myFunction($company); ?>
</source>
Create function from string
<source lang="html4strict">
<? $lambda =create_function("$a,$b","return(strlen($a)-strlen($b));"); $array = array("really long string here,boy", "this", "middling length",
"larger");
usort($array,$lambda); print_r($array); ?>
</source>
Creating and calling a function that accepts arguments
<source lang="html4strict">
<? function saysomething ($something){
echo $something . "
";
} Saysomething ("Hello World!"); ?>
</source>
Declaring a Function
<source lang="html4strict">
<html> <head> <title>Declaring a Function</title> </head> <body> <?php function bighello(){
print "HELLO!
";} bighello(); ?>
</body>
</html>
</source>
Declaring a Simple Function
<source lang="html4strict">
<?php function aFunction(){ print "Music is a way of life!"; } aFunction(); ?> </source>
Define constant in a function
<source lang="html4strict">
<?php define("CONST1", 1); function MyTest() {
define("CONST2", 2);
} MyTest(); echo "CONST1 = " . CONST1 . " and CONST2 = " . CONST2 . "\n"; ?>
</source>
Defining functions before or after calling them
<source lang="html4strict">
<? function page_header() {
print "<html><head><title>Welcome to my site</title></head>"; print "<body bgcolor="#ffffff">";
} page_header(); print "Welcome, $user"; page_footer(); function page_footer() {
print "Thanks for visiting.";
print "</body></html>";
} ?>
</source>
Function printing text on a Web Page
<source lang="html4strict">
<HTML>
<HEAD> <TITLE> Printing text on a Web Page </TITLE> </HEAD> <BODY> <?php function textonweb ($content, $fontsize) { echo "$content"; } textonweb ("A
", 7); textonweb ("AA.
", 3); textonweb ("AAA.
", 3); textonweb ("AAAA!
", 3); ?> </BODY>
</HTML>
</source>
global key word in a function
<source lang="html4strict">
<?php $a = 7; function test() {
global $a; $a = 20;
} test(); echo "\$a = $a\n"; ?>
</source>
nesting functions
<source lang="html4strict">
<?php function myFunction($price,$tax) {
function convert_pound($dollars, $conversion=1.6) { return $dollars * $conversion; } $total = $price + ($price * $tax); echo "Total cost in dollars: $total. Cost in British pounds: " . convert_pound($total);
} myFunction(15.00,.075); echo convert_pound(15); ?>
</source>
Using nested functions
<source lang="html4strict">
<? function display_footer($site_name) {
function display_copyright($site_name) { print "© ". date("Y"). " $site_name."; } print "contact us
"; display_copyright($site_name);
} $site_name = "PHP"; display_footer($site_name); ?>
</source>
Variables Defined Outside Functions Are Inaccessible from Within a Function by Default
<source lang="html4strict">
<html> <head> <title>No Default Access to Globals in Functions</title> </head> <body>
<?php
$life = 42; function meaningOfLife() { print "The meaning of life is $life
"; } meaningOfLife();
?>
</body> </html>
</source>