Home php教程 php手册 PHP3中文文档(续4)

PHP3中文文档(续4)

Jun 21, 2016 am 08:57 AM
function include nbsp quot

中文

    为了理解SWITCH语句为了躲避语句冗长,知道它如何被执行的是非常重要的.SWITCH语句石一行一行     执行的(事实上,是一条语句一条语句).在开始时,没有代码被执行.仅仅当一个值于SWITCH表达式的语句的值相同的CASE语句被发现后,PHP继续执行语句直到SWITCH体的结束,或者出现了BREAK语句.如果你在一个分支语句的后面没有写BREAK语句,PHP将继续执行下面分支的语句.例如:

 

/* example 3 */

 

switch ($i) {

case 0:

print "i equals 0";

case 1:

print "i equals 1";

case 2:

print "i equals 2";

}

 

    这里,如果$i等于0,PHP将会执行所有打印语句.如果$i等于1,PHP将会执行剩下的两个打印语句,并且仅仅当$i等于2的时候,你才能得到你期望的执行结果,只有’I equals 2’被显示.因此不要在每一个分支语句的后面忘记BREAK语句(即使你在一定的环境下有可能想避免提供他们).

    一个特殊的分支是默认的分支.这个分支可以匹配任何其它分支没有匹配的事情.例如:

 

/* example 4 */

 

switch ($i) {

case 0:

print "i equals 0";

break;

case 1:

print "i equals 1";

break;

case 2:

print "i equals 2";

break;

default:

print "i is not equal to 0, 1 or 2";

}

 

    另外一个值得一提的事实是CASE表达式可以是任何计算标量类型的表达式,也就是说,整型或实型和字符型.数组和对象将不会导致PHP崩溃,但是他们没有没有任何意义.

 

REQUIRE
    REQUIRE语句使用指定的文件来代替它自己,与C语言中的#include语句非常相似。这意味着你不能把一个require()语句放在一个循环体中,并且期望它在每一次反复中都包含不同文件的内容.为了达到这个目的,可以使用INCLUDE语句。

    require ('header.inc');

     

INCLUDE
    INCLUDE语句包括和计算指定文件.

    在每次遭遇INCLUDE语句的时候都会进行一次.因此你可以在一个循环体中使用INCLUDE语句.以用来包含一些不同的文件.

$files = array ('first.inc', 'second.inc', 'third.inc');

for ($i = 0; $i
include($files[$i]);

}

    INCLUDE()与REQUIRE()语句是不同的,include语句是每一次都重新计算(并且仅仅当它被执行的时候),而require()语句当他第一次被碰到的时候,用指定的文件来代替它本身,无论这个文件的内容是否被计算(例如,如果它出现在一个条件为FALSE的if语句中).

    因为include()是一个特殊的语言结构,你必须在一个语法块中封闭它,如果它在一个条件块中.

 

    /*错误,不会同设想一样工作. */

 

if ($condition)

include($file);

else

include($other);

 

/*下面是正确的. */

 

if ($condition)

{

include($file);

}

else {

include($other);

}

 

    当一个文件被计算的时候,语法分析将处在”HTML-模式”中,这个模式将输出这个文件的内容直到第一个PHP开始标签()被遇到.

    参考 readfile(),virtual().

函数

    一个function(函数)可以使用如下语法来定义

function foo ($arg_1, $arg_2, ..., $arg_n) {

echo "Example function.\n";

return $retval;

}

    任何有效的PHP代码可以出现在function(函数)中,即使是其它的函数或类定义.

    Function必须在他们被引用之前定义.

返回值

    使用一些可选的返回语句,任何类型的数值都能被返回,包括对象和广义表.

function my_sqrt ($num)

{

return $num * $num;

}

echo my_sqrt (4); // 输出 '16'.

 

    可以返回多值,但是同时的影响使通过一个广义表来实现:

function foo()

{

return array (0, 1, 2);

}

list ($zero, $one, $two) = foo();

参数

    通过参数表可以传递信息到函数,这个表可以是变量或常量以逗号(,)分隔构成的。

    PHP3支持使用值来传递变量(默认),通过调用传递,和默认的参数值.可变长度的参数列表目前不被支持,但是可以通过传递数组来实现.

function takes_array($input) {

echo "$input[0] + $input[1] = ", $input[0]+$input[1];

}

通过调用传递(Passing by reference)

    默认的,函数参数通过值来传递.如果你希望允许一个函数可以修改它的参数的值,你可以通过调用来传递他们.

    如果你希望一个函数参数意志通过引用被传递,你可以预先函数定义中在参数名前加符号(&):

function foo( &$bar ) {

$bar .= ' and something extra.';

}

$str = 'This is a string, ';

foo ($str);

echo $str; // 输出 'This is a string, and something extra.'

    如果你希望向一个不是用这种方式定义的函数用调用的方式传递参数,你可以在函数调用中的参数名称前加符号(&).

function foo ($bar) {

$bar .= ' and something extra.';

}

$str = 'This is a string, ';

foo ($str);

echo $str; //输出 'This is a string, '

foo (&$str);

echo $str; //输出 'This is a string, and something extra.'

默认值
    一个函数对于标量参数可以定义C++-风格的默认值.

function makecoffee ($type = "cappucino") {

echo "Making a cup of $type.\n";

}

echo makecoffee ();

echo makecoffee ("espresso");

    上面的程序段的输出如下:

Making a cup of cappucino.

Making a cup of espresso.

    默认值必须是一个常量表达式,不是一个变量或类成员.

    注意当时用默认参数时,任何默认都应该在任何非默认参数右边;否则,事情将不会想你所想的那样.考虑下面的程序段:

function makeyogurt ($type = "acidophilus", $flavour) {

return "Making a bowl of $type $flavour.\n";

}

echo makeyogurt ("raspberry"); // 将不会按照预想工作

    上面例子的输出是:

Warning: Missing argument 2 in call to makeyogurt() in

/usr/local/etc/httpd/htdocs/php3test/functest.html on line 41

Making a bowl of raspberry .

    现在,用下面的对比上面的:

function makeyogurt ($flavour, $type = "acidophilus"){

return "Making a bowl of $type $flavour.\n";

}

echo makeyogurt (“raspberry”);//正常工作

    这个例子的输出是:

    Making a bowl of acidophilus raspberry.

OLD_FUNCTION

    OLD_FUNCTION语句允许你声明一个使用PHP/FI2 的语法的函数(你需要做的就是把’function’替换成’old_function’.

    这是一个不被赞成的特征,并且应该仅仅被PHP/FI2到PHP3的转换这使用.

    定义成为OLD_FUNCTION模式的函数不能被PHP的内部代码调用。其中之一的情况是,意味着您不能在象as usort()、array_walk()和register_shutdown_function().这样的函数中使用使用他们。您可以使用编写交换函数的方法来调用OLD_FUNCTION模式的函数(在普通的PHP3表单中)。

CLASS

    一个class是一个变量和使用这些变量的函数的组合。定义class使用如下的语法:


// Add $num articles of $artnr to the cart

function add_item ($artnr, $num) { $this->items[$artnr] += $num; }

// Take $num articles of $artnr out of the cart

function remove_item ($artnr, $num) {

if ($this->items[$artnr] > $num) {

$this->items[$artnr] -= $num;

return true;

} else {

return false;

}

}

}

?>

 

    如上所示,定义了一个名字为Cart的class。这个class由多个描写物品的数组和添加项目、删除项目的函数组成。

 

    Class是一种类型,这就是说,它是实际变量的设计蓝图。您可以根据设计建立一个变量组和针对它们的一些新的操作。示例如下:

 

$cart = new Cart;

$cart->add_item("10", 1);

 

    如上所示,建立了一个类型为class Cart的对象$cart。这个对象中的函数add_item()被调用来添加一个物品号为10的项目。

 

    Class可以使用其他的Class来扩展。这个扩展的或者继承的class(类)拥有基本class所有的变量和函数同时您还可以在其中加如您自己做的扩展定义。要这样定义需要使用扩展定义的关键词。

 

class Named_Cart extends Cart {

var $owner;

function set_owner ($name) {

$this->owner = $name;

}

}

 

    以上定义了一个名字为Named_Cart的class(类),这个类拥有所有包含于类Cart中的变量和函数,同时还添加了一个变量$owner和函数set_owner()。您可以建立一个有名字的cart,同时获得cart的拥有者名字。您还可以在类Named_Cart中,使用普通的属于类cart的函数。

 

$ncart = new Named_Cart; // Create a named cart

$ncart->set_owner ("kris"); // Name that cart

print $ncart->owner; // print the cart owners name

$ncart->add_item ("10", 1); // (inherited functionality from cart)

 

    在这个类的函数中变量$this表示这个对象。在当前的对象中,您可以使用$this->something来访问任何变量和函数。

 

    当您建立一个新的类时,有一个构造器函数将被自动的调用。如果某个函数的名字和类的名字一样,那么它就成为构造器:

 

class Auto_Cart extends Cart {

function Auto_Cart () {

$this->add_item ("10", 1);

}

}

 

    在上面的例子中,定义了一个名字为Auto_Cart的类,它是在原来的类Cart中加上了一个构造器函数,这个构造器函数初始化了类Cart使用的方法是:在每次建立一个类的时候添加一个物品号为10的一个项目。构造器还可以显示一些信息,这些信息可以被随意的挑选,这使他们可以变的十分的有用,

 

class Constructor_Cart {

function Constructor_Cart ($item = "10", $num = 1) {

$this->add_item ($item, $num);

}

}

// Shop the same old boring stuff

$default_cart = new Constructor_Cart;

// Shop for real...

$different_cart = new Constructor_Cart ("20", 17);





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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months 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)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

How to Fix Activation Error Code 0xc004f069 in Windows Server How to Fix Activation Error Code 0xc004f069 in Windows Server Jul 22, 2023 am 09:49 AM

The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Go through these initial checks, and if they don't help you activate your system, jump to the main solution to resolve the issue. Workaround – close the error message and activation window. Then restart the computer. Retry the Windows activation process from scratch again. Fix 1 – Activate from Terminal Activate Windows Server Edition system from cmd terminal. Stage – 1 Check Windows Server Version You have to check which type of W you are using

See all articles