php笔记

php操作数和标记

echo tag

You can use the short echo tag to .
It’s always enabled in PHP 5.4.0 and later, and is equivalent to

  ?>
  echo 'print this string'; ?>

echo与print的区别

echo和print都能输出内容,这两个看起来很相似,因为它们都是将一些值打印在屏幕上。

echo用来输出字符串,显示多个值的时候可以用逗号隔开。

print不仅可以打印字符串值,而且可以打印函数的返回值。

print()函数自身的返回值总是1。

数据类型

弱类型转换规则

  • 强制类型转化的语法
    (要转化的类型)当前值
    (bool)“0”

  • 字符串转化为数字类

去除字符串开头的空白符,检测字符串中的数字部分,抛弃非数字部分,如果不包括数字部分,则转化结果为0

  • 字符串转化为boolean值
    采用字符串转化为数字的方式,如果结果大于0,则为true,如果结果等于0,则为false
    因此,"0"会被转化为false

  • 空数组

var_dump([] == flalse) //true

** 递增/递减运算符不影响布尔值。递减 NULL 值也没有效果,但是递增 NULL 的结果是 1。 **

常量定义

PHP中一些典型的常量

  • PHP_EOL

PHP_EOL 是一个些已经定义好的变量,代表php的换行符,这个变量会根据平台而变,在windows下会是/r/n,在linux下是/n,在mac下是/r.

  • DIRECTORY_SEPARATOR

目录分隔符, window路径分隔符为,linux为/.

  • 魔术常量(编译时替换为具体的内容)

__LINE__//当前行
__FILE__//文件全路径 
__DIR__//目录

如对于类 \App\Repositories\ExportTaskRepositoryEloquent的方getList,其相关的常量为:

__NAMESPACE__ //命名空间 App\Repositories\
__CLASS__ //类名称 ExportTaskRepositoryEloquent
__FUNCTION__ // getExportTaskList
__METHOD__ //类方法  App\Repositories\ExportTaskRepositoryEloquent::getList

define 和 const都可以用来定义常量,一下是二者的主要区别.

define 和 const区别

As of PHP 5.3 there are two ways to define constants: Either using the const keyword or using the define() function:

const FOO = 'BAR';
define('FOO', 'BAR');

The fundamental difference between those two ways is that const defines constants at compile time, whereas define defines them at run time. This causes most of const’s disadvantages. Some disadvantages of const are:

  • const cannot be used to conditionally define constants. To define a global constant, it has to be used in the outermost scope:
//会报语法错误
if (...) {
    const FOO = 'BAR';    // invalid
}
// but
if (...) {
    define('FOO', 'BAR'); // valid
}

Why would you want to do that anyways? One common application is to check whether the constant is already defined:

if (!defined('FOO')) {
    define('FOO', 'BAR');
}
  • const accepts a static scalar (number, string or other constant like true, false, null, __FILE__), whereas define() takes any expression. Since PHP 5.6 constant expressions are allowed in const as well
const BIT_5 = 1 << 5;    // valid since PHP 5.6, invalid previously

define('BIT_5', 1 << 5); // always valid
  • const takes a plain constant name, whereas define() accepts any expression as name. This allows to do things like this:
for ($i = 0; $i < 32; ++$i) {
    define('BIT_' . $i, 1 << $i);
}
  • consts are always case sensitive, whereas define() allows you to define case insensitive constants by passing true as the third argument:
define('FOO', 'BAR', true);
echo FOO; // BAR
echo foo; // BAR

So, that was the bad side of things. Now let’s look at the reason why I personally always use const unless one of the above situations occurs:

  • const simply reads nicer. It’s a language construct instead of a function and also is consistent with how you define constants in classes.

  • const defines a constant in the current namespace, while define() has to be passed the full

namespace name:
namespace A\B\C;
// To define the constant A\B\C\FOO:
const FOO = 'BAR';

define('A\\B\\C\\FOO', 'BAR');

Since PHP 5.6 const constants can also be arrays, while define() does not support arrays yet. However arrays will be supported for both cases in PHP 7.

const FOO = [1, 2, 3];    // valid in PHP 5.6
define('FOO', [1, 2, 3]); // invalid in PHP 5.6, valid in PHP 7.0
  • As consts are language constructs and defined at compile time they are a bit faster than define()s.

It is well known that PHP define()s are slow when using a large number of constants. People have even invented things like apc_load_constants() and hidef to get around this.

hidef是一个用于提高define性能的php扩展.

consts make the definition of constants approximately twice as fast (on development machines with XDebug turned on even more). Lookup time on the other hand does not change (as both constant types share the same lookup table)

$_SERVER 变量

以下是一个请求的url实例,
http://zhidao.baidu.com/question/469321857.html?push=core&group=1#pagePart
具体可分解为:

{schema}://{host}:{port}{path}?{queryString}#{hashLocation}

  • {schema},请求的协议,如http,https,ftp等,均为不同的通信协议.类似于约定张三和李四沟通时是用英语还是汉语.

  • {host},目标主机名或目标主机ip(ipv4)地址,相当于张三给李四打电话,需要知道李四的电话号码.host或ip地址就是消息送达的目标机器.ip地址相当于学生的学号,但是通常不太好记.因此后来有了host主机名,通过dns协议,有可用通过主机名找到ip地址,存为唯一确定目标机器.dsn协议就相当于一张记录着域名和ip对应关系的表.实例中,主机名为zhidao.baidu.com

  • {port},端口号,我们电脑上运行这各种这样的软件服务,聊天有微信,qq,查看网页有浏览器,发邮件有outlook等,可以说,可以说经常会有大量来自不同应用的消息同时涌入我们的电脑.那么,我们的电脑是如何知道哪些消息是要qq去接收,哪些消息要浏览器处理,哪些消息需要通过邮件系统查收呢? 这就引发了端口的产生.端口是一种虚拟的编号,带有统一端口号标记的信息,用于被同一个服务接受.如浏览器的默认行为为:捕获经过网卡的带有端口80和端口443标识的消息.而QQ则默认捕获消息中带有端口4000标识的消息. 示例中,我们并未有看到主机名称,这又是什么原因呢?原来,浏览器默认的端口号就是80端口.如果为声明端口号,浏览器最终发消息的时候会给消息标记目标端口号为80端口.

  • {path},是请求的路径,一个服务通过不同色的路径,对外提供不同类型的服务.如/order/insert为用户提供新增订单功能,/order/query为用户提供订单详情查询的功能,/order/update为用户提供订单修改功能./order/cancel为用户提供订单取消功能.path通常有服务端服务自行实行.通过形形色色的path,为用户提供各种各样的功能,这事web工程师日常的主要工作.

  • {path}部分也是可选的,如果没有,一般会走服务端自行设置的默认路径.

  • {queryString},是请求参数部分,通过?与请求路径区分开来,如order/update,你总得告诉服务端你要更新的订单号吧?不然服务端怎么知道具体处理那一单呢?所以有order/update?id=111&data=2019-02-13可以告诉服务端,将id为111的订单的日期改签为2019-02-13.可以看出,多个参数通过&分割开来,一组参数的参数名称和值是通过=分隔开来.如id=111表示id为111.{queryString}部分是可选的,如果不声明,则表明没有只要知道除请求路径以外的更多信息了.

以上{schema},{host},{port},{queryString}这些部分均为一次请求中要要处理的内容,如经过浏览器一次请求后,一篇上好的博客文章呈现在了读者面前.但是由于内容太长,读者写记录想自己看到的章节,下次直接从该章节看起,怎么办呢?这时候,{hashLocation}就派上用场了,通过hashLocation后标记的内容,浏览器就知道将滚动条调整到什么为值,以为用户展示需要的内容. 可以看出,{hashLocation}部分是纯粹的浏览器理解行为.与通过协议http,https无关…

让我们再次回顾下一个完整的请求地址的各个组织部分,整体结果如下:

 {schema}://{host}:{port}{path}?{queryString}#{hashLocation}

说完了浏览器的一次请求路径的大致组成部分,我们进入主题讨论下php中与web服务器交互用到的超全局变量$_SERVER.顾名思义,该全局变量主要记录了请求服务端相关的一些基本信息.


$_SERVER['HTTPS']   //表示请求是否属于https请求
$_SERVER['HTTP_HOST']  //请求的主机名称或主机ip
$_SERVER['REQUEST_URI'] //为{path}?{queryString}两部分组成
$_SERVER["REMOTE_ADDR"];// 如何取得客户端的ip

以下是$_SERVER变量的一个应用实例,

结合利用header()函数实现页面跳转

if($_SERVER['HTTPS'] !="on" )
{
	$redirect= "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
	header("Location:$redirect");
}
?>

你可能感兴趣的:(php,php笔记,const和define区别,define和const区别)