0

I was writing the code: <?PHP $where='<something'; echo $where; ?> but it doesn't echo anything until I change the string $where to '< something'(add a space after "<"), why?

It might compile < to the begining of some markup language.

4
  • Doesn't echo, or doesn't appear to echo? Check the page source. Commented Feb 16, 2013 at 2:37
  • have you tried "&lt;something" instead? Commented Feb 16, 2013 at 2:38
  • Works fine for me: codepad.org/oUVy5M1D. Commented Feb 16, 2013 at 2:50
  • I think you're missing the "big picture" that you are writing a PHP script whose output is HTML that a browser will render/process. Commented Feb 16, 2013 at 3:51

3 Answers 3

1

Less than < and greater than > signs are html entities or reserved symbols to denote html tags. To display them you need to use entity name or entity number. In case of "less than" they are < and < respectively.

So change your code like this:

$where='&lt;someting'; echo $where;

or better yet

$where='&lt;someting'; echo htmlentities($where);

as correctly suggested by @Waleed Kahn.

Sign up to request clarification or add additional context in comments.

2 Comments

Or better — echo htmlentities($where).
u might want to explain why u did that. (considering that he even asked the question)
0

In a browser, < introduces an HTML tag. If your string were $where = "<img" you'd probably end up with something like this:

Image http://example.com/

Whenever you are outputting text, you should always either run htmlentities, htmlspecialchars or just str_replace("<","&lt;",$where); to escape HTML from being processed (the last one will still allow &characters;)

Comments

0

It is because browsers treat the opening bracket, <, as an HTML tag. In the browser, right-click the page and select "View source".

If you have a terminal then you can use:

php -f file.php

That will output the raw content without parsing it.

If you want to see the code in your browser then you need to encode HTML characters using something like htmlentities():

$where = '<something';
echo htmlentities($where); // &lt;something

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.