dongxianji0968 2015-03-17 14:31
浏览 50
已采纳

简单的PHP加密程序

I'm not really sure why this simple PHP script is not working,

My browser won't load the page. I'm thinking that its a flaw in the logic rather than syntax, but perhaps someone here would be kind enough to point out what/why I am going wrong.

<html>
    <head>
        <title>My Encryption Program</title>
    </head>
    <body>
    <?PHP
    $ConvertedLetter ="";
    $SecretMessage= "Kiss My Shiny Metal...";
    $MessageLength = strlen($SecretMessage);
    $Counter = 0;
    For($Counter;$MessageLength;$Counter++){
        $LetterToEncrypt = substr($SecretMessage,$Counter,1);
        $AsciiNumber = ord($LetterToEncrypt) + 3;
        $ConvertedLetter = $ConvertedLetter + Chr($AsciiNumber);
    }
    echo $ConvertedLetter;
    ?>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • dousi9215 2015-03-17 14:37
    关注

    This should work for you:

    <?php
    //^^^ good practice in lowercase
    
        $ConvertedLetter ="";
        $SecretMessage= "Kiss My Shiny Metal...";
        $MessageLength = strlen($SecretMessage);
    
        for($Counter = 0; $Counter < $MessageLength; $Counter++) {
      //^   ^^^^^^^^^^^^  ^^^^^^^^^^ You need a condition for a for loop
      //|   | Initialize the variable
      //| good practice control structure in lowercase
    
            $LetterToEncrypt = $SecretMessage[$Counter];
                             //^^^^^^^^^^^^^^^^^^^^^^^^ You can access a string like an array
            $AsciiNumber = ord($LetterToEncrypt) + 3;
            $ConvertedLetter .= chr($AsciiNumber);
                           //^^ ^^^ wrote the function name in the same case as it is defined
                           //| Append the string
        }
    
        echo $ConvertedLetter;
    
    ?>
    

    Output:

    Nlvv#P|#Vklq|#Phwdo111
    

    For more information see:

    Side Notes:

    Add error reporting at the top of your file(s) only while staging, NOT in production:

    <?php
        ini_set("display_errors", 1);
        error_reporting(E_ALL);
    ?>
    

    Also before you make the mistake:

    Variables are case-sensitive, functions not (But it's still good practice to write them in the same case as they are defined)!


    And a few references which may help you in the future to solve such problems yourself or at least get an answer faster (Hover over it!vvv)!

    google Google is your best friend! (He will never lie to you, trust me :D)
    php manual Always a good start to search for something
    How to ask This will help you to get an answer fast!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?