Output does not show new lines in PHP - php

You all might say this is a foolish question submitted here but can anyone give any valid reason for the output I am getting. This is amazing and strange because "\n" forgot its work. I am getting this output
Test Test Test Test Test Test Test Test
for this code:
<?php
for($i=1;$i<5;$i++)
{
echo "TEST \n";
echo "TEST"."\n";
}
?>

You're looking at it in HTML; in HTML, new lines are treated like regular spaces. Look at the source for the page and you will see the new lines there.

If you want output in new line try this...
<?php
for($i=1;$i<5;$i++)
{
echo "TEST </br>";
echo "TEST </br>";
}
?>

Related

How do I get a newline in the PHP output from echo?

I am generating a large amount of HTML code with PHP using echo and it is all appearing on one line.
I understand that HTML ignores whitespace, and there are thousands of answers which point out that you need <BR> or a block element. But that is not my question.
My problem is that is very hard to debug my HTML source code when it is all on one line. I am using Windows 7 and the Firefox browser tool Page Source to show the source code.
For a simple example, if the text editor I use shows
<HTML><BODY>
Hello, World!
</BODY></HTML>
Then the browser source code tool shows exactly that too, and when I generate it with PHP like
<?php
echo "<HTML><BODY>";
echo "Hello, World!";
echo "</BODY></HTML>";
?>
then the browser tool shows, as you would expect,
<HTML><BODY>Hello, World!</BODY></HTML>
I want to break the lines, and have tried
<?php
echo "<HTML><BODY>\n";
echo "Hello, World!\n";
echo "</BODY></HTML>\n";
?>
and with "\r\n" and with "\xA" and also like this
<?php
echo "<HTML><BODY>" . PHP_EOL;
echo "Hello, World!" . PHP_EOL;
echo "</BODY></HTML>" . PHP_EOL;
?>
yet the content stays resolutely on a single line.
This works on windows
<?php
echo "<HTML><BODY>
";
echo "Hello, World!
";
echo "</BODY></HTML>
";
?>

How to echo php code and use it?

<?php echo $row["html"]; ?>
Inside of the $row["html"] there's:
<?php $Site->Nav($owner); ?>
but when I echo it, it only echoes:
Nav($owner); ?>
How may I print the full and make it usable, which means that it will print the function Nav?
I've tried to replace <?php with [[// i the database, and just before echoing it, I change back with replace. But without success
I think you need to use eval function of php. See the example below.
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
Might be it can help.
Use eval function. It might solve your problem like this:
<?php echo eval($row["html"]); ?>
Keep the code as is in DB as if you are writing it in PHP file but without PHP opening and closing tags i.e. <?php and ?>. I haven't checked this (as i am not sure what $Site->Nav($owner); will do) but hope it would work in this case.
If I understand correctly you are wanting to output the results of $Site->Nav($owner);
I have no idea what this is expected to output, but assuming it is a string of some kind that you wish to display (hence echo) - an example of achieving this would be calling your code and have that method return the value, so you can echo it out. Ie:
function Nav($owner){
// Do your stuff
return 'Your Desired Output';
}
Then on your page you would have
<?php echo $Site->Nav($owner); ?>
Which would echo "Your Desired Output".

echo is blank when using include statement

I wanted to try out an example you can find here: http://php.net/manual/en/function.include.php,
but I can't seem to be able to make it work.
In a first file, index1.php, I put this:
<?php
$color = 'green';
$fruit = 'apple';
?>
In the second file,index2.php, I put this:
<?php
include("http://www.domain.com/mypathtothefile/index2.php");
echo "A $color $fruit";
?>
It should echo 'A green apple', but it echos nothing.
The path is correct though, since when I put the echo part in the first file (index1.php), then it does echo 'A geen apple', both in index1.php as index2.php.
Hope someone can help.
Thanks in advance!
Shouldn't it just be:
include('index1.php');
Edit: corrected to reflect file names.

Display dynamic text based on GET parameters

I'm trying to learn PHP, and I figured I'd make myself a simple exercise of making a site that if someone goes to it, they get "Hello friend!" but if my wife (who is named Dawn) goes to it, she gets a different message.
Unfortunately, it's always showing up as blank and I'm not really sure why.
I know it works for index.html with just text, and I know it works for index.php as long as I have no <?php tag in it (just text works). But when I try to make it actual php, it just fails.
I'd like site/index.php to yield
"Hello friend!"
I'd like site/index.php?who=Bob to
yield "Hello friend!"
I'd like site/index.php?who=Dawn to
yield "Hello Dawn! I love you!"
Here's what I have:
<?php
print 'Hello ';
$who = $_GET("who");
if($who && $who == "Dawn")
print "Dawn! I love you!";
else
print "friend!";
/>
So, what's wrong?
Access to arrays ($_GET is an array), like in Java, uses square brackets:
$who = $_GET['who'];
Also if($who) evaluates to true if $who is non-false, to check it's set you need to use isset:
if(isset($who) && $who == "Dawn")
Last, as noted by #Shivan, the end tag should be ?>, not />.
Multiple issues, should be:
<?php
print 'Hello ';
$who = $_GET["who"];
if(isset($who) && $who == "Dawn") {
print 'Dawn! I love you!';
} else {
print 'friend!';
}
?>
More information:
if possible, use single quotes for faster parsing
it is a good habit to close if else case with brackets
end tag should be a ?>
try this on for size:
<?php
echo 'Hello ';
$who = isset($_GET["who"])?$_GET["who"]:false;
if($who)
echo "Dawn! I love you!";
else
echo "friend!";
?>
This checks to make sure there is a _GET value with key who or else php will throw errors.

Changing Text in PHP

I haven't found anytihng in Google or the PHP manual, believe it or not. I would've thought there would be a string operation for something like this, maybe there is and I'm just uber blind today...
I have a php page, and when the button gets clicked, I would like to change a string of text on that page with something else.
So I was wondering if I could set the id="" attrib of the <p> to id="something" and then in my php code do something like this:
<?php
$something = "this will replace existing text in the something paragraph...";
?>
Can somebody please point me in the right direction? As the above did not work.
Thank you :)
UPDATE
I was able to get it working using the following sample:
Place this code above the <html> tag:
<?php
$existing = "default message here";
$something = "message displayed if form filled out.";
$ne = $_REQUEST["name"];
if ($ne == null) {
$output = $existing;
} else {
$output = $something;
}
?>
And place the following where ever your message is to be displayed:
<?php echo $output ?>
As far as I can get from your very fuzzy question, usually you don't need string manipulation if you have source data - you just substitute one data with another, this way:
<?php
$existing = "existing text";
$something = "this will replace existing text in the something paragraph...";
if (empty($_GET['button'])) {
$output = $existing;
} else {
$output = $something;
}
?>
<html>
<and stuff>
<p><?php echo $output ?></p>
</html>
but why not to ask a question bringing a real example of what you need? instead of foggy explanations in terms you aren't good with?
If you want to change the content of the paragraph without reloading the page you will need to use JavaScript. Give the paragraph an id.<p id='something'>Some text here</p> and then use innerHTML to replace it's contents. document.getElementById('something').innerHTML='Some new text'.
If you are reloading the page then you can use PHP. One way would be to put a marker in the HTML and then use str_replace() to insert the new text. eg <p><!-- marker --></p> in the HTML and $html_string = str_replace('<!-- marker -->', 'New Text', $html_string) assuming $html_string contains the HTML to output.
If you are looking for string manipulation and conversion you can simply use the str_replace function in php.
Please check this: str_replace()
If you're using a form (which I'm assuming you do) just check if the variable is set (check the $_POST array) and use a conditional statement. If the condition is false then display the default text, otherwise display something else.

Categories