Global namespace  //看不懂看下面的中文 中英结合看看

When using namespaces, you may find that internal functions(内部(内置)函数) are hidden by functions you wrote. To fix this, refer to the global function by using a backslash before the function name.

<?php
namespace phptherightway; function fopen()
{
$file = \fopen(); // Our function name is the same as an internal function.
// Execute the function from the global space by adding '\'.
} function array()
{
$iterator = new \ArrayIterator(); // ArrayIterator is an internal class. Using its name without a backslash
// will attempt to resolve it within your namespace.
}

global space (全局空间):

如果没有定义任何命名空间,所有的类与函数的定义都是在全局空间,与 PHP 引入命名空间概念前一样。在名称前加上前缀 \ 表示该名称是全局空间中的名称,即使该名称位于其它的命名空间中时也是如此。

<?php
namespace A\B\C; /* 这个函数是 A\B\C\fopen */
function fopen() {
/* ... */
$f = \fopen(...); // 调用全局的fopen函数
return $f;
}
?>

 

Strings

Concatenation

  • If your line extends beyond the recommended line length (120 characters), consider concatenating your line
  • For readability it’s best to use concatenation operators over concatenating assignment operators
  • While within the original scope of the variable, indent when concatenation uses a new line
<?php
$a = 'Multi-line example'; // concatenating assignment operator (.=)
$a .= "\n";
$a .= 'of what not to do'; vs. $a = 'Multi-line example' // concatenation operator (.)
. "\n" // indenting new lines
. 'of what to do';

 

String types

String types are a constant feature within the PHP community, but hopefully this section will explain the differences between the string types and their benefits/uses.

Single quotes

Single quotes are the simplest way to define a string and are often the quickest. Their speed stems from PHP not parsing the string (doesn’t parse for variables). They’re best suited for:

  • Strings that do not need to be parsed
  • Writing of a variable into plain text
<?php
echo 'This is my string, look at how pretty it is.'; // no need to parse a simple string /**
* Output:
*
* This is my string, look at how pretty it is.
*/

  

Double quotes

Double quotes are the Swiss army knife of strings, but are slower due to the string being parsed. They’re best suited for:

  • Escaped strings
  • Strings with multiple variables and plain text
  • Condensing multi-line concatenation, and improving readability
<?php
echo 'phptherightway is ' . $adjective . '.' // a single quotes example that uses multiple concatenating for
. "\n" // variables and escaped string
. 'I love learning' . $code . '!'; vs. echo "phptherightway is $adjective.\n I love learning $code!" // Instead of multiple concatenating, double quotes
// enables us to use a parsable string

  While using double quotes that contain variables, it’s often the case that the variable will be touching another character. This will result in PHP not parsing the variable due to the variable being camouflaged. To fix this problem, wrap the variable within a pair of curly brackets.

<?php
$juice = 'plum';
echo "I drank some juice made of $juices"; // $juice cannot be parsed vs. $juice = 'plum';
echo "I drank some juice made of {$juice}s"; // $juice will be parsed /**
* Complex variables will also be parsed within curly brackets
*/ $juice = array('apple', 'orange', 'plum');
echo "I drank some juice made of {$juice[1]}s"; // $juice[1] will be parsed

  

Nowdoc syntax

Heredoc syntax

见PHP手册。

Ternary operators(三元运算操作符)

Ternary operators are a great way to condense code, but are often used in excess. While ternary operators can be stacked/nested, it is advised to use one per line for readability.

<?php
$a = 5;
echo ($a == 5) ? 'yay' : 'nay'; vs. // nested ternary
$b = 10;
echo ($a) ? ($a == 5) ? 'yay' : 'nay' : ($b == 10) ? 'excessive' : ':('; // excess nesting, sacrificing readability

  

To ‘return’ a value with ternary operators use the correct syntax.

<?php
$a = 5;
echo ($a == 5) ? return true : return false; // this example will output an error vs. $a = 5;
return ($a == 5) ? 'yay' : 'nope'; // this example will return 'yay'

  

Variable declarations

<?php
$about = 'A very long string of text'; // uses 2MB memory
echo $about; vs. echo 'A very long string of text'; // uses 1MB memory

  

 

PHP基础知识(二)的更多相关文章

  1. java 基础知识二 基本类型与运算符

    java  基础知识二 基本类型与运算符 1.标识符 定义:为类.方法.变量起的名称 由大小写字母.数字.下划线(_)和美元符号($)组成,同时不能以数字开头 2.关键字 java语言保留特殊含义或者 ...

  2. 菜鸟脱壳之脱壳的基础知识(二) ——DUMP的原理

    菜鸟脱壳之脱壳的基础知识(二)——DUMP的原理当外壳的执行完毕后,会跳到原来的程序的入口点,即Entry Point,也可以称作OEP!当一般加密强度不是很大的壳,会在壳的末尾有一个大的跨段,跳向O ...

  3. Dapper基础知识二

    在下刚毕业工作,之前实习有用到Dapper?这几天新项目想用上Dapper,在下比较菜鸟,这块只是个人对Dapper的一种总结. 2,如何使用Dapper?     首先Dapper是支持多种数据库的 ...

  4. python基础知识(二)

    python基础知识(二) 字符串格式化 ​ 格式: % 类型 ---- > ' %类型 ' %(数据) %s 字符串 ​ print(' %s is boy'%('tom')) ----> ...

  5. Java基础知识二次学习--第三章 面向对象

    第三章 面向对象   时间:2017年4月24日17:51:37~2017年4月25日13:52:34 章节:03章_01节 03章_02节 视频长度:30:11 + 21:44 内容:面向对象设计思 ...

  6. Java基础知识二次学习-- 第一章 java基础

    基础知识有时候感觉时间长似乎有点生疏,正好这几天有时间有机会,就决定重新做一轮二次学习,挑重避轻 回过头来重新整理基础知识,能收获到之前不少遗漏的,所以这一次就称作查漏补缺吧!废话不多说,开始! 第一 ...

  7. 快速掌握JavaScript面试基础知识(二)

    译者按: 总结了大量JavaScript基本知识点,很有用! 原文: The Definitive JavaScript Handbook for your next developer interv ...

  8. Java基础知识二次学习--第六章 常用类

    第六章 常用类   时间:2017年4月26日16:14:49~2017年4月26日16:56:02 章节:06章_01节~06章_06节 视频长度:20:57+1:15+8:44+1:26+11:2 ...

  9. java接口自动化基础知识(二)

    二.HttpClient+testNG实现对接口的测试及校验 在上面第一篇中已经实现了基础配置和测试用例数据准备,本篇文章将以登录举例进行测试执行. 这是之前login接口的代码 @Test(grou ...

  10. ThinkPHP框架基础知识二

    一.空操作和空控制器处理 空操作:没有指定的操作方法:空控制器:没有指定控制器,例如: http://网址/index.php/Home/Main/login  正常 http://网址/index. ...

随机推荐

  1. 《深入理解Android2》读书笔记(七)

    接上篇<深入理解Android2>读书笔记(六) 广播接受者 注册 ContextImpl @Override public Intent registerReceiver(Broadca ...

  2. CF1020B Badge 【模拟链表】

    n个点(n<=1000) 接下来n个整数表示ai 第i个数ai表示i到ai有一条边 输出: n个数 表示从第i个点出发,最先被访问两次的点 样例1: 从1 出发,先到达2,2会到达3,3又到达2 ...

  3. python3 2017.3.19

    今天弄了一个晚上没弄出来一个小东西,只弄出来了写追加,而且还是笨方法,起码死不掉那种. global log 127.0.0.1 local2 daemon maxconn 256 log 127.0 ...

  4. Python3 文件基本修改替换

    现有原文件: Somehow, it seems the love I knew was always the most destructive kind 不知为何,我经历的爱情总是最具毁灭性的的那种 ...

  5. Hydra 8.4/8.5新增功能

    Hydra 8.4/8.5新增功能   Kali Linux 2017.1自带的Hydra为8.3,现在Hydra升级到8.5,新增以下功能.   (1)为输出文件选项-o,添加一个配套选项-b,允许 ...

  6. [HDU3756]Dome of Circus

    题目大意: 在一个立体的空间内有n个点(x,y,z),满足z>=0. 现在要你放一个体积尽量小的圆锥,把这些点都包住. 求圆锥的高和底面半径. 思路: 因为圆锥里面是对称的,因此问题很容易可以转 ...

  7. 1.4(SQL学习笔记)分组、子查询、联结、组合查询

    一.分组 建表及数据填充语句下载:链接: https://pan.baidu.com/s/1WHYafwqKJEKq1kDwCH_Zlg 提取码: 3wy4 1.1初识分组 分组是按照某一列,将该列中 ...

  8. ConstraintLayout导读

    ConstraintLayout是Android Studio 2.2中主要的新增功能之一,也是Google在去年的I/O大会上重点宣传的一个功能,可以把ConstraintLayout看成是一个更高 ...

  9. golang slice 切片原理

    golang 中的 slice 非常强大,让数组操作非常方便高效.在开发中不定长度表示的数组全部都是 slice .但是很多同学对 slice 的模糊认识,造成认为golang中的数组是引用类型,结果 ...

  10. 安卓安装ZXING(二维码)SDK

    安卓安装ZXING(二维码)SDK 安装<WIFI共享精灵>后,ZXING(二维码)SDK就有了,扫二维码又快而且精度又高. 真是意外的发现.不需要去下载BARCODE SCANNER.