當前位置:首頁 » 編程語言 » php代碼php代碼

php代碼php代碼

發布時間: 2023-11-30 01:48:54

❶ 高質量php代碼的50個技巧(3)

42

43

44

45

/**

Method to execute a command in the terminal

Uses :

1. system

2. passthru

3. exec

4. shell_exec

*/

function terminal($command)

{

//system

if(function_exists('system'))

{

ob_start();

system($command , $return_var);

$output = ob_get_contents();

ob_end_clean();

}

//passthru

else if(function_exists('passthru'))

{

ob_start();

passthru($command , $return_var);

$output = ob_get_contents();

ob_end_clean();

}

//exec

else if(function_exists('基悔exec'))

{

exec($command , $output , $return_var);

$output = implode(" " , $output);

}

//shell_exec

else if(function_exists('shell_exec'))

{

$output = shell_exec($command) ;

}

else

{

$output = 'Command execution not possible on this system';

$return_var = 1;

}

return array('output' => $output , 'status' => $return_var);

}

terminal('ls');

上面的函數將運行shell命令, 只要有一個系統函數可用, 這保持了代碼的一致性.

5. 靈活編寫函數

?

1

2

3

4

5

6

function add_to_cart($item_id , $qty)

{

$_SESSION['cart']['item_id'] = $qty;

}

add_to_cart( 'IPHONE3' , 2 );

使用上面的函數添加單個項目. 而當添加項列表的時候,你要創建另一個函數嗎? 不用, 只要稍加留意不同類型的參數, 就寬好會更靈活. 如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

慎鋒鉛14

15

16

function add_to_cart($item_id , $qty)

{

if(!is_array($item_id))

{

$_SESSION['cart']['item_id'] = $qty;

}

else

{

foreach($item_id as $i_id => $qty)

{

$_SESSION['cart']['i_id'] = $qty;

}

}

}

add_to_cart( 'IPHONE3' , 2 );

add_to_cart( array('IPHONE3' => 2 , 'IPAD' => 5) );

現在, 同個函數可以處理不同類型的輸入參數了. 可以參照上面的例子重構你的多處代碼, 使其更智能.

6. 有意忽略php關閉標簽

我很想知道為什麼這么多關於php建議的博客文章都沒提到這點.

?

1

2

3

<?php

echo "Hello";

//Now dont close this tag

這將節約你很多時間. 我們舉個例子:

一個 super_class.php 文件

?

1

2

3

4

5

6

7

8

9

<?php

class super_class

{

function super_function()

{

//super code

}

}

?>

//super extra character after the closing tag

index.php

?

1

2

require_once('super_class.php');

//echo an image or pdf , or set the cookies or session data

這樣, 你將會得到一個 Headers already send error. 為什麼? 因為 “super extra character” 已經被輸出了. 現在你得開始調試啦. 這會花費大量時間尋找 super extra 的位置。因此, 養成省略關閉符的習慣:

?

1

2

3

4

5

6

7

8

9

<?php

class super_class

{

function super_function()

{

//super code

}

}

//No closing tag

這會更好.

7. 在某地方收集所有輸入, 一次輸出給瀏覽器

這稱為輸出緩沖, 假如說你已在不同的函數輸出內容:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

function print_header()

{

echo "<p id='header'>Site Log and Login links</p>";

}

function print_footer()

{

echo "<p id='footer'>Site was made by me</p>";

}

print_header();

for($i = 0 ; $i < 100; $i++)

{

echo "I is : $i ';

}

print_footer();

替代方案, 在某地方集中收集輸出. 你可以存儲在函數的局部變數中, 也可以使用ob_start和ob_end_clean. 如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

function print_header()

{

$o = "<p id='header'>Site Log and Login links</p>";

return $o;

}

function print_footer()

{

$o = "<p id='footer'>Site was made by me</p>";

return $o;

}

echo print_header();

for($i = 0 ; $i < 100; $i++)

{

echo "I is : $i ';

}

echo print_footer();

為什麼需要輸出緩沖:

>>可以在發送給瀏覽器前更改輸出. 如 str_replaces 函數或可能是 preg_replaces 或添加些監控/調試的html內容.

>>輸出給瀏覽器的同時又做php的處理很糟糕. 你應該看到過有些站點的側邊欄或中間出現錯誤信息. 知道為什麼會發生嗎? 因為處理和輸出混合了.

8. 發送正確的mime類型頭信息, 如果輸出非html內容的話.

輸出一些xml.

?

1

2

3

4

5

6

$xml = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';

$xml = "<response>

<code>0</code>

</response>";

//Send xml data

echo $xml;

工作得不錯. 但需要一些改進.

?

1

2

3

4

5

6

7

$xml = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';

$xml = "<response>

<code>0</code>

❷ PHP源碼到底是什麼

PHP,是英文超級文本預處理語言Hypertext Preprocessor的縮寫。PHP 是一種 HTML 內嵌式的語言,是一種在伺服器
端執行的嵌入HTML文檔的腳本語言,語言的風格有類似於C語言,被廣泛的運用。PHP源碼指的使用PHP開發的實例,沒有經過二次封裝,能夠直接進行二
次開發的程序,PHP簡單易學,如果你想學網站開發,PHP是一個不錯的選擇,因會PHP跟其它語言相對有一定的優勢:
1、PHP是開放的源代碼:所有的PHP源代碼事實上都可以得到。
2、PHP是免費的。和其它技術相比,PHP本身免費。
3、php的快捷性,程序開發快,運行快,技術本身學習快。嵌入於HTML:因為PHP可以被嵌入於HTML語言,它相對於其他語言,編輯簡單,實用性強,更適合初學者。
4、跨平台性強:由於PHP是運行在伺服器端的腳本,可以運行在UNIX、LINUX、WINDOWS下。
5、效率高:PHP消耗相當少的系統資源。
6、圖像處理:用PHP動態創建圖像
7、面向對象:在php5 中,面向對象方面都有了很大的改進,現在php完全可以用來開發大型商業程序。
8、專業專註:

❸ php代碼執行順序

php代碼理論上是從上到下的執行順序,但是也不是你這樣理解!

他最終的輸出內容,是根據你寫的php代碼的邏輯進行判斷輸出的!

比如:

$i=10;
if($i==20){
echo'您好!'
}else{
echo'太好了';
}

這段代碼執行順序確實是從上到下,但並不是說, echo '您好' 在echo '太好了' 的上面,那就一定會 輸出 「您好」這兩個字,

而是你看的邏輯

首先 $i 等於10,

所以 if ( $i == 20 )這個條件是不成立的

所以最終輸出的結果是 」太好了「3個字!


php所謂的代碼從上到下的執行順序,那是對於php伺服器端而言, 最終你在終端看到的結果, 是以代碼的邏輯思維為准!

❹ php 代碼如何調試

開啟Apache伺服器以及MY_SQL資料庫

在PHP集成環境的安裝目錄「www」文件夾中創建我們需要用到的php文件,1.php.

用Notepad++打開1.php文件,然後輸入PHP的語言格式:
<?php ?>

輸入標準的輸出語言:
<?php
echo "Hello world !" //輸出Hello world 語言。 echo 是列印的意思
?>

在網頁上輸入localhost:8080/1.php可以調試我們自己寫的php代碼

PHP的簡單運算,用$來定義應用數據,輸出也是一樣。
<?php
$sum=1;
$total=1.22;
$sum=$total+$sum;
echo $sum ?>

熱點內容
給定一個演算法 發布:2024-05-19 17:50:08 瀏覽:863
戀愛生物種離線緩存 發布:2024-05-19 17:49:15 瀏覽:578
卡巴斯基伺服器如何連接外網更新 發布:2024-05-19 17:42:06 瀏覽:559
手機虛榮怎麼連接伺服器 發布:2024-05-19 17:41:47 瀏覽:729
linux修改保存文件 發布:2024-05-19 17:30:38 瀏覽:665
網路有你腳本 發布:2024-05-19 17:29:55 瀏覽:769
黎明我的世界伺服器 發布:2024-05-19 17:17:34 瀏覽:538
雷神g50如何設置安卓原生模式 發布:2024-05-19 16:50:04 瀏覽:120
c語言小數四捨五入 發布:2024-05-19 16:23:28 瀏覽:525
資料庫被注入攻擊 發布:2024-05-19 16:21:31 瀏覽:835