Jump to content

[SOLVED] Why doesn't this echo statement output what one might expect??


carlos1234

Recommended Posts

Hi there,

 

Why doesn't the following snippet output what I would expect it to output in echo case number 1?

 

<?php

echo '1. /<'."hello"."<br>";

echo "2. /<hello"."<br>";

echo "3. /\<hello"."<br>";

echo "4. /hello"."<br>";

echo "5. /<\hello"."<br>";

?>

 

What I want to see it output is "1. /<hello".  Instead I get the following output.

 

1. /

2. /

3. /\

4. /hello

5. /<\hello

 

Why is that? 

 

Anybody?

 

Thanks. 

 

Carlos

Thanks very much thebadbad!  You is bad! :)

 

I never would have guessed that the browser would try and render a "<hello" as HTML.  Just weird. 

 

But that certainly explains it. 

 

What threw me off is that some other words than "hello" added to the "<" don't cause this problem and are displayed by the browser just fine. 

 

Thanks again. 

 

Carlos

Thanks very much thebadbad!  You is bad! :)

 

TBH I often think about how lame my username is. It was just something random I came up with when creating my account, didn't know I would stay around for this long ;) But the forum rules specifically say that the staff won't change usernames, so I guess I'm stuck with it.

 

On topic, you're always better off checking the source code of your output when developing. Or alternatively set a text/plain content type header.

Yeah...I nearly always check the source code of what is output.  With the exception of when trying to debug things.  When I am debugging...I just rely on the output as it appears on the screen of my browser. 

 

This is actually the first time I encountered this kind of quirk and it had me completely, 100% stumped.  I kept looking and looking at the output, changing the code a bit here and there, looking and looking and just could not for the life of me figure out why part of the string I was trying to output kept disappearing on me. 

 

Thanks again!  You bad (sorry I couldn't help it LOL)! 

 

Carlos

I had this problem before? want to know a fix for it

;D

 

sure here you go!

 

$data = htmlentities($data);

 

that function turns any HTML tag into a non-HTML tag but still viewable very neat function you should burn it into your brain's memory cell like I have.. yes in HTML source you'll see <hello> instead of  <hello>  but it will look nice.. in rendered mode

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.