在php的换行操作中,\n与<br/>的区别

本文通过实例对比了PHP中使用 和&lt;br/&gt;的效果,解释了两者在源码及浏览器显示上的不同,并分析了其原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    博主我今天遇见了一个情况就是在php代码中写入\n换行操作符,以为在浏览器那里呈现的输出是换行,没想到却没有。代码和结果如下所示:

<?php
$name="haha";
echo "hi,$name";
echo "\n";
echo 'hi,$name';
?>

如果将中间的换行操作符改为<br/>,则浏览器呈现出来的结果会换行,如下图所示:


为什么会出现这样子的结果呢?我们来从源码来进行分析,在火狐浏览器那里右击查看页面源代码,我们会发现,上述第一种情况在源码那里会出现分行,如图所示:


而上述第二种情况除了出现一个<br>标签外就没有换行,结果如图所示:


从而可以得出一个结论是:在php中,\n这个换行操作符是对源码进行操作,在浏览器中是无作用的;而<br>这个标签在浏览器中起换行作用,在源码中会出现这个标签,但是在源码中不换行。

<form id="inputForm" action="update.jhtml" method="post" type="ajax" validate-type="validate" style="width: 100%"> <input type="hidden" name="id" value="${followUpRecord.id}" /> <div class="tabContent"> <table class="input input-edit"> <tr> <th> <span class="requiredField">*</span>${message("跟进类型")}: </th> <td colspan="2"> <select class="text followType" name="followType" id="followType" required btn-fun="clear"> <option value="">--请选择--</option> <option value="1" [#if followUpRecord.followType == 1] selected [/#if]>商机跟进</option> <option value="2" [#if followUpRecord.followType == 2] selected [/#if]>客户跟进</option> </select> </td> <th> <span class="requiredField">*</span>${message("跟进对象")}: </th> <td colspan="2"> <span class="search" style="position:relative"> <input type="hidden" id="objId" name="objId" class="text objId" value="${followUpRecord.objId}" btn-fun="clear" required/> <input type="text" id="objName" name="objName" class="text objName" value="${followUpRecord.objName}" onkeyup="clearSelect(this)" required/> <input type="button" class="iconSearch" value="" id="selectObj"> </span> </td> <th class="t_projectStageValue" style="display: none;"> ${message("商机阶段")}: </th> <td class="t_projectStageValue" style="display: none;"> <input type="text" name="projectStageValue" class="text projectStageValue" maxlength="200" value="${followUpRecord.projectStageValue}"/> </td> <th> <span class="requiredField">*</span>${message("状态")}: </th> <td colspan="2"> <select class="text recordStatus" name="recordStatus" required btn-fun="clear" style="pointer-events: none;background-color: #f7f7f7"> <option value="1" [#if followUpRecord.recordStatus == 1] selected [/#if]>未提交</option> <option value="2" [#if followUpRecord.recordStatus == 2] selected [/#if]>已提交</option> <option value="3" [#if followUpRecord.recordStatus == 3] selected [/#if]>审批中</option> </select> </td> </tr> <tr> <th> ${message("标段划分")}: </th> <td> <input type="text" name="sectionDivision" class="text sectionDivision" maxlength="200" /> </td> <th> ${message("跟进阶段")}: </th> <td> <select class="text followUpState" name="followUpState" id="followUpState" required btn-fun="clear"> <option value="">--请选择--</option> [#list followUpStates as followUpState] <option value="${followUpState.value}" [#if followUpRecord.followUpState == followUpState.value] selected [/#if] >${followUpState.remark}</option> [/#list] [#-- <option value="1" [#if followUpRecord.followUpState == 1] selected [/#if]>发现阶段</option>--] [#-- <option value="2" [#if followUpRecord.followUpState == 2] selected [/#if]>客户接触阶段</option>--] [#-- <option value="3" [#if followUpRecord.followUpState == 3] selected [/#if]>需求了解阶段</option>--] [#-- <option value="4" [#if followUpRecord.followUpState == 4] selected [/#if]>评估阶段</option>--] </select> </td> <th> ${message("是否关闭商机")}: </th> <td> <select class="text isCloseNiche" name="isCloseNiche" id="isCloseNiche" required btn-fun="clear"> <option value="">--请选择--</option> <option value="1" [#if followUpRecord.isCloseNiche == 1] selected [/#if]>否</option> <option value="2" [#if followUpRecord.isCloseNiche == 2] selected [/#if]>是</option> </select> </td> </tr> <tr> <th> ${message("项目进度详细描述")}: </th> <td colspan=" 10" style="width: 95%"> <textarea name="projectScheduleDescription" class="text projectScheduleDescription" cols="30" rows="2"/>${followUpRecord.projectScheduleDescription}</textarea> </td> </tr> <tr> <th> ${message("项目运作思路及关键")}: </th> <td colspan="10" style="width: 95%"> <textarea name="ideaPoint" class="text ideaPoint" cols="30" rows="2"/>${followUpRecord.ideaPoint}</textarea> </td> </tr> <tr> <th> ${message("下一步工作计划")}: </th> <td colspan="10" style="width: 95%"> <textarea name="nextPlan" class="text nextPlan" cols="15" rows="2"/>${followUpRecord.nextPlan} </textarea> </td> </tr> <tr> <th> ${message("需公司其他部门支撑事项")}: </th> <td colspan="10" style="width: 95%"> <textarea name="needBackup" class="text needBackup" cols="15" rows="2"/>${followUpRecord.needBackup}</textarea> </td> </tr> <tr> <th> <span class="requiredField">*</span>${message("总结")}: </th> <td colspan="10" style="width: 95%"> <textarea cols="20" rows="2" name="sumUp" class="text sumUp" required/>${followUpRecord.sumUp} </textarea> </td> </tr> <tr> <th>${message("图片")}</th> <td colspan="10" style="width: 95%"> <div class="box-wrap"> <div class="box-main" style="overflow:visible;"> <div class="upload-list tc" style="overflow:initial;width:260px;"> [#list followUpRecord.images as image] <div class="ul-box" style="padding:13px 0px 0px"> <div class="pic" style="margin-left:65px; width:80px"><a href="${image.source}" target="_blank"><img src="${image.thumbnail}"></a></div> <b> </b> <a class="del delProductImage1" href="#"></a> <input type="hidden" name="images[${image_index}].title" value="${image.title}"> <input type="hidden" name="images[${image_index}].source" value="${image.source}"> <input type="hidden" name="images[${image_index}].large" value="${image.large}"> <input type="hidden" name="images[${image_index}].medium" value="${image.medium}"> <input type="hidden" name="images[${image_index}].thumbnail" value="${image.thumbnail}"> </div> [/#list] <a href="javascript:;" class="a-upload" id="addProductImage"></a> </div> </div> </div> </td> </tr> <tr style="display: none" class="t_closeResult"> <th> ${message("商机关闭原因")}: </th> <td colspan=" 10" style="width: 95%"> <textarea name="closeResult" class="text closeResult" cols="30" rows="2" required/>${followUpRecord.closeResult}</textarea> </td> </tr> </table> <div class="task-content"> [#include "/crm/follow_up_record/edit/task-form.ftl" /] </div> <br/> <div class="title-style"> 附件上传: <div class="btns"> <a href="javascript:;" id="addAttach" class="button">添加附件</a> </div> </div> <table id="table-attach"></table> <div class="title-style"> 抄送人: <div class="btns"> <a href="javascript:;" id="addMember" class="button">添加抄送人</a> </div> </div> <table id="table-addMember"></table> <div class="title-style"> 定位:提示:你可以放大地图以便更准确标记。 </div> <div> <table class="input tabContent"> <tr> <td colspan="4" style="line-height: 25px;padding: 5px;border-bottom: 1px solid #dde9f5;"> <!-- <span class="fieldSet"> <input type="hidden" id="areaId" name="areaId" /> </span>   详细地址:<input type="text" name="address" class="txt" maxlength="200"/> --> <br/> 定位地址:<input type="text" id="searchText" style="line-height: 20px;width: 400px;border: solid 1px #dcdcdc;"/> <input type="button" value="搜索" id="searchButton" style="border: revert;"/>    详细地址:<input type="text" id="address" name="address" readonly="readonly" value="${followUpRecord.address}" style="line-height: 20px;width: 400px;border: solid 1px #dcdcdc;"/> 经度:<input type="text" name="longitude" id="mapy" readonly="readonly" value="${followUpRecord.longitude}" style="line-height: 20px;border: solid 1px #dcdcdc;"/>   纬度:<input type="text" name="latitude" id="mapx" readonly="readonly" value="${followUpRecord.latitude}" style="line-height: 20px;border: solid 1px #dcdcdc;"/> </td> </tr> <tr> <td style="height:400px;display:block;" colspan="3"> <div id="allmap"></div> </td> <td style="height:400px;"> <ul id="searchResUl"> </ul> </td> <tr> </table> </div> </div> <div class="fixed-top"> [#if followUpRecord.recordStatus == 1] [#if followUpRecord.isCloseNiche != 2] <a href="javascript:void(0);" class="button sureButton" onclick="submitData(this)">${message("提交")}</a> [/#if] <a href="javascript:void(0);" class="button sureButton" onclick="save(this)">${message("保存")}</a> [#if followUpRecord.isCloseNiche == 2 && followUpRecord.wfState == 0] <a href="javascript:void(0);" class="button sureButton" onclick="check_wf(this)">${message("12501")}</a> [/#if] [/#if] <input type="button" onclick="location.reload(true);" class="button resetButton ml15" value="${message("重置")}"> </div> </form>该代码不允许回车换行的原因及解决方案
最新发布
07-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值