php6个你可能不知道的技巧
翻译的老外的,自己e文不太好,能够有些翻译禁绝。
Over the years I've come across some useful tricks in PHP that are not obvious, but are worth mentioning. This is not meant to be a comprehensive list of all the useful tricks that you can do with PHP.
这些废话就免了。
1. Count Characters in a String
To do this, I've usually just used the function strlen(). However, there is a faster method. Take a look at the following code example:
要做到这一点,我凡间运用strlen函数,但这有一个更快的办法。看下面的例子
<?php
$string = 'testing';
if(isset($string[6]))
echo "The string '$string' is at least 7 characters long.";
else
echo "The string '$string' is less than 7 characters long.";
You treat the $string value like an array by passing an integer value to isset(). If that number plus one is greater than or equal to the number of characters in the string, it will return true. You need to add one because it is zero based.
大约是说:能过像数组一样,用isset经过下标的方法来挪用他,假如返回true则证实他有如许的长度。
笔者注:岂非这个技巧就是为了证实字符串也可以像数组一样挪用?假如是如许的话,那还不如写成如许,让人看到更清楚:
<?php
$a = "abcdefg";
echo $a[3];
//输入d
?>
2. Use PHP Echo like a Function
像函数一样运用echo
I've always thought that if you wanted to concatenate strings with echo, you needed to use periods. But you can actually treat echo like a function and use commas instead (it is also faster). Take a look at the following code:
普通运用echo 输出字符串和变量都用衔接符(.)把他们衔接起来。但下面这个例子你可以像挪用函数一样用","(逗号)的方法像传参数一样挪用echo
<?php
$string1 = 'test-string1';
$string2 = 'test-string2';
$string3 = 'test-string3';
echo 'String #1: ', $string1, '
';
echo 'String #2: ', $string2, '
';
echo 'String #3: ', $string3, '
';
3. Use Single Quotes When Possible
尽能够的运用单引号(这个我想人人应概都晓得)
By using single quotes instead of double quotes, you save PHP from having to parse your string for variables. It not only is faster, but I find it more programmer-friendly because it is easier to find variables in your code.
Also, when referencing an array that has a string index, always use single quotes. This prevents PHP from having to figure out exactly what you were trying to say.
运用单引号替代双引号,可以削减php解析字符传变量。他不只速度快,更多的是对顺序员友爱,易读。
别的在运用数组的字符传索引时也要用单引号,来由同上
4. PHP Variable Variables
There have been several cases when I needed to access a dynamic variable (where the variable name changed). You can do this easily in PHP by using what is called Variable Variables. Take a look at this example:
不太清楚他想表达的是什么。可变变量的长处照样缺陷?
<?php
$var1 = 'nameOfVariable';
$nameOfVariable = 'This is the value I want!!!';
echo $$var1;
5. Use Arrays in Form Fields
在表单表中运用数组,特殊是在用多选框时(老调牙的器械,不翻译了,估量phper都晓得)
Not only can you create a form field that creates an element in an array (like name['firstname'] ), but you can create dynamic arrays as well. This is especially useful in checkboxes, where the user could check multiple options. Take a look at this HTML:
When the above fields are posted to a PHP page, each hobby is added to the hobbies array. You can then loop through that array and access each value that was checked.
6. PHP Output Buffering
运用php的缓冲输出,ob_start和ob_end嘛,生成静态都需求用到。也不翻译了。
I have come across cases where something was being output to the screen that I didn't want (at least at that point, or maybe not at all).
A common example of this is if you have a function or a script that echoes out a string, but you are using the code in that instance where you don't want it to print to the screen at that point (when using a template system or framework system, for example). In this case you would still want to be able to reuse the code, but just prevent anything from being printed out at that exact point.
This will make more sense if you look at this simple example:
<?php
ob_start();
echo 'Print to the screen!!!';
$getContent = ob_get_contents();
ob_end_clean();
// Do whatever you want...
// Do something with the printed content (only if you want)...
echo 'Now: ' . $getContent;
You probably were familiar with at least some of these, but I hope that someone will find this list useful in some capacity. :)
南昌网站建设
- 1新手投资域名怎样少投资多挣钱
- 2魅妆网店建设测试报告
- 3评判ERP系统为优的几个参考标准
- 4搜索引擎变化对优化战略的影响
- 5生产管理软件之企业信息化是结果,ERP是手段!
- 6每个人的体质不同,因而所排出的汗也不同
- 7资料的整理过程实质上是资料的辨析过程
- 8SEOer的发展
- 9网站建设公司归纳“九大”行动窍门
- 10学会查看并修正自己的网站
- 11域名网网站优化优势
- 12寒性体质的人泡脚不能过度
- 13东莞网络营销公司揭秘曾经互联网草根牛人
- 14宝鸡西安网站建设分享-如何做最有效果的外链
- 15PHP中常犯的几个MySQL错误
- 16生活服务类网站为何流量大涨?
- 17不能因为有了好用的热水器就毫无节制的洗澡
- 18php6个你可能不知道的技巧
- 19PHP程序员易忽略的PHP精华和技巧
- 20晋城博远建材网温馨提示:谨慎驾驶安全出行
- 21PHP 6将实现的8个特性和功能
- 22关于虚拟机管理的五个关键性问题
- 23请慎用PHP的ip2long有bug
- 24国内虚拟主机中“南电信北网通”互通的方法
- 25PHP集成开发工具(IDE)的比较
- 26浅谈站长应该如何选择好域名
- 27数据服务器版与管理平台版的区别
- 28观看沙盘模型成了购房必不可少的步骤
- 29企业如何让ERP系统更加安全
- 30SEO创业路有多难