# \<% %\> is not exactly the same as \<?php ?\>

**URL:** <https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549>\
**Category:** Rails\
**Created:** [May 9, 2009, 8:33am UTC](https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549 "2009-05-09T08:33:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jianlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jianlin](https://www.ruby-forum.com/u/jianlin)\
**Post date:** [May 9, 2009, 8:33am UTC](https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549/1 "2009-05-09T08:33:47Z")

</div>

it seems that \<% %\> is not exactly the same as \<?php ?\> in which,

PHP’s

\<?php echo "something" ?\>

will add to the output

but

ERB’s  
\<% puts “something” %\>

will not?

Does someone know if JSP and ASP behave like ERB or PHP and can make a  
summary of their likes and differences? Thank you.

---

<div class="post-metadata">

**Author:** ![jianlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jianlin](https://www.ruby-forum.com/u/jianlin)\
**Post date:** [May 9, 2009, 11:33am UTC](https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549/2 "2009-05-09T11:33:16Z")

</div>

Jian L. wrote:

> it seems that \<% %\> is not exactly the same as \<?php ?\> in which,
> 
> PHP’s
> 
> \<?php echo "something" ?\>
> 
> will add to the output
> 
> but
> 
> ERB’s  
> \<% puts “something” %\>
> 
> will not?

actually, i found that \<% puts 123 %\> prints 123 to the stdout. so the  
shell that is running script/server will see “123”… but the 123 is not  
part of the webpage generated. Is there a way or option to include that  
123 as part of the generated HTML? thanks.

---

<div class="post-metadata">

**Author:** ![jianlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jianlin](https://www.ruby-forum.com/u/jianlin)\
**Post date:** [May 9, 2009, 11:57am UTC](https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549/3 "2009-05-09T11:57:41Z")

</div>

\<%= “blah” %\>

is what you are looking for

RobL

> **[Posts](https://www.robl.me/)**
>
> I write thems codes

2009/5/9 Jian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> > but
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> >

–  
Rob L.  
removed\_email\_address@domain.invalid

> **[Posts](https://www.robl.me/)**
>
> I write thems codes

---

<div class="post-metadata">

**Author:** ![jianlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jianlin](https://www.ruby-forum.com/u/jianlin)\
**Post date:** [May 9, 2009, 12:07pm UTC](https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549/4 "2009-05-09T12:07:26Z")

</div>

Rob L. wrote:

> \<%= “blah” %\>
> 
> is what you are looking for

actually… sometimes i want to output multiple things inside of \<% %\>,  
or put everything inside a loop and inside a \<% %\>, so that’s why the  
question of \<% puts 123 %\>

---

<div class="post-metadata">

**Author:** ![jianlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jianlin](https://www.ruby-forum.com/u/jianlin)\
**Post date:** [May 9, 2009, 2:01pm UTC](https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549/5 "2009-05-09T14:01:47Z")

</div>

Jian L. wrote:

> actually… sometimes i want to output multiple things inside of \<% %\>,  
> or put everything inside a loop and inside a \<% %\>, so that’s why the  
> question of \<% puts 123 %\>

I think you should use something like:

\<% for x in y do %\>  
\<%= “#{x.z}, #{x.a}” %\>  
\<% end %\>

---

<div class="post-metadata">

**Author:** ![jianlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jianlin](https://www.ruby-forum.com/u/jianlin)\
**Post date:** [May 9, 2009, 2:58pm UTC](https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549/6 "2009-05-09T14:58:09Z")

</div>

putting this code into a helper method or partial will keep your views  
clean.  
[http://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html](http://api.rubyonrails.org/classes/ActionController/Helpers/ClassMethods.html)  
[http://api.rubyonrails.org/classes/ActionView/Partials.html](http://api.rubyonrails.org/classes/ActionView/Partials.html)

---

<div class="post-metadata">

**Author:** ![jianlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jianlin](https://www.ruby-forum.com/u/jianlin)\
**Post date:** [May 9, 2009, 3:05pm UTC](https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549/7 "2009-05-09T15:05:11Z")

</div>

I would suggest most of that should be in the controller not the view,  
you  
don’t want to be rescuing exceptions in the view.

Colin

2009/5/9 Jian L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![jianlin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e9bcb4/32.png) [@jianlin](https://www.ruby-forum.com/u/jianlin)\
**Post date:** [May 9, 2009, 2:36pm UTC](https://www.ruby-forum.com/t/is-not-exactly-the-same-as-php/167549/8 "2009-05-09T14:36:42Z")

</div>

Alberto S. wrote:

> Jian L. wrote:
> 
> > actually… sometimes i want to output multiple things inside of \<% %\>,  
> > or put everything inside a loop and inside a \<% %\>, so that’s why the  
> > question of \<% puts 123 %\>
> 
> I think you should use something like:
> 
> \<% for x in y do %\>  
> \<%= “#{x.z}, #{x.a}” %\>  
> \<% end %\>

so for example… if i have the code

\<%  
begin  
t = ‘’  
s = Iconv.conv(“UTF-32”, “UTF-8”, some\_utf8\_string)

(s.length / 4).times do |i|  
b3 = s[i\*4 + 2]  
b4 = s[i\*4 + 3]  
t += ("&#x" + “%02X” % b3) + ("%02X" % b4) + “;”  
end  
rescue =\> details  
t = "exception " + details  
end  
%\>

\<%= t %\>

then if i don’t want to concat the output into t first… then would it  
be a bit messy to use

\<%  
begin  
t = ‘’  
s = Iconv.conv(“UTF-32”, “UTF-8”, some\_utf8\_string)

(s.length / 4).times do |i|  
b3 = s[i\*4 + 2]  
b4 = s[i\*4 + 3]  
%\>

\<%= ("&#x" + “%02X” % b3) + ("%02X" % b4) + “;” %\>

\<%  
end  
rescue =\> details  
%\>  
\<%= "exception " + details %\>

\<%  
end  
%\>
