PHP-Web服务端-03-基础学习

配置当天虚拟主机

在D:\Apache\Apache24\conf\extra路径下的httpd-vhosts.conf文件尾添加以下一组内容:
在这里插入图片描述
直接访问IP与回环IP,会返回第一个虚拟主机上的内容

案例–读取文本文件内容呈现到一个表格中
<?php
	// 1. 读取文件内容     // => 包含文本内容的字符串数据
	$contents = file_get_contents('names.txt');
	// 2. 按照一个特定的规则解析文件内容   // => 数组
	// 2.1. 按照换行拆分-->数组-但只有一个元素
	$lines = explode("\n", $contents);
	// 2.2. 遍历每一行分别解析每一行中的数据
	foreach ($lines as $item) {
  		if (!$item) continue;
  		// $item => '70 | 余娜 | 37 | [email protected] | http://HKHEBUI.RO'
  		$cols = explode('|', $item);
 	 	// $cols => []
  		$data[] = $cols;
 		// $data => [ [], [] ]
	}
	// 3. 通过混编的方式将数据呈现到表格中
	// var_dump($data);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>全部人员信息表</title>
</head>
<body>
  <h1>全部人员信息表</h1>
  <table>
    <thead>
      <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>年龄</th>
        <th>邮箱</th>
        <th>网址</th>
      </tr>
    </thead>
    <tbody>
      <?php foreach ($data as $line): ?>
      <tr>
        <?php foreach ($line as $col): ?>
        <!-- $col => ' http://XEP.VC' -->
        <?php $col = trim($col); ?>
        <!-- $col => 'http://XEP.VC' -->
        <!-- 判断这里的数据是不是一个网址(看看是否是 http://) -->
        <?php if (strpos($col, 'http://') === 0): ?>
          <td><a href="<?php echo strtolower($col); ?>"><?php echo substr($col, 7); ?></a></td>
        <?php else: ?>
          <td><?php echo $col; ?></td>
        <?php endif ?>
        <?php endforeach ?>
      </tr>
      <?php endforeach ?>
    </tbody>
  </table>
</body>
</html>

API(Application Programming Interface)–应用程序编程接口

接口:USB接口,网线接口??---》输入/输出(成对出现):提供某种特定能力的事物,需要输入-返回输出
API:应用程序用到的接口

配置 PHP 扩展的步骤

// 1. 在 PHP 的解压目录去复制php.ini-development文件去创建一个 php.ini文件
// 2. 打开php.ini文件搜索--extension_dir
// 3. 添加一行:extension_dir = "D:/Apache-PHP/ext"------extension_dir = "PHP解压目录/ext"
// 4. 将;extension=php_mbstring.dll 解开注释---删掉 " ; " 重启Apache
// 5. 默认Apache加载的php.ini 是去 Windows目录找的--所以找不到当前的php.ini
// 6. 可以通过 Apache 的配置文件添加修改默认加载路径 PHPIniDir到PHP文件夹

在这里插入图片描述

PHP中的REPL环境

用于不经过Apache运行PHP文件

cmd cd 到PHP的解压路径 ⇒ php -a	//进入到php的console模式
Interactive Shell----可交互脚本---REPL(read execute print loop)

在这里插入图片描述

常用字符串API

PHP所有能力都是函数,内置1000多个

		// PHP 中专门为 宽字符集 添加了一套 API
		// 这一套 API 不在内置的 1000+ 里面,而是在一个模块(php_mbstring.dll)中
		// 模块成员必须通过配置文件载入模块过后再使用
		// 所有的API 都是 mb_xxxx
	1.字符串截取
		string substr ( 待截取的字符串 , 截取的起始位置,截取的长度/可不写 )
		// 中文截取
		string mb_substr (  待截取的字符串 , 截取的起始位置,截取的长度/可不写)
	2.字符串长度
		字符串长度获取:
		//strlen 只能获取拉丁文的长度
		int strlen ( $str )
		//获取中文字符串长度
		mixed mb_strlen ( $str )
					mb_strlen('你好');
	3.大小写转换
		string strtolower ( $str )
		string strtoupper ( $str )
	4.去除首尾空白字符
		string trim ( $str )
		string ltrim ( $st )
		string rtrim ( $str )
	5.查找字符串中某些字符首次出现的位置
		mixed strpos ( 待查找字符串 , 查找的字符串 )
		int mb_strpos ( 待查找字符串 , 查找的字符串 )
	6.字符串替换
		mixed str_replace ( 要被替换的字符串 ,用于替换的字符串, 目标总字符串 )
	7.使字符串重复指定次数
		string str_repeat ( 字符串,重复次数 )
	8.字符串分割
		array explode( 分割字符, 待分割字符串 )

常用数组API

// 只有当 php.ini 中 display_errors = On 时候
// 才会在界面上显示 notice 错误
// 开发阶段一定设置为 On 生产阶段(上线)设置为 Off

//字符串==bool	比较时转换为===》字符串==字符串(0/1)??
'0'==false    //==》true	//本应非空为true

	1.获取关联数组中全部的键/值
		array array_keys(关联数组)
		array array_values(关联数组)
	2.判断关联数组中是否存在某个键
		bool array_key_exists(键,数组)
		isset(数组)----可消除警告--isset会吞掉Undefined index的警告
		empty(数组)----也可消除警告--判断是否undefined/未定义或false
			function empty ($input) {
				  return !isset($input) || $input == false
			}

			这种类似于 JavaScript 的方式虽然可以达到效果,但是会有警告
				if ($dict['foo']) {
				  echo $dict['foo'];
				} else {
				  echo '没有';
				}	
			----------------------------没有警告
				if (isset($dict['foo'])) {
					  echo $dict['foo'];
					} else {
					  echo '没有';
					}	
	3.去除重复的元素
		array_unique(数组)
	4.将一个或多个元素追加到数组中
		int array_push(数组,元素)	//返回元素个数
		$arr[] = 'new value'
	5.删除数组中的最后一个元素
		string array_pop()	//返回删除的元素
	6.数组长度
	    int count(数组)
	7.检测存在
	    bool in_array(元素,数组)	//返回值--有-1---没有-空			

常用的时间API

1.时间戳:time()
		从 Unix 纪元(格林威治时间 1970-01-01 00:00:00)到当前时间的**秒数**
2.设置时区--默认是格林威治时间
	1)通过代码	date_default_timezone_set("PRC");	//设为中国时间---推荐
	2)通过配置文件	php.ini---date.timezone===》date.timezone = PRC
3.格式化日期:date()
		获取有格式的当前时间
		格式化一个指定的时间戳
		date("Y-m-d",time());
		------------------------------------------------------
		$str = '2017-10-22 15:18:58';
		// 对已有时间做格式化
		// strtotime 可以用来将一个有格式的时间字符串 转换为一个 时间戳
		$timestamp = strtotime($str);
		// 注意单引号字符串问题
		echo date('Y年m月d日<b\r>H:i:s', $timestamp);

include和require载入文件

类似 CSS 的 "@import url(其它CSS文件路径)" 导入文件
require 'config.php';
require 可以用于在当前脚本中载入一个别的脚本文件并且执行他
require 在每一次调用时都会载入对应的文件,并执行
-------------------------------------------------------------------------
require_once
require_once 如果之前载入过,不再执行(只执行一次)
由于类似于 定义常量 定义函数 这种操作不能执行多次
所以 require_once 更加合适载入这种文件
=========================================================================
require 特点: 一旦被载入的文件不存在就会报一个致命错误,当前文件不再往下执行 

include 特点: 载入文件不存在不会报错误(会有警告,警告不用管),当前文件继续执行(推荐)
	<?php include 'aside.php'; ?>

猜你喜欢

转载自blog.csdn.net/weixin_42966943/article/details/88843789