PHP编程语言手册:php语法初识
1.什么都不说,来个Hello World
先 : echo 输出
感觉挺爽的嘛,SpringBoot的HelloWorld比这麻烦多了
---->[Hello.php]---------------------------
<?php
echo "Hello World!";
复制代码
2.变量 $ 名称 = 变量
---->[vartest.php]---------------------------
<?php
$name="张风捷特烈";//生成变量
echo $name;//输出变量
echo "<br />";//输出换行
$name="toly";//改变变量
echo $name;//输出变量
复制代码
3.几种数据组织形式
感觉比JavaScript还要奔放,比Python要收敛一丢丢。就是$符号太多...晃眼
<?php
$t = true;//1.布尔型
$fa = false;//布尔型
$i = 5; //2.整型
$a = 0xff; // 支持十六进制数 -- 255
$b = 074; // 支持八进制数 -- 60
$f = 3.14159;//3.浮点型
$f1 = 3.14159e4;//浮点型 31415.9
$f2 = 3.14159e-3;//浮点型 0.00314159
$str = "String Type";//4.字符串
$str = 'String Type';//字符串
$color = array("red", "green", "blue");//5.数组
echo $color[0];//获取元素
$colorMap = array("red"=>"红色", "green"=>"绿色", "blue"=>"蓝色");//6.类似映射或字典
echo $colorMap["red"];//红色
$shape = new Shape("四维空间");//7.对象
echo $shape->getName(); //调用方法
$n=null;//8.null
class Shape{
var $name;
function __construct($name = "green"){
$this->name = $name;
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @param string $name
*/
public function setName($name){
$this->name = $name;
}
}
复制代码
4.看一下请求和响应:Hello.php
|--- 客户端请求 (此时不太关心)
GET http://toly1994328.com/Hello.php HTTP/1.1
Host: toly1994328.com
Connection: keep-alive
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.110 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
|--- 服务端响应
HTTP/1.1 200 OK
Date: Wed, 13 Mar 2019 14:06:15 GMT
Server: Apache/2.4.23 (Win64) PHP/5.6.25
X-Powered-By: PHP/5.6.25
Content-Length: 13
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Type: text/html; charset=UTF-8 <----text/html 表示可以让浏览器解析请求体中的html代码
Hello World! <---- 可见echo可以将数据放入请求体里
复制代码
5.小测试
既然echo可以输出,放个静态页面输出会怎么样,就来个粒子吧
这说明echo可以将整个html页面输出让浏览器解析,这样的话php的地位应该是控制服务端的输出流。

<?php
echo "<!doctype html>
<html>
<head>
<meta charset=\"utf-8\">
<title>液态粒子字体</title>
<style>
body,html {
margin:0;
width:100%;
overflow:hidden;
}
canvas {
width:100%;
}
.control {
position:absolute;
}
.control input {
border:0;
margin:0;
padding:15px;
outline:none;
text-align:center;
}
.control button {
border:0;
margin:0;
padding:15px;
outline:none;
background:#333;
color:#fff;
}
.control button:focus,.control button:hover {
background:#222
}
</style>
</head>
<body>
<div class = \"control\" style = \"position:absolute\">
<input type = \"text\" value = \"张风捷特烈\" id = \"t\">
<button onclick = \"changeText(t.value)\">
change
</button>
</div>
<script>
var can = document.createElement(\"canvas\");
document.body.appendChild(can);
var ctx = can.getContext('2d');
function resize(){
can.width = window.innerWidth;
can.height = window.innerHeight;
}
const max_radius = 3;
const min_radius = 1;
const drag = 50;
window.onresize = function(){
resize();
};
function cfill(){
ctx.fillStyle = \"#000\";
ctx.fillRect(0,0,can.width,can.height);
ctx.fill();
}
var mouse = {
x:-1000,
y:-1000
};
can.onmousemove = function(e){
mouse.x = e.clientX;
mouse.y = e.clientY;
};
can.ontouchmove = function(e){
mouse.x = e.touches[0].clientX;
mouse.y = e.touches[0].clientY;
};
resize();
cfill();
function distance(x,y,x1,y1){
return Math.sqrt( ( x1-x ) * ( x1-x ) + ( y1-y ) * ( y1-y ) );
}
class Particle{
constructor(pos,target,vel,color,radius){
this.pos = pos;
this.target = target;
this.vel = vel;
this.color = color;
this.radius = radius;
var arr = [-1,1];
this.direction = arr[~~(Math.random()*2)]*Math.random()/10;
}
set(type,value){
this[type] = value;
}
update(){
this.radius += this.direction;
this.vel.x = (this.pos.x - this.target.x)/drag;
this.vel.y = (this.pos.y - this.target.y)/drag;
if(distance(this.pos.x,this.pos.y,mouse.x,mouse.y) < 50){
this.vel.x += this.vel.x - (this.pos.x - mouse.x)/15;
this.vel.y += this.vel.y - (this.pos.y - mouse.y)/15;
}
if(this.radius >= max_radius){
this.direction *= -1;
}
if(this.radius <= 1){
this.direction *= -1;
}
this.pos.x -= this.vel.x;
this.pos.y -= this.vel.y;
}
draw(){
ctx.beginPath();
ctx.fillStyle = this.color;
ctx.arc(this.pos.x,this.pos.y,this.radius,0,Math.PI*2);
ctx.fill();
}
}
var particles = [];
var colors = [\"#bf1337\",\"#f3f1f3\",\"#084c8d\",\"#f2d108\",\"#efd282\"];
var bool = true;
var current = 0,i;
function changeText(text){
var current = 0,temp,radius,color;
cfill();
ctx.fillStyle = \"#fff\";
ctx.font = \"120px Times\";
ctx.fillText(text,can.width*0.5-ctx.measureText(text).width*0.5,can.height*0.5+60);
var data = ctx.getImageData(0,0,can.width,can.height).data;
cfill();
for(i = 0;i < data.length;i += 8){
temp = {x:(i/4)%can.width,y:~~((i/4)/can.width)};
if(data[i] !== 0 && ~~(Math.random()*5) == 1/*(temp.x % (max_radius+1) === 0 && temp.y % (max_radius+1) === 0)*/){
if(data[i+4] !== 255 || data[i-4] !== 255 || data[i+can.width*4] !== 255 || data[i-can.width*4] !== 255){
if(current < particles.length){
particles[current].set(\"target\",temp);
}else{
radius = max_radius-Math.random()*min_radius;
temp = {x:Math.random()*can.width,y:Math.random()*can.height};
if(bool){
temp = {x:(i/4)%can.width,y:~~((i/4)/can.width)};
}
color = colors[~~(Math.random()*colors.length)];
var p = new Particle(
temp,
{x:(i/4)%can.width,y:~~((i/4)/can.width)},{x:0,y:0},
color,
radius);
particles.push(p);
}
++current;
}
}
}
bool = false;
particles.splice(current,particles.length-current);
}
function draw(){
cfill();
for(i = 0;i < particles.length;++i){
particles[i].update();
particles[i].draw();
}
}
changeText(\"张风捷特烈\");
setInterval(draw,1);</script>
</body>
</html>"
复制代码
6、Idea 集成PHP环境
工欲善其事必先利其器,总不能在文本编辑器里写代码吧,IDE 本人首选Idea
6.1.先装插件
6.2.配置php环境
然后就开心的敲代码了,提示什么的都有
作者:张风捷特烈
链接:https://juejin.im/post/5c8a19d75188257dd56e7d91
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
THE END