Table of Contents
'.$tablename.'
Home php教程 php手册 php学习笔记之 函数声明_php入门_脚本之家

php学习笔记之 函数声明_php入门_脚本之家

Jun 06, 2016 pm 08:37 PM
function declaration

函数必须调用才能执行,可以在声明之前调用,也可以在声明之后调用

代码如下:
/* 函数定义:
* 1.函数是一个被命名的
* 2.独立的代码段
* 3.函数执行特定任务
* 4.并可以给调用它的程序返回一个值
*
* 函数的优点:
* 1.提高程序的重用性
* 2.提高程序的可维护性
* 3.可以提高开发效率
* 4.提高软件的可靠性
* 5.控制程序的复杂性
*
* 函数的声明
* function 函数名(){
*
* }
*
* function 函数名(参数1,参数2,参数...)
* {
* 函数体
* }
*
* function 函数名()
* {
* 函数体;
* 返回值;
* }
* function 函数名(参数列表...)
* {
* 函数体;
* 返回值
* }
*
* 注意:
* 一、函数必须调用才能执行,可以在声明之前调用,也可以在声明之后调用
* 二、函数名命名和变量一样,aaa bbb ccc aaaBbbCcc(第一单词小写,以后每个单词首字母大写)
* 三、函数在声明时不能重名
* 四、可以通过向函数传递参数,改变函数的行为
* 形参:在声明函数时,声明的参数,参数就是变量,多个参数用,分开
* 实参:调用函数时传给形参数值(数据,也可以是变量)
* 五、如果没有返回值则称为过程
* 六、通过使用return语句返回数据
* 七、函数执行到return语句就结束,不要再这个语句后写代码,也可以用return结束函数的执行
*
* 函数名的作用:
* 1.调用函数,开始执行函数
* 2.可以向函数中传递数据
* 3.函数名就是返回的值
*
*
*/
//一个输出表格的函数
function table($tablename,$width,$row,$col)
{
echo '';
echo '';
for($i=0;$i{
//隔行换色
if($i%2==0)
$bg="#cccccc";
else
$bg="yellow";
echo '';//输出行
for($j=0;$j{
echo '';

}
echo '

'.$tablename.'

'.($i*$row+$j).'{
echo '';
echo '';
for($i=0;$i{
//隔行换色
if($i%2==0)
$bg="#cccccc";
else
$bg="yellow";
echo '';//输出行
for($j=0;$j{
echo '';
}
echo '';

}
echo '

'.$tablename.'

'.($i*$row+$j).'
';

}/td>';
}
echo '
';

}
table("输出表格",600,10,10);
table("输出表格2",300,6,6);

//另一种输出方式
function table2($tablename,$width,$row,$col)
{
$str='';
$str.= '';
for($i=0;$i{
//隔行换色
if($i%2==0)
$bg="#cccccc";
else
$bg="yellow";
$str.='';//输出行
for($j=0;$j{
$str.='';
}
$str.='';

}
$str.='

'.$tablename.'

'.($i*$row+$j).'
';

return $str;
}

echo table2("直接输出表格",400,15,15);
?>
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1673
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
Default parameters in C++ function declarations: a comprehensive analysis of their declaration and usage Default parameters in C++ function declarations: a comprehensive analysis of their declaration and usage May 02, 2024 pm 03:09 PM

Default parameters in C++ provide the ability to specify default values ​​for function parameters, thereby enhancing code readability, simplicity, and flexibility. Declare default parameters: Add the "=" symbol after the parameter in the function declaration, followed by the default value. Usage: When the function is called, if optional parameters are not provided, the default values ​​will be used. Practical case: A function that calculates the sum of two numbers. One parameter is required and the other is optional and has a default value of 0. Advantages: Enhanced readability, increased flexibility, reduced boilerplate code. Note: It can only be specified in the declaration, it must be at the end, and the types must be compatible.

What impact does the order of declaration and definition of C++ functions have? What impact does the order of declaration and definition of C++ functions have? Apr 19, 2024 pm 01:42 PM

In C++, the order of function declarations and definitions affects the compilation and linking process. The most common is that the declaration comes first and the definition comes after; you can also use "forwarddeclaration" to place the definition before the declaration; if both exist at the same time, the compiler will ignore the declaration and only use the definition.

What is the difference between C++ function declaration and definition? What is the difference between C++ function declaration and definition? Apr 18, 2024 pm 04:03 PM

A function declaration informs the compiler of the existence of the function and does not contain the implementation, which is used for type checking. The function definition provides the actual implementation, including the function body. Key distinguishing features include: purpose, location, role. Understanding the differences is crucial to writing efficient and maintainable C++ code.

C++ function declarations and definitions C++ function declarations and definitions Apr 11, 2024 pm 01:27 PM

Function declaration and definition are necessary in C++. Function declaration specifies the return type, name and parameters of the function, while function definition contains the function body and implementation. First declare the function and then use it in your program passing the required parameters. Use the return statement to return a value from a function.

[[nodiscard]] in C++ function declarations: Demystifying the consequences of ignoring return values [[nodiscard]] in C++ function declarations: Demystifying the consequences of ignoring return values May 01, 2024 pm 06:18 PM

The [[nodiscard]] attribute indicates that the return value of the function must not be ignored, otherwise it will cause a compiler warning or error to prevent the following consequences: uninitialized exceptions, memory leaks, and incorrect calculation results.

C++ compilation error: function call does not match function declaration, how to solve it? C++ compilation error: function call does not match function declaration, how to solve it? Aug 22, 2023 pm 12:39 PM

C++ compilation error: function call does not match function declaration, how to solve it? When developing C++ programs, you will inevitably encounter some compilation errors. One of the common errors is that the function call does not match the function declaration. This kind of error widely exists among C++ programmers. Due to not paying attention to the correctness of function declaration, it leads to compilation problems, which ultimately wastes time and energy to fix the problem and affects development efficiency. Ways to avoid this mistake require following some norms and standard practices, let’s take a look at them below. What is a function call versus a function declaration?

Detailed syntax of C++ function declaration: from syntax analysis to standard usage analysis Detailed syntax of C++ function declaration: from syntax analysis to standard usage analysis Apr 30, 2024 pm 02:54 PM

The C++ function declaration syntax is: returnTypefunctionName(parameterType1parameterName1,...,parameterTypeNparameterNameN);, where returnType is the return type, functionName is the function name, parameterType is the parameter type, and parameterName is the parameter name, which must end with a semicolon.

A step-by-step guide to C++ function declaration: detailed instructions covering every step A step-by-step guide to C++ function declaration: detailed instructions covering every step May 02, 2024 pm 04:33 PM

A function declaration tells the compiler that a function exists without providing a function body. The steps are as follows: specify the function return type (void if there is no return value) define the function name and declare the function parameters (optional, including data type and identifier) ​​plus semicolon

See all articles