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!

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

报告相同问题?

悬赏问题

  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面