国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 php手冊 PHP 語法

PHP 語法

Jun 21, 2016 am 09:14 AM
people quot

語法

PHP 基本語法看起來很熟悉。




echo "Hello, World!";

?>

運行結(jié)果:

Hello, World!

變量標記為“$”。你也可以將上面的“Hello, World!”寫為如下代碼:


$message = "Hello, World!";

echo $message;

?>

字符串的串聯(lián)則是用“.”(一個英文句點)標示出來;其他有關數(shù)字的運算符號則如同你所預期的一樣:


$greeting = "Hello ";

$num = 3 + 2;

$num++;

echo "$greeting $num people!";

?>

運行結(jié)果:

Hello 6 people!

PHP 有完整的運算符,其功能同你所預期的一樣——特別是當你有C或者C++的知識背景時的時候。使用PHP一條很好的經(jīng)驗法則是:“遇到問題,先試試看,很可能成功?!?br>
正如在Perl一樣,一個字符串用雙引號括起來,這將使得其中的變量被值置換,而如果以單引號括起來,則不會。因此,如下代碼:


$name = 'Susannah';

$greeting_1 = "Hello, $name!";

$greeting_2 = 'Hello, $name!';

echo "$greeting_1\n";

echo "$greeting_2\n";

?>

運行結(jié)果:

Hello, Susannah!

Hello, $name!

注意字串中“\n ”是換行符號,就跟在Perl或C一樣。不過這僅在以雙引號括起來的字符串內(nèi)才有效。

變量

PHP 可以把環(huán)境變量當作一般變量使用。這包括由服務器為一段CGI 程序所設定的環(huán)境變量(即使當你以模塊方式執(zhí)行PHP)。因此,如果頁面http://www.domain.com/farm/cattle/cow-cow.cow.html包括以下代碼:


echo "[$REQUEST_URI]";

?>

它將輸出[/farm/cattle/cow-cow-cow.html]

數(shù)組
使用方括?。╗ and ])設置數(shù)組索引(一般或關聯(lián)性):



$fruit[0] = 'banana';

$fruit[1] = 'papaya';

$favorites['animal'] = 'turtle';

$favorites['monster'] = 'cookie';

如果你對數(shù)組賦值,但索引是空白的,PHP則會把對象放于數(shù)組末尾。上面對變量$fruit的聲明同下面代碼的結(jié)果是一樣的:

$fruit[] = 'banana';

$fruit[] = 'papaya';

你也可以用多維數(shù)組:

$people['David']['shirt'] = 'blue';

$people['David']['car'] = 'minivan';

$people['Adam']['shirt'] = 'white';

$people['Adam']['car'] = 'sedan';

一個創(chuàng)建數(shù)組的簡便方法是

array()

函數(shù)為:

$fruit = array('banana','papaya');

$favorites = array('animal' => 'turtle',

'monster' => 'cookie);

或者

$people = array ('David' => array('shirt' => 'blue',

'car' => 'minivan'),

'Adam' => array('shirt' => 'white',

'car' => 'sedan'));

內(nèi)建函數(shù)count()表明一個數(shù)組里有多少元素:

$fruit = array('banana','papaya');

print count($fruit);

得到如下結(jié)果

2


控制結(jié)構
你可以利用循環(huán)結(jié)構例如for以及while:



for ($i = 4; $i
print "I have eaten $i bagels today.\n"; }

結(jié)果

I have eaten 4 bagels today.

I have eaten 5 bagels today.

I have eaten 6 bagels today.

I have eaten 7 bagels today.

同樣可寫為

$i = 4; while ($i
print "I have eaten $i bagels today.\n";

$i++;

}

你可以使用控制結(jié)構if以及elseif:

if ($user_count > 200) {

print "The site is busy right now!";

} elseif ($user_count > 100) {

print "The site is sort of active right now!";

else {

print "The site is lonely - only $user_count user logged on.";

}

使用運算符的經(jīng)驗法則同樣也可以運用在控制結(jié)構上面。你還可以使用switch,do...while,甚至是 ?: 結(jié)構。



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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 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)