点击“前端自学社区”查看更多精彩
跟读者说的一些话
身为前端程序员,我们工作中每天做的事情最频繁的就是,写界面,然后根据后端的api来实现接口的处理。根据UI设计稿完成网站的设计。其实前端的领域很多,不同的领域又需要学习对应的框架,不断的踩坑,来熟练的使用框架实现网站的建设。
前端接触多了,你可能也会好奇后端怎么写api接口,它们怎么把数据封装好传递给你的?我们一直做的是接口的接收处理,不了解如何制作接口?
想要开始学习制作网站api或者app api,那么面临着就是选择 后端语言 ?
我前段时间做过一个调研: 前端程序员该如何选择后端语言?
热门后端语言如下:
Java
PHP
Node.js
Python
Go
.NET
….
至于选择哪门语言学习,取决于自己的需求, 2020 前端程序员Node.js 已成必须,不管之前有没有了解,现在必须学习了。
至于为什么选择PHP,因为它在Web领域已经战斗很多年了,社区生态已经很完善,而且是弱类型语言,前端上手会容易点。在这,就不讨论其它后端语言了。那么,让我们一起开始PHP之旅吧。
PHP 之旅 开始
环境搭建
wampserve 下载
IDE
VScode PhpStorm
虚拟主机
安装好虚拟环境,就可以开发PHP 了, 在开始一个新的项目时,项目文件都放在www 目录下进行访问,它会默认访问 index.php 。
当创建了新的项目 Test 文件夹/ index.php , 那么访问时,就得通过 http://localhost/test/ 进行访问,
有种方便得访问方法, 建立虚拟主机, 点击 wanp -----> your VirtualHosts -------> VirtualHosts 建立虚拟主机
进行绑定, 1.访问站点名称 2.访问路径 3. 建立虚拟主机 4.重启服务器 5. 直接通过 站点名称进行访问 (test.cms)
基础语法
访问外部变量
<?php
$name = 'HELLO';
// 第二种 传递参数
function getName($test){
// echo $GLOBALS['name']; 第一种访问方法 $GLOBALS
echo $test;
// $GLOBALS $name 第三种
}
getName($name)
?>
超全局变量可以全局访问
$GLOBALS
$_SERVER
$_GET
$_POST
$_FILES
$_COOKIE
$_SESSION
$_REQUEST
$_ENV
常用函数
var_dump()函数用于输出变量的相关信息。
isset(变量) 检测变量是否被定义
strlen(变量) 变量的长度
mb_srtlen(变量, 字体类型) 变量的长度
strtolower() 转小写
strtoupper() 转大写
ucwords() 每个首字母变大写
ucfirst() 首字母大写
explode('拆分格式',变量) 字符串拆分
implode(':',$name); 字符串合并
mb_substr(变量,开始索引, 结束索引,-数位末尾) 字符串截取
static 静态变量 持久保存变量
<?php
function getAge(){
// echo $GLOBALS['name'];
// static 会持久保存 该变量得值 . 第一次执行完, 它得值是 23, 第二次调用时,它会在23 得基础进行运算
static $age = 22;
$age = $age + 1;
return $age;
}
echo getAge(); //23
echo getAge(); //24
?>
常量定义 defin const
define('NAME','小米');
const job = '是一家互联网企业';
echo NAME.job
小结
变量在PHP 中定义了,想在局部访问,可以通过以下三种方法进行访问
GLOBALS[′name′]
- 函数传入参数
- GLOBALS $name
常量 定义两种方法:
const
define
常量定义后,可以直接在局部使用
== 与 === 区别
== 表示 统一类型进行比较
=== 表示类型和值一样才能相等
?? 与 ?:与别
?? 检测变量 1.值不能为空 2. 变量是否为空
echo var_dump(objs∗=∗null;echovardump(objs*??*'没有值') //没有值
?: 三元表达式
include 加载模块 require 强加载
//include 文件
if( !@include 'test.html') {
include 'default.html';
}
//require: 加载文件 和 必须的内库
require('demo.html') 如果demo.html 不存在,直接报错,截止执行下面程序
函数模块
函数传值 and 传址 区别
传址 :传递的地址,
传值:传递的参数变值
$age = 22;
//传址
*function* getAge(*&*$age) {
*++*$age;
echo $age;
}
getAge($age); *//23*
echo $age; *//23*
//传值
*function* getAges($age){
*++*$age;
echo $age;
}
getAge($age); *//23*
echo $age; *//22*
函数参数解构赋值
function getSum(...$args){
return array_sum($args);
}
echo getSum(1,2,3,4,5);
函数默认参数
function getInfo($names = '漳卅') {
echo $names;
}
getInfo();
函数参数指定类型 严格模式
// 使用严格模式后, 参数必须传递指定参数的类型值
declare(strict_types = 1);
function getMessage(int $age,String $name){
echo '年龄为'.$age.'。姓名为:'.$name;
}
getMessage(22,'张三');
函数返回值约束
function get\_userName(): String {
return '老王来了';
}
function get\_password(): int {
return 111111;
}
// 返回值 为 null 或者 String 时,使用 ?String
function get\_Email(): ?String {
return null;
}
// 没有返回值 使用 void
function get\_Sex(): void {
echo '无返回值使用';
}
数组玩法
基本数组和关联数组
// 关联数组
$arr = ['name'=> '张三','age' => 22];
//基本数组
$arrs = [1,2,3,4];
数组遍历方法 list foreach
$arr = [
['name'=> '张三','age' => 22],
['name'=> '小红','age' => 12],
];
//list 遍历
while(list('name' => $name,'age' => $age) = current($arr)) {
echo "name:{$name},age:{$age}"."<hr/>";
next($arr);
}
//foreach
foreach($arr as $key => $item) {
echo $item['name']."<hr/>";
}
数组常用方法
- array_shift() 移除数组顶层数据
- array_unshift() 向数组顶层添加数据
- array_push() 向数组尾部添加元素
- array_pop() 移除数组元素
- array_values() 取数组元素值
- count() 统计数量
- array_map(function(item){},数组变量) 数组遍历 可以对数组做修改
arrMap = array\_map(function(<span class="katex-html" aria-hidden="true" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="strut" style="height:1em;vertical-align:-0.25em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span class="mord mathit" style="margin-right:0.10903em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">M<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">p¨<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E61<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span class="mord mathit" style="margin-right:0.02778em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">r<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span class="mord mathit" style="margin-right:0.03588em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">y¨<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E95<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">m<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">p(<span class="mord mathit" style="margin-right:0.10764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">f<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">u<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">n<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">c<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">o<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">n(items){
return ¨
E
91
E
′
n
a
m
e
′
¨
E
93
E
;¨
E
125
E
,<annotation encoding="application style="font-size: inherit;line-height: inherit;color: rgb(153, 204, 153);overflow-wrap: inherit !important;word-break: inherit !important;" span="" encoding=""application"><span class="katex-html" aria-hidden="true" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="strut" style="height:0.946332em;vertical-align:-0.19444em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">i<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">t<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">m<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">s¨<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E91<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E<span class="vlist" style="height:0.751892em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="top:-3.063em;margin-right:0.05em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="pstrut" style="height:2.7em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="sizing reset-size6 size3 mtight" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="mord mtight" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="mord mtight" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">′<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">n<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">a<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">m<span class="mord mathit" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">e<span class="vlist" style="height:0.751892em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span style="top:-3.063em;margin-right:0.05em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="pstrut" style="height:2.7em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="sizing reset-size6 size3 mtight" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="mord mtight" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;"><span class="mord mtight" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">′¨<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E93<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E;<span class="mspace" style="margin-right:0.16666666666666666em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">¨<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E125<span class="mord mathit" style="margin-right:0.05764em;" style="font-size: inherit;color: inherit;line-height: inherit;overflow-wrap: inherit !important;word-break: inherit !important;">E,arr);
</span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mspace" style="margin-right:0.16666666666666666em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mtight"></span class="mord mtight"></span class="sizing reset-size6 size3 mtight"></span class="pstrut" style="height:2.7em;"></span style="top:-3.063em;margin-right:0.05em;"></span class="vlist" style="height:0.751892em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mtight"></span class="mord mtight"></span class="sizing reset-size6 size3 mtight"></span class="pstrut" style="height:2.7em;"></span style="top:-3.063em;margin-right:0.05em;"></span class="vlist" style="height:0.751892em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="strut" style="height:0.946332em;vertical-align:-0.19444em;"></span class="katex-html" aria-hidden="true"></annotation encoding="application></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.10764em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.03588em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit" style="margin-right:0.05764em;"></span class="mord mathit"></span class="mord mathit"></span class="mord mathit" style="margin-right:0.10903em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit" style="margin-right:0.02778em;"></span class="mord mathit"></span class="strut" style="height:1em;vertical-align:-0.25em;"></span class="katex-html" aria-hidden="true">
时间
<?php
// 时间
// 设置中国时区
date_default_timezone_set('PRC');
// 时间转时间戳
// 1583843402 数据库存储使用,可以存储ISO格式
$sjc = strtotime('2020-3-10 12:30:02');
echo $sjc."<hr/>";
// 将时间戳转换为ISO格式时间 2020-03-10 12:30:02
echo date('Y-m-d H:i:s',$sjc)."<hr/>";
// 获取当前时间戳
$nows = strtotime('NOW');
echo $nows."<hr/>";
echo date('Y-m-d H:i:s',$nows)."<hr/>";
// 截止到期时间: 当前时间到 增加的时间进行比对
//2020-03-09 16:03:24 -------- 2021-03-09 16:03:24
echo date('Y-m-d H:i:s',strtotime("1year"))."<hr/>"; // 加了一年
// DateTime()类的 使用
$nowTime = new DateTime(); // 实例化DateTime 对象是获取的当前时间
// 把当前时间转换为时间戳 format('U')
echo $nowTime -> format('U')."<hr/>";
echo date('Y-m-d H:i:s')."<hr/>";
// 时间的增加 和减少, 应用业务类似 VIP会员到期时间
// 在某个时间段增加这么多天 用 DateInterval()
//天用D隔开 时用T隔开 M分
$times = new DateTime();
$times-> format('Y-m-d H:i:s');
$interval = new DateInterval('P10DT10H10M'); //增加10天10小时10分
$times-> add($interval);
echo $times -> format('Y-m-d H:i:s').'增加时间'."<hr/>";
$times-> sub($interval);
echo $times -> format('Y-m-d H:i:s').'减少时间'."<hr/>";
以上是PHP7的 基础语法,通过本章的学习,可以对PHP有大体的认识。本章有的语法没有介绍到,学习过其他语言的话,很快会上手的。
更新详细的语法 可以去PHP 官方获取:https://www.php.net/manual/zh/
——前端扫地僧
ES6_11_Module 的语法(import, export,export default )
ES6系列文章已经在公众号更新完
有加入一起学习吗