Leilin Peng Share: PHP function

  The real power of PHP comes from its functions.

  In PHP, it offers more than 1000 built-in functions.

  PHP built-in function

  For a complete reference manual and examples of all array functions, visit our PHP Reference.

  PHP function

  In this chapter, we will show you how to create your own functions.

  To execute the script when the page loads, you can put it in a function.

  Function is performed by calling the function.

  You can call the function anywhere on the page.

  Create a PHP function

  Function is performed by calling the function.

  

  function functionName()

  {

  // code to be executed

  }

  ?>

  PHP function guidelines:

  The name of the function should prompt its function

  Function name starts with a letter or an underscore (can not start with a digit)

  Examples

  A simple function when it is called I can output the name:

  

  function writeName()

  {

  echo "Kai Jim Refsnes";

  }

  echo "My name is ";

  writeName();

  ?>

  Output:

  My name is Kai Jim Refsnes

  PHP Functions - Adding parameters

  To add more functionality to a function, we can add parameters. Parameters similar variables.

  Parameters just behind a designated function names are in parentheses.

  Example 1

  The following examples will output a different name, but the name is the same:

  

  function writeName($fname)

  {

  echo $fname . " Refsnes.
";

  }

  echo "My name is ";

  writeName("Kai Jim");

  echo "My sister's name is ";

  writeName("Hege");

  echo "My brother's name is ";

  writeName("Stale");

  ?>

  Output:

  My name is Kai Jim Refsnes.

  My sister's name is Hege Refsnes.

  My brother's name is Stale Refsnes.

  Example 2

  The following function has two parameters:

  

  function writeName($fname,$punctuation)

  {

  echo $fname . " Refsnes" . $punctuation . "
";

  }

  echo "My name is ";

  writeName("Kai Jim",".");

  echo "My sister's name is ";

  writeName("Hege","!");

  echo "My brother's name is ";

  writeName("Ståle","?");

  ?>

  Output:

  My name is Kai Jim Refsnes.

  My sister's name is Hege Refsnes!

  My brother's name is Ståle Refsnes?

  PHP Functions - Return values

  To make the function returns a value, use the return statement.

  

  function add($x,$y)

  {

  Total = $ $ $ x + y;

  return $total;

  }

  echo "1 + 16 = " . add(1,16);

  ?>

  Output:

  1 + 16 = 17

  View all PHP tutorial article: https://www.codercto.com/courses/l/5.html

  (Editor: Leilin Peng Source: network intrusion deleted)

Guess you like

Origin www.cnblogs.com/linpeng1/p/11002393.html