The Basics

Comparison operators

Comparison operators are an often overlooked aspect of PHP, which can lead to many unexpected outcomes. One such problem stems from strict comparisons (the comparison of booleans as integers).

<?php
$a = 5; // 5 as an integer var_dump($a == 5); // compare value; return true
var_dump($a == '5'); // compare value (ignore type); return true
var_dump($a === 5); // compare type/value (integer vs. integer); return true
var_dump($a === '5'); // compare type/value (integer vs. string); return false /**
* Strict comparisons
*/
if (strpos('testing', 'test')) { // 'test' is found at position 0, which is interpreted as the boolean 'false'
// code...
} vs. if (strpos('testing', 'test') !== false) { // true, as strict comparison was made (0 !== false)
// code...
}

Conditional statements

If statements

While using ‘if/else’ statements within a function or class, there is a common misconception that ‘else’ must be used in conjunction to declare potential outcomes. However if the outcome is to define the return value, ‘else’ is not necessary as ‘return’ will end the function, causing ‘else’ to become moot.

<?php
function test($a)
{
if ($a) {
return true;
} else {
return false;
}
} vs. function test($a)
{
if ($a) {
return true;
}
return false; // else is not necessary
}

  

Switch statements

Switch statements are a great way to avoid typing endless if’s and elseif’s, but there are a few things to be aware of:

  • Switch statements only compare values, and not the type (equivalent to ‘==’)
  • They Iterate case by case until a match is found. If no match is found, then the default is used (if defined)
  • Without a ‘break’, they will continue to implement each case until reaching a break/return
  • Within a function, using ‘return’ alleviates the need for ‘break’ as it ends the function
<?php
$answer = test(2); // the code from both 'case 2' and 'case 3' will be implemented function test($a)
{
switch ($a) {
case 1:
// code...
break; // break is used to end the switch statement
case 2:
// code... // with no break, comparison will continue to 'case 3'
case 3:
// code...
return $result; // within a function, 'return' will end the function
default:
// code...
return $error;
}
} 如果在一个函数中调用 return 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。return 也会终止 eval() 语句或者脚本文件的执行。
Note: 注意既然 return 是语言结构而不是函数,因此其参数没有必要用括号将其括起来。通常都不用括号,实际上也应该不用,这样可以降低 PHP 的负担。

 

 

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

  1. .NET面试题系列[1] - .NET框架基础知识(1)

    很明显,CLS是CTS的一个子集,而且是最小的子集. - 张子阳 .NET框架基础知识(1) 参考资料: http://www.tracefact.net/CLR-and-Framework/DotN ...

  2. RabbitMQ基础知识

    RabbitMQ基础知识 一.背景 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然 ...

  3. Java基础知识(壹)

    写在前面的话 这篇博客,是很早之前自己的学习Java基础知识的,所记录的内容,仅仅是当时学习的一个总结随笔.现在分享出来,希望能帮助大家,如有不足的,希望大家支出. 后续会继续分享基础知识手记.希望能 ...

  4. selenium自动化基础知识

    什么是自动化测试? 自动化测试分为:功能自动化和性能自动化 功能自动化即使用计算机通过编码的方式来替代手工测试,完成一些重复性比较高的测试,解放测试人员的测试压力.同时,如果系统有不份模块更改后,只要 ...

  5. [SQL] SQL 基础知识梳理(一)- 数据库与 SQL

    SQL 基础知识梳理(一)- 数据库与 SQL [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5902856.html 目录 What's 数据库 ...

  6. [SQL] SQL 基础知识梳理(二) - 查询基础

    SQL 基础知识梳理(二) - 查询基础 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5904824.html 序 这是<SQL 基础知识梳理( ...

  7. [SQL] SQL 基础知识梳理(三) - 聚合和排序

    SQL 基础知识梳理(三) - 聚合和排序 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5926689.html 序 这是<SQL 基础知识梳理 ...

  8. [SQL] SQL 基础知识梳理(四) - 数据更新

    SQL 基础知识梳理(四) - 数据更新 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5929786.html 序 这是<SQL 基础知识梳理( ...

  9. [SQL] SQL 基础知识梳理(五) - 复杂查询

    SQL 基础知识梳理(五) - 复杂查询 [博主]反骨仔 [原文]http://www.cnblogs.com/liqingwen/p/5939796.html 序 这是<SQL 基础知识梳理( ...

  10. APP测试入门篇之APP基础知识(001)

    前言        最近两月比较多的事情混杂在一起,静不下心来写点东西,月初想发表一遍接口测试的总结,或者APP测试相关的内容,一晃就月底了,总结提炼一时半会也整不完.放几个早年总结内部培训PPT出来 ...

随机推荐

  1. 【剑指offer】面试题 55. 二叉树的深度

    面试题 55. 二叉树的深度 题目一:二叉树的深度 题目描述:输入一棵二叉树,求该树的深度.从根结点到叶结点依次经过的结点(含根.叶结点)形成树的一条路径,最长路径的长度为树的深度. Java 实现 ...

  2. Linux下python版本的升级步骤

    1.先下载,你要升级的python版本(我升级的是python3.3.0) 可使用系统自带下载工具wget下载: wget http://www.python.org/ftp/python/3.3.0 ...

  3. 数据库SQL归纳(一)

    SQL功能分类 SQL 功能 动 词 数据定义 DDL CREATE.ALTER.DROP 数据查询 DQL SELECT 数据更改 DML INSERT.UPDATE.DELETE 数据控制 DCL ...

  4. [SimpleOJ239]Cards

    题目大意: 有n(n为偶数)张牌,每张牌正反面有两张数字,你可以从中选出n/2张牌,减去某一面的数字,再选出另外n/2张牌,加上某一面的数字,问最终的答案最小能是多少? 思路: 先不考虑n/2的限制, ...

  5. [转] Mybatis 示例之 SelectKey

    SelectKey在Mybatis中是为了解决Insert数据时不支持主键自动生成的问题,他可以很随意的设置生成主键的方式. 不管SelectKey有多好,尽量不要遇到这种情况吧,毕竟很麻烦. sel ...

  6. 手Q游戏中心上线 完美释放娱乐基因

        今年A股市场上手游概念股的表现可谓“独当一面”,不少和手游沾边的公司股价都翻了倍.在笔者看来,这些手游企业的股价明显高得离谱,这轮行情可以证明资本市场对手游的关注度非常高,但并不意味着这些手游 ...

  7. JavaScript 时间与日期处理实战:你肯定被坑过

    本部分的知识图谱请参考编程语言知识图谱-时间与日期. 本文JavaScript 时间与日期处理实战:你肯定被坑过从属于笔者的Web 前端入门与最佳实践中 JavaScript 入门与最佳实践系列文章. ...

  8. 从connect到express02-中间件morgan

    控制台输出请求日志 输出格式 默认格式: dev, combined, tiny等 自定义格式: morgan(':method :url :status :res[content-length] - ...

  9. tomcat配置优化

    tomcat服务管理页面 http://192.168.1.249:8080/manager/status 找到下面的内容 "http-nio-8081"  (此处端口是根据自己实 ...

  10. C#读取Windows日志

    管理-->事件查看器     可以查看[应用程序].[安全].[系统]等分类的日志 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 2 ...