dongzhuange2625 2016-06-05 20:46
浏览 26
已采纳

PHP- 不起作用

when i try

echo "anything 
 whatever";

the result :

anything

whatever

But when i try it from user input it doesn't work

test.php

<html>
    <body>
    <h1>Authentication v 0.04</h1>
    <form action="" method="GET">
     Login&nbsp;<br/>
     <input type="text" name="username" /><br/><br/>
     <input type="submit" value="connect" /><br/><br/>
    </form>
<fieldset><legend>Authentication log</legend><pre>

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo $_GET['username'];


?>
</pre></fieldset></body></html>

if $_GET['username'] = "anything whatever"

the result is

"anything 
 whatever"

it treats as string !!!

why ?

  • 写回答

2条回答 默认 最新

  • douyuqing_12345 2016-06-05 20:54
    关注

    is an escape sequence which represents an end of line [1]. An escape sequence only makes sense on PHP interpolation (i.e. double quotes).

    In HTML has no special meaning so it is printed as a string. [2]

    If there is an actual new line, you may want to use the nl2br function (http://php.net/manual/en/function.nl2br.php) to automatically convert any to a <br> tag.

    If you want to convert any literal to <br>, you may want to use a str_replace function (http://php.net/manual/en/function.str-replace.php) for example like this:

    echo str_replace ('
    ', '<br>', $_GET['username']);
    

    Notice the single quotes to avoid further interpolation.

    [1] In fact, it is a new line only on *nix systems, while it's different on Microsoft and old Mac OSx. You may want to take a look at PHP_EOLconstant (http://php.net/manual/en/reserved.constants.php).

    [2] In fact, it is printed as a new line in the HTML source, which is a text file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 STM32无法向设备写入固件
  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并