打印在单独的行数组的内容 [英] Printing contents of array on separate lines

查看:76
本文介绍了打印在单独的行数组的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用数组的试验与疑惑,为什么下面似乎并不当我运行它打印在单独的行值?

 < PHP$ my_array =阵列(stuff1,stuff2,stuff3);回声$ my_array [0]。 \\ n;
回声$ my_array [1]。 \\ n;
回声$ my_array [2]。 \\ n;?>


解决方案

从我的PHP课本:


  

一个错误往往被新的PHP程序员做(尤其是那些从
  Ç背景)是试图通过打破行文字在其浏览器
  将结束行字符(\\ n),在他们打印的字符串。至
  明白为什么这不工作,你必须区分输出
  PHP的(通常是HTML code,准备对在发送
  从输出呈现的方式上网浏览器程序)
  通过用户的浏览器。大多数浏览器程序会让自己
  有关如何分割线在HTML文本,除非你强制选择
  BR>与&LT换行符标记。尾行字符字符串中会
  放置换行符在HTML源代码的PHP发送到您的用户
  浏览器(它仍然可以用于创建可读的HTML有用
  源),但它们通常有在该文本的方式没有影响
  看起来在一个网页。


< BR> 标记是所有浏览器中正确PTED间$ P $,而 \\ n 通常只影响源$ C ​​$ c和使其更具可读性。

Experimenting with arrays and wondering why the following DOESN'T seem to print the values on SEPARATE lines when I run it?

<?php

$my_array = array("stuff1", "stuff2", "stuff3");

echo $my_array[0] . "\n";
echo $my_array[1] . "\n";
echo $my_array[2] . "\n";

?>

解决方案

From my PHP textbook:

One mistake often made by new php programmers (especially those from a C background) is to try to break lines of text in their browsers by putting end-of-line characters ("\n") in the strings they print. To understand why this doesn’t work, you have to distinguish the output of php (which is usually HTML code, ready to be sent over the Internet to a browser program) from the way that output is rendered by the user’s browser. Most browser programs will make their own choices about how to split up lines in HTML text, unless you force a line break with the <BR> tag. End-of-line characters in strings will put line breaks in the HTML source that php sends to your user’s browser (which can still be useful for creating readable HTML source), but they will usually have no effect on the way that text looks in a Web page.

The <br> tag is interpreted correctly by all browsers, whereas the \n will generally only affect the source code and make it more readable.

这篇关于打印在单独的行数组的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆