PHP Introductory Learning--function Chapter

Source: Internet
Author: User

This article source: http://www.zretc.com/technologyDetail/442.html

To get started with php, the function is a must-have, and here's what you need to know:

Definition of a function

function definition

Function name ([parameter 1, parameter 2, parameter 3, ...] ){

function body;

[return value;]

}

Function call

Function name ([parameter 1, parameter 2, parameter 3, ...] );

For example:

define function sum

Functionsum ($a, $b) {

$sum _value= $a + $b;

return$sum_value;

}

Call function sum

Sum (100,300);

Description

1.function: Define PHP function keywords;

2.sum: Name of function;

3. $a, $b: parameters;

4.return: return worth keyword ($sum _value return value);

second, variable function

The so-called mutable function, that is, the function is called by the value of the variable, because the value of the variable is variable, so you can call different functions by changing the value of a variable. Often used in callback functions, function lists, or depending on dynamic parameters to invoke different functions. The variable function is called by the variable name in Parentheses.

Function name () {

Echo ' Jobs ';

}

$func = ' name ';

$func (); Calling a mutable function

three, built-in functions

Built-in functions refer to Php's Default supported functions, and PHP contains a number of standard common processing functions, including string processing, array functions, file handling, session and Cookie Processing.

For example, a string substitution can be implemented by using the built-in function str_replace. The following example replaces "jobs" with "steven jobs":

$str = ' I am jobs ';

$str = str_replace (' jobs ', ' steven Jobs ', $str);

Echo $str; The result is "i am Steven jobs"

Iv. determining whether a function exists

1.function_exists determine if a function exists

Functionfunc () {

}

If (function_exists (' func ')) {

Echo ' exists ';

}

2.method_exists the existence of a method of judging a class

3.class_exists to determine whether a class exists

classmyclass{

}

Check if class exists before use

If (class_exists (' MyClass ')) {

$myclass = new MyClass ();

}

4.file_exists determine if a file exists

$filename = ' Test.txt ';

If (!file_exists ($filename)) {

Echo$filename. ' Not Exists. ';

}

Learn more about getting started with PHP learn more in the soft International education Group Technical Knowledge base!

PHP Introductory Learning--function Chapter

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.