2016-10-01 27 views
0

目標:創建一個將字符串轉換爲相應數字的函數。對於例如ABCDEFGHIJ將被轉換到1234567890當用於MySQL查詢時,禁止PHP函數在前端響應結果

功能我已經寫:

function decrypt_code($data1){ 

$someArray=array($data1[0],$data1[1],$data1[2],$data1[3],$data1[4],$data1[5],$data1[6],$data1[7],$data1[8],$data1[9],$data1[10],$data1[11],$data1[12],$data1[13]); // size 7 
foreach($someArray as $value1){ 
    if($value1 == "A"){$dc1= "1";} 
    elseif($value1 == "B"){$dc2= "2";} 
    elseif($value1 == "C"){$dc3= "3";} 
    elseif($value1 == "D"){$dc4= "4";} 
    elseif($value1 == "E"){$dc5= "5";} 
    elseif($value1 == "F"){$dc6= "6";} 
    elseif($value1 == "G"){$dc7= "7";} 
    elseif($value1 == "H"){$dc8= "8";} 
    elseif($value1 == "I"){$dc9= "9";} 
    elseif($value1 == "J"){$dc10= "0";} 

} 
return $dc = $dc1.$dc2.$dc3.$dc4.$dc5.$dc6.$dc7.$dc8.$dc9.$dc10; 
} 

問題:我已經包含在頭一個函數庫。現在每次我打電話給該功能時,它都會打印解密後的數字。我只是想在需要時將它們存儲回&。當我使用函數獲取解密的數字並在SQL命令中運行它時,這也會產生問題。這是發生在標題和函數打印在網站的標題部分的數字,因爲我使用該功能在標題部分運行MySQL命令。

注意:我不想知道如何編寫函數,因爲我已經實現了。我只是不希望函數在我使用它運行MySQL查詢時回顯結果。

+0

放入一個變量並返回變量 – RiggsFolly

+0

@RiggsFolly能否詳細說明一下?我是PHP的新手。 –

+0

@RiggsFolly更新了代碼。你的意思是我的更新方式? –

回答

2

您必須保留子解密部分的功能並返回整個解密字符串。這個例子適用於超過14個符號。而將字符串轉換成數組str_split更好的解決方案

function decrypt_code($str){ 
    $someArray = str_split($str); // size is not bounded 

    $decrypt_code; 
    foreach($someArray as $value1){ 
     if($value1 == "A"){$decrypt_code .= "1";} 
     elseif($value1 == "B"){$decrypt_code .= "2";} 
     elseif($value1 == "C"){$decrypt_code .= "3";} 
     elseif($value1 == "D"){$decrypt_code .= "4";} 
     elseif($value1 == "E"){$decrypt_code .= "5";} 
     elseif($value1 == "F"){$decrypt_code .= "6";} 
     elseif($value1 == "G"){$decrypt_code .= "7";} 
     elseif($value1 == "H"){$decrypt_code .= "8";} 
     elseif($value1 == "I"){$decrypt_code .= "9";} 
     elseif($value1 == "J"){$decrypt_code .= "0";} 
    } 

    return $decrypt_code; 
} 
+0

* hmm *,它似乎像傳遞參數是一個字符串,而不是一個數組。這行'$ someArray = array($ data1 [0],...);'欺騙了我。 *大聲笑* +1爲你,*回答 - 刪除*爲我。 ;-) –

1

當您想要在屏幕上或標記內打印某些內容時(例如屏幕上的文本,HTML頁面頂部圖像或標題的路徑),只能使用回顯。返回以常用函數返回功能即

echo =>節目的函數的最終結果的輸出。

echo()函數輸出一個或多個字符串。

注意:echo()函數實際上並不是一個函數,因此您不需要使用圓括號。但是,如果要將多個參數傳遞給echo(),則使用括號會生成解析錯誤。

return =>返回從函數

返回程序控制返回給調用模塊的值。在被調用模塊調用後的表達式處執行。

如果從函數內部調用,return語句會立即結束當前函數的執行,並返回其參數作爲函數調用的值。 return還會結束eval()語句或腳本文件的執行。

注意:如果未提供參數,則必須省略括號並返回NULL。使用括號調用返回值但不帶參數將導致分析錯誤。

/*Function to return a value*/ 
public function returnName($name){ 

/* See here i'm returning the output of the function*/ 
return "Hello " . $name . " mate!"; 
} 

/*Here i will echo (display) the output of the function onto the screen*/ 
$greeting = returnName("Billy"); 

echo $greeting; 

/*********OUTPUT*********/ 
Hello Billy mate 
/*********OUTPUT*********/ 

一些更明確的例子:

函數的參數

參數是函數名之後指定,在括號內。

<html> 
<body> 

<?php 
    function ourFunction($x) 
    { 
      echo $x . ".<br />"; 
    } 

    $y = "black"; 
    echo "My car color is "; 
    ourFunction("white"); 

    echo "My car color is "; 
    ourFunction ("$y"); 
?> 

</body> 
</html> 

的實施例的結果將是:

My car color is white. 
My car color is black. 

可以通過使用所述參數之間的逗號傳遞多個參數,例如:

function MyFunction($X, $Y, $Z) 
{ 
     code to be executed; 
} 

函數的返回值

有時候如果您使用的是函數,您希望返回一個值。這是return語句進來讓我們來看一個例子:

<html> 
<body> 

<?php 
    function addValues($x,$y) 
    { 
      $total=$x+$y; 
      return $total; 
    } 

    echo "2 + 2 = " . addValues(2,2); 
?> 

</body> 
</html> 

輸出:

4 

這個解釋可以很清楚的回聲的理解和從函數返回的語句。

快樂編碼:)

+0

''黑色''語法錯誤;捲曲的報價。 –

+0

根據您提出的建議@ Fred-ii-更正了代碼。 –

+0

您的兩個示例都不會返回所需的結果 - 方法一將返回一個字符串,最多10個字符長取決於輸入字符串中的字符。方法二 - 你不能在字符串上使用'foreach'。 – Steve

0

儘管說:「我不想知道怎麼寫的功能,因爲我已經實現了」您已發佈的功能不會做什麼你說你需要。

這裏有一個功能有很多評論,將實現你所要求的。

function decrypt_code($code) { 

    // a variable to hold the result 
    $result = ''; 

    // an array representing the encoding 
    $convert = [ 
     'A' => '1', 
     'B' => '2', 
     'C' => '3', 
     'D' => '4', 
     'E' => '5', 
     'F' => '6', 
     'G' => '7', 
     'H' => '8', 
     'I' => '9', 
     'J' => '0', 
    ]; 

    // convert the submitted code to an array of it's chars 
    $code_array = str_split($code); 

    // work through the submitted code converting as we go 
    foreach ($code_array as $char) { 
     // build the result string 
     $result .= $convert[$char]; 

    } 

    // return the final string 
    return $result; 
}