PHP/Functions/Definition

Материал из Web эксперт
Перейти к: навигация, поиск

Accessing a Global Variable from Within a Function

 
<?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";
  }
?>



A Function Requiring Two Arguments

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



A function that calculates sales tax

 
<?
$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;
?>



A Function to Build Query Strings

 
<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;
?>
<p>
<a href="anotherpage.php?<?php print $query ?>">Go!</a>
</p>
</body>
</html>



A Simple User Function

 
function foo( ) {
            return  1;
    }
    print foo( );



Build and then call a function

 
<?php
  function helloworld (){
    echo "Hello World!<br />";
  }
  helloworld();
  
  function saysomething ($something){
    echo $something . "<br />";
  }
  Saysomething ("Hello World!"); //This would output "Hello World!"
  function addvalues ($firstvalue, $secondvalue){
    return $firstvalue + $secondvalue;
  }
  $newvalue = addvalues (1,3); 
  echo $newvalue; 
?>



Call the function

 
<?php
function hi(  ){
    echo ("Hello from function-land!");
}
hi(  );
?>



Create a User Function

 
<html> 
<head> 
<title>Create a User Function</title> 
<?php 
function myFunction($company) { 
    print("<p>Welcome to $company</p>"); 
} 
?> 
</head> 
<body> 
<h3>Start</h3> 
<?php 
$company = "A"; 
myFunction($company); 
?>



Create function from string

<?
$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);
?>



Creating and calling a function that accepts arguments

 
<?
function saysomething ($something){ 
    echo $something . "<br />"; 
} 
Saysomething ("Hello World!"); 
?>



Declaring a Function

<html>
<head>
<title>Declaring a Function</title>
</head>
<body>
<?php
function bighello(){
     print "<h1>HELLO!</h1>";
}
bighello();
?>
 </body>
</html>



Declaring a Simple Function

  <?php
  function aFunction(){
      print "Music is a way of life!";
  }
  aFunction();
  ?>



Define constant in a function

 
<?php
define("CONST1", 1);
function MyTest() {
  define("CONST2", 2);
}
MyTest();
echo "CONST1 = " . CONST1 . " and CONST2 = " . CONST2 . "\n";
?>



Defining functions before or after calling them

 
<?
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 "<hr>Thanks for visiting.";
    print "</body></html>";
}
?>



Function printing text on a Web Page

<HTML>
  <HEAD>
  <TITLE> Printing text on a Web Page </TITLE>
  </HEAD>
  <BODY>
  <?php
  function textonweb ($content, $fontsize) {
     echo "<FONT SIZE=$fontsize>$content</FONT>";
  }
  textonweb ("A <BR>", 7);
  textonweb ("AA. <BR>", 3);
  textonweb ("AAA. <BR>", 3);
  textonweb ("AAAA! <BR>", 3);
  ?>
  </BODY>
</HTML>



global key word in a function

 
<?php
$a = 7;
function test() {
  global $a;
  $a = 20;
}
test();
echo "\$a = $a\n";
?>



nesting functions

 
<?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);
?>



Using nested functions

 
<?
function display_footer($site_name) {
         function display_copyright($site_name) {
              print "&copy ". date("Y"). " $site_name.";
         }
         print "contact us<br>";
         display_copyright($site_name);
}
$site_name = "PHP";
display_footer($site_name);
?>



Variables Defined Outside Functions Are Inaccessible from Within a Function by Default

 
<html>
<head>
<title>No Default Access to Globals in Functions</title>
</head>
<body>
<div>
<?php
    $life = 42;
    function meaningOfLife() {
      print "The meaning of life is $life<br />";
    }
    meaningOfLife();
?>
</div>
</body>
</html>