php switch 语法用于执行基于不同条件不同的行动。
-------------------------------------------------- ------------------------------
开关语句的PHP
使用switch语句来选择代码的许多组成部分之一被执行。
语法
switch (n)
{
case label1:
code to be executed if n=label1;
break;
case label2:
code to be executed if n=label2;
break;
default:
code to be executed if n is different from both label1 and label2;
}
这是它的工作原理:首先,我们有一个单一表达式n(常常是一个变量),只计算一次。该表达式的值,然后比较与结构中每个案件的价值观。如果有匹配,与该案件相关的代码就会被执行。利用休息时间,避免因进入下一个代码自动执行。默认语句用于如果没有找到匹配。
例如
<html>
<body>
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
switch (expression)
{
case label1:
code to be executed if expression = label1;
break;
case label2:
code to be executed if expression = label2;
break;
default:
code to be executed
if expression is different
from both label1 and label2;
}
</body>
</html>
工作原理:
对表达式(通常是变量)进行一次计算
把表达式的值与结构中 case 的值进行比较
如果存在匹配,则执行与 case 关联的代码
代码执行后,break 语句阻止代码跳入下一个 case 中继续执行
如果没有 case 为真,则使用 default 语句
<?php
switch ($x)
{
case 1:
echo "Number 1";
break;
case 2:
echo "Number 2";
break;
case 3:
echo "Number 3";
break;
default:
echo "No number between 1 and 3";
}
?>
</body>
</html>
php loop continue执行的代码块中指定的次数,或在指定的条件为真。
-------------------------------------------------- ------------------------------
对于循环
循环使用的是当你事先知道多少次的脚本应该运行。
语法
for (init; condition; increment)
{
code to be executed;
}
参数:
初始化:主要用于设置一个计数器(但可以是任何代码执行在一次循环的开始)
条件:评估每个循环迭代。如果值为TRUE,则循环继续。如果值为FALSE,则循环结束。
增量:主要用于增加一个计数器(但可以是任何代码将在循环结束时执行)
注:以上可以是空的,或者有多个表达式每个参数(以逗号分隔)。
例如
下面的例子定义了一个循环,开始与i = 1。循环将继续运行,因为我只要小于或等于5。我将增加1每次循环运行:
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br />";
}
?>
</body>
</html>
The number is 1
The number is 2
The number is 3
The number is 4
The number is 5
实例二
<?php
while (list($key, $value) = each($arr)) {
if (!($key % 2)) { // skip odd members
continue;
}
do_something_odd($value);
}
$i = 0;
while ($i++ < 5) {
echo "Outer<br />n";
while (1) {
echo " Middle<br />n";
while (1) {
echo " Inner<br />n";
continue 3;
}
echo "This never gets output.<br />n";
}
echo "Neither does this.<br />n";
}
?>
<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
print "$in";
}
?>
例外是用来改变脚本的正常流动,如果指定的错误
-------------------------------------------------- ------------------------------
什么是异常
在PHP 5中来到一个新对象的处理错误导向的方式。
异常处理是用来改变代码的执行,如果指定的错误(例外的正常流动)条件发生。这种情况称为例外。
这就是通常发生异常时触发:
当前状态保存代码
该代码的执行将切换到预定的(自定义)异常处理函数
根据这一情况,处理便可恢复已保存的代码由国家执行,终止或继续执行脚本,从代码中的不同位置的脚本
我们将显示不同的错误处理方法:
基本使用异常
创建自定义的异常处理程序
多例外
重新抛出异常
设置顶层异常处理
注意:例外只应使用错误条件,而不应被用来在指定的跳点代码中的另一个地方。
-------------------------------------------------- ------------------------------
例外的基本用法
当抛出异常,下面的代码也不会执行,而PHP将试图找到匹配“catch”块。
如果一个异常没有被捕获,一个致命的错误会发出一个“未捕获的异常”消息。
让我们试着扔不抓出异常:
<?php
//create function with an exception
function checkNum($number)
{
if($number>1)
{
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception
checkNum(2);
?>
Fatal error: Uncaught exception 'Exception'
with message 'Value must be 1 or below' in C:webfoldertest.php:6
Stack trace: #0 C:webfoldertest.php(12):
checkNum(28) #1 {main} thrown in C:webfoldertest.php on line 6
Try, throw and catch
为了避免从上面的例子中的错误,我们需要创造适当的代码来处理异常。
适当的异常代码应包括:
尝试 - 函数使用一个例外,应在一个“试”块。如果异常不会触发的代码将继续正常。但是,如果异常触发,一个异常“抛出”
扔 - 这是如何触发异常。每一个“扔”必须有至少一个“捕获”
捕获 - 阿“catch”块检索例外,创建一个对象,包含异常信息
让我们试着触发一个有效的代码除外
<?php
//create function with an exception
function checkNum($number)
{
if($number>1)
{
throw new Exception("Value must be 1 or below");
}
return true;
}
//trigger exception in a "try" block
try
{
checkNum(2);
//If the exception is thrown, this text will not be shown
echo 'If you see this, the number is 1 or below';
}
//catch exception
catch(Exception $e)
{
echo 'Message: ' .$e->getMessage();
}
?>
PHP stripos
stripos() 函数返回字符串在另一个字符串中第一次出现的位置。与strpos(),stripos()不区分大小写
string 必需。规定被搜索的字符串。
find 必需。规定要查找的字符。
start 可选。规定开始搜索的位置。
<?php
$findme = 'a';
$mystring1 = 'xyz';
$mystring2 = 'ABC';
$pos1 = stripos($mystring1, $findme);
$pos2 = stripos($mystring2, $findme);
// Nope, 'a' is certainly not in 'xyz'
if ($pos1 === false) {
echo "The string '$findme' was not found in the string '$mystring1'";
}
// Note our use of ===. Simply == would not work as expected
// because the position of 'a' is the 0th (first) character.
if ($pos2 !== false) {
echo "We found '$findme' in '$mystring2' at position $pos2";
}
?>
strpos() - 查找的字符串的第一个出现的位置
strrpos() - 查找字符串中的字符的最后出现的位置
strrchr() - 查找字符串中的字符最后一次出现
substr() - 返回字符串的一部分
stristr() - 忽略大小写strstr
strstr() - 查找第一次出现的字符串
strripos() - 找出案件的最后出现的位置在一个字符串不敏感的字符串
str_ireplace() - 病例的str_replace区分版本。
php strtolower字母转换小写
串用strtolower(string $str )
返回字符串转换为小写的所有字母字符。
请注意,'字母'是当前语言环境决定的。这意味着,即如元音变音默认的“C”语言环境,字符甲(一)将不转换
$str输入字符串。报告错误返回值返回小写的字符串。
<?php
$str = "Mary Had A Little Lamb and She LOVED It So";
$str = strtolower($str);
echo $str; // Prints mary had a little lamb and she loved it so
?>
UTF 8使用mb_convert_case
exampel
<?php
$string = "А";
$string = mb_convert_case($string, MB_CASE_LOWER, "UTF-8");
echo $string;
//output is: а
?>