Arijit Aich
Arijit Aich

Reputation: 157

Prohibit PHP Function To Echo Results In Front-End, When Using For MySQL Query

Goal: To create a function that converts a string to its corresponding number. For e.g. ABCDEFGHIJ would get converted to 1234567890.

Function I Have Written:

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;
}

Problem: I have included a function library in the header. Now each time i am calling the function, its printing the decrypted numbers. I just want to store them & echo as and when required. This is also creating a problem when i am using the function to get the decrypted numbers and run them in a SQL Command. This is taking place in the header and the function is printing the numbers in the header part of the website because i am using the function to run a MySQL Command in the header part.

NOTE: I do not want to know how to write the function because that i have already achieved. I just do not want the function to echo the result when i am using it for running a MySQL query.

Upvotes: 0

Views: 101

Answers (3)

Steve
Steve

Reputation: 1963

Despite saying "I do not want to know how to write the function because that i have already achieved", the function you have posted will not do what you have said you need.

Here's a function with lots of comments that will achieve what you have asked for.

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;
}

Upvotes: 0

Naresh Kumar P
Naresh Kumar P

Reputation: 4210

You only use echo when you want to print something onto the screen or within the markup i.e text on the screen, path to an image or title on the top of an html page. Return is commonly used in a function to return the output of the function i.e.

echo => shows the final result of a function.

The echo() function outputs one or more strings.

Note: The echo() function is not actually a function, so you are not required to use parentheses with it. However, if you want to pass more than one parameter to echo(), using parentheses will generate a parse error.

return => returns the value from a function

return returns program control to the calling module. Execution resumes at the expression following the called module's invocation.

If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

Note: If no parameter is supplied, then the parentheses must be omitted and NULL will be returned. Calling return with parentheses but with no arguments will result in a parse error.

/*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*********/

Some more clear Examples:

Functions Parameters

Parameters are specified after the function name, inside the parentheses.

<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>

The result of the example will be:

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

You can pass multiple parameters by using a comma between the parameters, for example:

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

Function Return Values

Sometime you want to return a value if you are using functions. This is where the return statement comes in. Let’s look at an example:

<html>
<body>

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

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

</body>
</html>

Output:

4

This Explanation may be clear for understanding of the echo and return statements from a function.

Happy Coding :)

Upvotes: 1

Ramin Darvishov
Ramin Darvishov

Reputation: 1039

You must keep sub decrypted part in function and return whole decrypted string . This example work for more than 14 symbol. And dividing string into array str_split better solution

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;
}

Upvotes: 2

Related Questions