當前位置:工程項目OA系統(tǒng) > 泛普各地 > 江西OA系統(tǒng) > 南昌OA系統(tǒng) > 南昌網(wǎng)站建設(shè)公司
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函數(shù),但這有一個更快的辦法。看下面的例子
<?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.
大約是說:能過像數(shù)組一樣,用isset經(jīng)過下標的方法來挪用他,假如返回true則證實他有如許的長度。
筆者注:豈非這個技巧就是為了證實字符串也可以像數(shù)組一樣挪用?假如是如許的話,那還不如寫成如許,讓人看到更清楚:
<?php
$a = "abcdefg";
echo $a[3];
//輸入d
?>
2. Use PHP Echo like a Function
像函數(shù)一樣運用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 輸出字符串和變量都用銜接符(.)把他們銜接起來。但下面這個例子你可以像挪用函數(shù)一樣用","(逗號)的方法像傳參數(shù)一樣挪用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
盡能夠的運用單引號(這個我想人人應(yīng)概都曉得)
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解析字符傳變量。他不只速度快,更多的是對順序員友愛,易讀。
別的在運用數(shù)組的字符傳索引時也要用單引號,來由同上
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
在表單表中運用數(shù)組,特殊是在用多選框時(老調(diào)牙的器械,不翻譯了,估量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嘛,生成靜態(tài)都需求用到。也不翻譯了。
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. :)
南昌網(wǎng)站建設(shè)
- 1新手投資域名怎樣少投資多掙錢
- 2魅妝網(wǎng)店建設(shè)測試報告
- 3評判ERP系統(tǒng)為優(yōu)的幾個參考標準
- 4搜索引擎變化對優(yōu)化戰(zhàn)略的影響
- 5生產(chǎn)管理軟件之企業(yè)信息化是結(jié)果,ERP是手段!
- 6每個人的體質(zhì)不同,因而所排出的汗也不同
- 7資料的整理過程實質(zhì)上是資料的辨析過程
- 8SEOer的發(fā)展
- 9網(wǎng)站建設(shè)公司歸納“九大”行動竅門
- 10學(xué)會查看并修正自己的網(wǎng)站
- 11域名網(wǎng)網(wǎng)站優(yōu)化優(yōu)勢
- 12寒性體質(zhì)的人泡腳不能過度
- 13東莞網(wǎng)絡(luò)營銷公司揭秘曾經(jīng)互聯(lián)網(wǎng)草根牛人
- 14寶雞西安網(wǎng)站建設(shè)分享-如何做最有效果的外鏈
- 15PHP中常犯的幾個MySQL錯誤
- 16生活服務(wù)類網(wǎng)站為何流量大漲?
- 17不能因為有了好用的熱水器就毫無節(jié)制的洗澡
- 18php6個你可能不知道的技巧
- 19PHP程序員易忽略的PHP精華和技巧
- 20晉城博遠建材網(wǎng)溫馨提示:謹慎駕駛安全出行
- 21PHP 6將實現(xiàn)的8個特性和功能
- 22關(guān)于虛擬機管理的五個關(guān)鍵性問題
- 23請慎用PHP的ip2long有bug
- 24國內(nèi)虛擬主機中“南電信北網(wǎng)通”互通的方法
- 25PHP集成開發(fā)工具(IDE)的比較
- 26淺談?wù)鹃L應(yīng)該如何選擇好域名
- 27數(shù)據(jù)服務(wù)器版與管理平臺版的區(qū)別
- 28觀看沙盤模型成了購房必不可少的步驟
- 29企業(yè)如何讓ERP系統(tǒng)更加安全
- 30SEO創(chuàng)業(yè)路有多難
成都公司:成都市成華區(qū)建設(shè)南路160號1層9號
重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務(wù)大廈18樓