PHP Introductory tutorials for custom function usage (create, invoke, variable, parameter, return value, etc.) _php tips

Source: Internet
Author: User
Tags getting started with php php database

This example describes the use of PHP custom functions. Share to everyone for your reference, specific as follows:

demo1.php

<?php
  //standard function, built-in function
  echo MD5 (' 123456 ');
  Echo ' <br/> ';
  echo SHA1 (' 123456 ');
  Echo ' Read who asked you to chant, water fell fragrance floating. ';
? >

demo2.php

<?php
  //create function, not with the system's built-in function name
  //function has a feature, must be called to execute
  //no parameter representation () inside is empty, no return is the function of the program no
  returns function functionname () {
    echo ' I am a functions without arguments and no returns. ';
  }
  functionname ();
? >

demo3.php

<?php
  ///contains functions with no return value for parameters//In general
  , write functions that do not need to
  be modified//changed are generally incoming parameters
  function Functionarea ($radius) {
    $area = $radius * $radius * PI ();
    The echo ' radius is '. $radius. ' Area is: '. $area;
  }
  Call
  Functionarea (a);
? >

demo4.php

<?php
  //contains parameters, with return value
  function Functionarea ($radius) {
    $area = $radius * $radius * PI ();
    return $area;
  }
  Call
  //This greatly enhances the flexibility of the function
  //functionarea (20); The whole gets a value in memory.
  the echo ' radius of 20 is: '. Functionarea
>

demo5.php

<?php
  //contains parameters, with return value//
  This $radius = 10, where 10 indicates the default value of this parameter
  //If the calling function does not pass a parameter to the function, then the default value function is enabled
  Functionarea ($radius = ten) {
    $area = $radius * $radius * PI ();
    return $area;
  }
  Call
  //This greatly enhances the flexibility of the function
  //functionarea (20); The whole gets a value in memory.
  the area of echo ' Radius 20 is: '. Functionarea ();
? >

demo6.php

<?php//
  write a function where the function returns three data functions
  functioninfo ($name, $age, $job) {
    //$userInfo is the array
    //$ UserInfo = Array ($name, $age, $job);
    $userInfo [] = $name;
    $userInfo [] = $age;
    $userInfo [] = $job;
    return $userInfo;
  }
  Call function
  Print_r (Functioninfo (' One-stop web site ', 19, ' Programmer '));
$arr = Functioninfo (' One-stop-doing website ', 19, ' Programmer ');
echo $arr [0];
List ($name, $age, $job) = Functioninfo (' One-stop web site ', 19, ' Programmer ');
echo $name. ' This year '. $age, ' He was a '. $job;
>

demo7.php

<?php
  //Before the reference, are by the value of the reference
  $prices =;
  $tax = 0.5;
  This function does not have any value, is currently in the value
  of the parameters//function variables and functions outside the variables have no relationship function
  functionprices (& $prices,& $tax) {
    //inside of $ Prices this variable is already
    $prices = $prices + $prices * $tax;
    $tax = $tax * $tax;
    echo $prices;//75
    echo ' <br/> ';
    echo $tax;//0.25
    echo ' <br/> ';
  }
  Functionprices ($prices, $tax)  ;
  Citing this concept, we are currently unable to grasp, will be in OOP time, focus on the
  echo $prices,//By value 50, by reference to become the
  echo ' <br/> ';
  echo $tax;
? >

demo8.php

<?php
  //Understanding global variable
  //can set $a to global variable
  $a = 5;
  function fa () {
    global $a;//set $a as global variable
    $a = 2;
  }
  FA ();
  echo $a;  2
?>

demo9.php

<?php
  //Using Super global variable
  $GLOBALS [' a '] = 5;
  function fa () {
    $GLOBALS [' a '] = 2;
  }
  FA ();
  echo $GLOBALS [' a '];
  Print_r ($GLOBALS [' a ']);
? >

demo10.php

<?php
  //This sentence contains the function to include
  ' library/tool.library.php ';
  Echo Functionpi ();
? >

demo11.php

The <?php
  //include () statement contains and runs the specified file.
//include ' demo1.php ';
Include ' demo1.php ';
  The include_once () statement contains and runs the specified file during the execution of the script.
  //This behavior is similar to the include () statement, except that if the code in the file is already contained, it is not included again.
//include_once ' demo1.php ';
Include_once ' demo1.php ';  Contains only referenced files once
  //include if it does not exist, tell you two warnings, and then continue to execute
  //require if not stored, directly to the error, and then stop the implementation of the
  require ' demo1.php ';
  We recommend the use of require
//require ' demo1.php ';
Require_once ' demo1.php ';
Require_once ' demo1.php ';
  Echo ' <strong> read who asked you to chant, water fall fragrance floating. </strong> '
?>

demo12.php

<?php
  //__file__
  /Magic constant--here's a constant that's just a value./
/$file = __file__;//c:\appserv\www\basic7\demo12.php
//echo $file;
  As a result, it is recommended that you use __file__ for faster
  echo dirname (__file__) When you include files,//c:\appserv\www\basic7 require (dirname
  __).' \demo1.php ');//c:\appserv\www\basic7
  echo __line__;
  function ffff () {return
    __function__;
  }
  Echo '  
 

For more information on PHP related content readers can view the site topics: "PHP common functions and techniques summary", "PHP Basic Grammar Primer", "PHP operation and operator Usage Summary", "PHP object-oriented Program Design Introductory Course", "PHP Network Programming Skills Summary", "PHP Array ( Array), "Summary of PHP string usage", "Getting Started with Php+mysql database operations" and "Summary of common PHP database Operations Tips"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.