您的位置 首页 php

php应用:php快速入门

php应用:php快速入门

php脚本的后面名为 .php ,代码放置在下面的括号里面:

<?php…….?>

echo 可以打印信息,类似于 printf

<?phpecho “hallo world”;?>

每条语句后面用分号结尾 ;

php支持三种注释方式:

<?php// 第一种# 第二种/*

这是

多行

注释

*/?>

在php中,函数、类、和关键词的大小写都是同一个东西:

<!DOCTYPE html><html><body><?phpECHO “Hello World!<br>”;echo “Hello World!<br>”;EcHo “Hello World!<br>”;?></body></html>

不过所有变量都对大小写敏感,需要区分大小写。

<?php$color=”red”;$Color=”black”;echo “my car is ” . $color . “<br>”;echo “my car is ” . $Color . “<br>”;?>

上面的例子同时指出了变量定义以及 字符串 拼接的语法。

变量命名规则:

PHP 变量规则:

变量以 $ 符号开头,其后是变量的名称

变量名称必须以字母或下划线开头

变量名称不能以数字开头

变量名称只能包含字母数字字符和下划线(A-z、0-9 以及 _)

变量名称对大小写敏感($y 与 $Y 是两个不同的变量)

变量会在第一次赋值时被创建。无需声明变量的类型。

变量会有三种不同的 作用域

PHP 有三种不同的变量作用域:

local(局部)global(全局) static (静态)

函数之外声明的变量拥有 Global 作用域,只能在函数以外进行访问。

函数内部声明的变量拥有 LOCAL 作用域,只能在函数内部进行访问。

下面的例子测试了带有局部和全局作用域的变量:<?php$x=5; // 全局作用域function myTest() { $y=10; // 局部作用域

echo “<p>测试函数内部的变量:</p>”; echo “变量 x 是:$x”; echo “<br>”; echo “变量 y 是:$y”;

}

myTest();echo “<p>测试函数之外的变量:</p>”;echo “变量 x 是:$x”;echo “<br>”;echo “变量 y 是:$y”;?>

运行结果:

测试函数内部的变量:

变量 x 是:

变量 y 是:10测试函数之外的变量:

变量 x 是:5变量 y 是:

比较奇怪的就是为什么 全局变量 不能在局部函数内访问。

其实可以访问,不过需要global关键字的帮助:

global 关键词用于访问函数内的全局变量。

要做到这一点,请在(函数内部)变量前面使用 global 关键词:<?php$x=5;$y=10;function myTest() { global $x,$y; $y=$x+$y;

}

myTest();echo $y; // 输出 15?>

PHP 同时在名为 $GLOBALS[index] 的数组中存储了所有的全局变量。下标存有变量名。这个数组在函数内也可以访问,并能够用于直接更新全局变量。

<?php$x=5;$y=10;function myTest() { $GLOBALS[‘y’]=$GLOBALS[‘x’]+$GLOBALS[‘y’];

}

myTest();echo $y; // 输出 15?>

通常,当函数完成执行后,会删除所有变量。不过,有时我需要不删除某个局部变量。实现这一点需要static:

<?phpfunction myTest() { static $x=0; echo $x; $x++;

}

myTest();

myTest();

myTest();?>

php中echo和print都能使用,两者的唯一区别是print返回1,echo没有返回值。

strlen函数可以返回字符串的长度。

strpos函数用来确定另外一个字符串的位置:

<?phpecho strpos(“Hello world!”,”world”);?>

完整的string可以参考手册.

使用 define 函数来定义常量:

<?phpdefine(“GREETING”, “Welcome to W3School.com.cn!”);echo GREETING;?>

define函数还有第三个参数,用来指定是否大小写敏感。

php的if-else语句和其他语言大同小异,举个例子:

<?php$t=date(“H”);if ($t<“10”) { echo “Have a good morning!”;

} elseif ($t<“20”) { echo “Have a good day!”;

} else { echo “Have a good night!”;

}?>

switch-case语句:

<?phpswitch ($x)

{case 1: echo “Number 1”; break ;case 2: echo “Number 2”; break;case 3: echo “Number 3”; break;default: echo “No number between 1 and 3”;

}?>

while、for语句和其他语言无差别,看看 foreach 吧:

<?php $colors = array(“red”,”green”,”blue”,”yellow”);

foreach ($colors as $value) { echo “$value <br>”;

}?>

php的真正力量来自于它的函数,它有1000个内置函数。

用户定义的函数声明以关单 “function” 开头:

function functionName() {

被执行的代码;

}

举个例子:

<?phpfunction writeMsg() { echo “Hello world!”;

}

writeMsg(); // 调用函数?>// 含参数<?phpfunction familyName($fname,$year) { echo “$fname Zhang. Born in $year <br>”;

}

familyName(“Li”,”1975″);

familyName(“Hong”,”1978″);

familyName(“Tao”,”1983″);?>// 默认参数<?phpfunction setHeight($minheight=50) { echo “The height is : $minheight <br>”;

}

setHeight(350);

setHeight(); // 将使用默认值 50setHeight(135);

setHeight(80);?>// 返回值<?phpfunction sum ($x,$y) { $z=$x+$y; return $z;

}echo “5 + 10 = ” . sum(5,10) . “<br>”;echo “7 + 13 = ” . sum(7,13) . “<br>”;echo “2 + 4 = ” . sum(2,4);?>

在 PHP 中,有三种数组类型:

  • 索引数组 – 带有数字索引的数组

  • 关联数组 – 带有指定键的数组

  • 多维数组 – 包含一个或多个数组的数组

索引数组:

$cars=array(” Volvo “,”BMW”,”SAAB”);<?php$cars=array(“Volvo”,”BMW”,”SAAB”);echo “I like ” . $cars[0] . “, ” . $cars[1] . ” and ” . $cars[2] . “.”;?>// count<?php$cars=array(“Volvo”,”BMW”,”SAAB”);echo count($cars);?>// 变量索引数组<?php$cars=array(“Volvo”,”BMW”,”SAAB”);$arrlength=count($cars);for($x=0;$x<$arrlength;$x++) { echo $cars[$x]; echo “<br>”;

}?>

关联数组:

$age=array(“Peter”=>”35″,”Ben”=>”37″,”Joe”=>”43”);

或$age[‘Peter’]=”35″;$age[‘Ben’]=”37″;$age[‘Joe’]=”43″;<?php$age=array(“Bill”=>”35″,”Steve”=>”37″,”Peter”=>”43”);echo “Peter is ” . $age[‘Peter’] . ” years old.”;?>// 遍历<?php$age=array(“Bill”=>”35″,”Steve”=>”37″,”Peter”=>”43”);foreach($age as $x=>$x_value) { echo “Key=” . $x . “, Value=” . $x_value; echo “<br>”;

}?>

数组排序方法有下面这些:

sort() – 以升序对数组排序

rsort() – 以降序对数组排序

asort() – 根据值,以升序对关联数组进行排序

ksort() – 根据键,以升序对关联数组进行排序

arsort() – 根据值,以降序对关联数组进行排序

krsort() – 根据键,以降序对关联数组进行排序

比较难理解的是键值对排序。

<?php$age=array(“Bill”=>”35″,”Steve”=>”37″,”Peter”=>”43”);

asort($age);?><?php$age=array(“Bill”=>”35″,”Steve”=>”37″,”Peter”=>”43”);

ksort($age);?>

超全局变量,也就是预定义的全局变量,在哪里都能用,有特殊含义:

$GLOBALS:引用全局作用域中可用的全部变量$_SERVER:保存关于报头、路径和脚本位置的信息。$_REQUEST:用于收集 HTML 表单提交的数据。$_POST:用于收集提交 method=”post” 的 HTML 表单后的表单数据。也常用于传递变量。$_GET:$_GET 也可用于收集提交 HTML 表单 (method=”get”) 之后的表单数据。$_FILES$_ENV$_COOKIE$_SESSION

文章来源:智云一二三科技

文章标题:php应用:php快速入门

文章地址:https://www.zhihuclub.com/79054.shtml

关于作者: 智云科技

热门文章

评论已关闭

35条评论

  1. Therefore, Tadalafil Daily is an ideal choice for those who have sex frequently, such as those in long term relationships Table 2 Treatment-Emergent Adverse Reactions Reported by 2 of Patients Treated with CIALIS for Once Daily Use 2

  2. Most cases of chronic bacterial prostatitis CBP can be diagnosed with history, physical examination, and urine or semen culture.

  3. There was a minor wreck early on when Marcos Ambrose got loose in front of the main grandstand and took out Juan Pablo Montoya, and 103 consecutive laps under green until the yellow and checkered flags waved together at the end A similar increased incidence in endometrial adenocarcinoma and uterine sarcoma was observed among women receiving Tamoxis tamoxifen citrate in five other NSABP clinical trials

  4. Vida npiHBjPQAArWbw 6 17 2022 Primary prevention of overweight and obesity an analysis of national survey data

  5. In particular, limb and cranial skeletal abnormalities were found in conditional mouse mutants deficient in Kif3a or in the cilia associated mechanosensory protein polycystin 1 Pkd1, as well as in mice bearing a missense mutation in the Pkd1 gene Olsen et al

  6. 5 for up to 14 days, depending upon the animal s age In the present study, we showed that in a cohort of 37 metastatic breast cancer patients there was a trend of increased overall survival in ESR2 high expressing patients compared to ESR2 low expressing patients

  7. geriforte location yasmine hammamet la marina The 30 year old, who won the World Championship titles in the 100m, 200m and 4x100m relay in 2007, took part in USADAГў

  8. That was the opinion of influential GP and campaigner Dr Louise Newson, appearing earlier this month on ITV s This Morning savitra purchase ivermectin for dogs The boat heeled to an almost 45 degree pitch on its port hull when the massive wingsail failed to tack in sync with the boat and local media reported only the work of the grinders, who continued to provide hydraulic pressure to the ram that controls the wing, allowed the catamaran to flop back into the water

  9. Cox proportional hazard ratios HR were calculated for recurrence of poor PM relative to extensive metabolizer EM phenotypes with increasing numbers of CYP2D6 variants

  10. loxitane para que es la pastilla ciprofloxacina 500 Meanwhile, Andy Pettitte 7 8 put forth his best effort in more than six weeks, limiting the Rangers to two runs over six plus innings

  11. 23 All three large studies used trastuzumab in combination either exclusively or predominantly with anthracycline based regimens, 22, 23 and all showed a significant benefit of trastuzumab, with a reduction in the rate of recurrence of approximately 50 and improvement in the rate of survival of approximately 30

  12. Animal studies of hydrocephalus have shown that a reduction in CBF occurs before a reduction in oxidative metabolism and irreversible injury 27

  13. Also, it would be interesting to determine in the future whether CP mediates infiltration also of other leukocytes such as neutrophils and T cells 30, 32 into the stroke injured brain

  14. I can tone it down if you request This drug interferes with ion transport and membrane polarization, leading to high host toxicity

  15. Based on the type of infection, and what particular bacteria is suspected or known to be causing the infection, your healthcare provider can decide if one of these drugs is appropriate for you

  16. But other than the aesthetic reasons, why should you care about increasing nitric oxide levels in the body

  17. Of course, it is not as expensive as Black Nine, Human beings have keto pills a psychology, that is, they like to compare themselves with others

  18. Congrats to your son In addition, dual treatment led to attenuation of signaling pathways, as well as cell cycle and survival pathways

  19. Monitor Closely 1 ranolazine will increase the level or effect of etoposide by P glycoprotein MDR1 efflux transporter

  20. Signs of liver, kidney disease, or hyperthyroidism can be determined by a physical examination In the oliguric patient, strategies to maintain fluid balance include fluid restriction, diuretic therapy, and renal replacement therapy

  21. Jianhua, the God of Creation, came to Man Up Male Enhancement Pills increase your testosterone Zhao Ling with wine while people were not paying attention I m actually feeling strong ovulation pains right now reminded me to check this app lol and should be 1 dpo today according to my temp spike

  22. how many follicles did you get on letrozole and then how many with the injections Artificial intelligence AI algorithms are being developed to interpret screening mammograms and breast biopsy specimens

  23. However, seldom the reduction in the particle size is alone responsible for dissolution improvement, as it is the interplay between amorphicity and particle size reduction, which achieves the goal of a higher dissolution rate The CCND1 amplification status and protein expression of cyclin D1 were assessed by chromogenic in situ hybridisation and immunohistochemistry, respectively, in 1, 155 postmenopausal, oestrogen receptor positive breast cancer patients included in the TransATAC substudy

  24. Values are expressed as the percentage of vehicle treated controls DMSO in the respective treatment condition Marin Acevedo JA, Dholaria B, Soyano AE, Knutson KL, Chumsri S, Lou Y

  25. Side effects include rash, itching, nausea, diarrhea, sleepiness, liver enzyme elevation and blood disorders

网站地图