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!

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

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog