PHP学习第二天

  • 案例  将文本文件中内容呈现到表格中

<?php
//1.读取文本内容即字符串
$contents = file('names.txt');
//var_dump($contents);
//2.将字符串以特定的格式解析
foreach ($contents as $items) {
	$cols = explode('|', $items);
    //var_dump($cols);
    $data[] = $cols;
    //var_dump($data);

}
//3.将解析的内容以HTML混编的形式呈现到网页中
?>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>全部信息人员表</title>
</head>
<body>
	<table>
	<thead>
		<tr>
			<th>编号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>邮箱</th>
            <th>网址</th>
		</tr>
	</thead>
	<tbody>
		<?php foreach ($data as $lines): ?>
			<tr>
				<?php foreach ($lines as $col): ?>
					<!-- 判断这里数据有没有一个网址 -->
					<?php $col = trim($col) ?>
					<?php if (strpos($col, 'http://') === 0) : ?>
					<td><a href="<?php echo strtolower($col) ?>"><?php echo substr($col, 8) ?></a></td>
					<?php else: ?>	
					<td><?php echo $col; ?></td>					
					<?php endif ?>
				<?php endforeach ?>
			</tr>
		<?php endforeach ?>
	</tbody>
	</table>
</body>
</html>
  • 获取字符串长度

如果获取的是阿丁文的长度,则直接用下面的例子:

<?php

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

$str = 'hello';
// 获取字符串长度

echo strlen($str);

但是想要获取宽字符的长度,则需要通过PHP扩展实现

  • 配置PHP扩展的步骤

1、在PHP安装目录中去创建一个php.ini

2、修改php.ini中的extension_dir的文件路径

3、修改文件中的部分选项

4、在Apache配置目录中声明php.ini的所在目录

上述步骤完成后,可以通过下面的代码获取到宽字符的长度:

<?php
echo mb_strlen('你好');

 

  • 数组处理

在php中有两种数组,分别是索引数组和关联数组,下面以关联数组举例

$dict = array(
  'hello' => '你好',
  'hello1' => '你好',
  'hello2' => '你好',
);

判断数组中有没有指定的键

下面的类似于javaScript的方式虽然可以达到效果,但会有警告,不利于用户体验

<?php

$dict = array('Hello' =>'你好' ,'Hello1' =>'你好','Hello2' =>'你好');
if ($dict['foo']) {
	echo $dict['foo'];
}else{
	echo "没有";
}
  • 只有当只有当 php.ini 中 display_errors = On 时候,才会在界面上显示notice的错误
  • 开发阶段一定设置On,生产阶段一定设置Off

所以在php中用isset吞掉Undefined index警告,代码如下:

<?php

$dict = array('Hello' =>'你好' ,'Hello1' =>'你好','Hello2' =>'你好');
if (isset($dict['foo'])) {
	echo $dict['foo'];
}else{
	echo "没有";
}


在php中empty相当于

!isset($dict['foo']) || $dict['foo'] == false

 

  • 时间处理

获取当前的时间

<?php
date_default_timezone_set('PRC');
echo date('Y-m-d H:i:s',time());

对已有时间格式化

<?php


$str = '2017-10-22 15:18:58';
$timestamp = strtotime($str);

echo date('Y年m月d日 H:i:s',$timestamp);

 

  • 常量

常量是一个定义后就不能修改的量,一般程序的配置信息(不会在运行过程中修改)都会在常量中定义

下面是常量的定义规则

<?php
define('SYSTEM_NAME','阿里百秀');
echo SYSTEM_NAME;

 

  • 载入其他文件

php中引入其他php文件的方法有以下4种:

4种方式对比

 requirerequire_onceincludeinclude_once
被载入文件如果不存在是否影响继续运行YYNN
多次调用是否会重复执行被载入的文件YNYN

总的来说:require和include两种,区别在于require会因为文件不存在而影响程序的运行,而include不会

XXX和XXX_once,区别在于XXX多次调用会重复执行被载入的文件,而XXX_once只会执行一次

使用层面:

  • include一般用于载入公共文件,这个文件是否存在不影响后面程序的运行
  • require用于载入不可缺失的文件
  • 至于是否采用载入一次(once)的文件取决于文件本身

表单处理(第一部分)

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>用户登录</title>
</head>
<body>
  <!--
    1. 必须有 form 标签
    2. form 必须指定 action 和 method
        不设置 action 默认是当前页面 (必须设置,因为兼容)
        不设置 method 默认是 get
    3. 表单元素(表单域)必须有 name (如果希望被提交的情况)
    4. 表单中必须有一个提交按钮
  -->
  <form action="11-foo.php" method="post">
    <table border="1">
      <tr>
        <td>用户名</td>
        <td><input type="text" name="username"></td>
      </tr>
      <tr>
        <td>密码</td>
        <td><input type="text" name="password"></td>
      </tr>
      <tr>
        <td></td>
        <!-- input: submit image -->
        <!-- button -->
        <td><button>登录</button></td>
      </tr>
    </table>
  </form>
</body>
</html>

 

 

 

转载于:https://my.oschina.net/u/3876057/blog/1824155

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值