Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > PHP How-To
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old June 23rd, 2004, 11:35 AM
Authorized User
Join Date: May 2004
Posts: 18
Thanks: 0
Thanked 0 Times in 0 Posts
Default string concatenation

in the beginning php book they say to use spaces between periods for string concatenation. but i noticed that when i tryed the followimg:
echo "price range:" . " " . $_Get['price'];
echo "price range:"." ".$_GET['price'];

both ways produced the same output which was:
Price Range: under 5,000

So do i need to use spaces between the periods when concatenating or can i just do thing like above without spacing the periods out.

also for the following php page:
$Message1="Bugs Bunny";
$Message2="Homer Simpson";
$Message3="Ren & Stimpy";
echo "<FORM METHOD=GET ACTION='hidden2.php'>";
echo "Which of the following would win in a shootout?";
echo "<SELECT NAME='ListBox'>";
echo "<OPTION>$Message1</OPTION>";
echo "<OPTION>$Message2</OPTION>";
echo "<OPTION>$Message3</OPTION>";
echo "</SELECT><BR><BR>";
echo "<INPUT TYPE=HIDDEN NAME=Hidden1 VALUE='$Message1'>";
echo "<INPUT TYPE=HIDDEN NAME=Hidden2 VALUE='$Message2'>";
echo "<INPUT TYPE=HIDDEN NAME=Hidden3 VALUE='$Message3'>";
echo "</FORM>";

i noticed that if you remove the '' from the VALUE='$MESSAGE1'
then when i ran the page i would get the output:

the options were
you selected:
bugs bunny

however when i left the '' marks around the value='$MESSAGE1'

i got the output:

the options were
you selected
bugs bunny

why is it cutting off the second part of what is in $message variable when there are not quotations around the $message variable???Also why are the quotations necessary??


Old June 24th, 2004, 06:17 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 256
Thanks: 0
Thanked 0 Times in 0 Posts

No you don't need the spaces: PHP is a free-form language, and you can write things just about any way you wish, as long as it all comes in the right order and you don't actually put spaces into keywords, variable names, etc.

It's usually just easier to read, that's all.

Your second problem is a HTML one. 'value' is an attribute of the <input> element. Attributes must always be quoted, in orer to say where they start and end. Without the quotes, your browser assumes that "bunny" is some new attribute (of the kind that doesn't need to take a value - like the "selected" and "checked" attributes of the <select> list and the checkboxes/radio-buttons, respectively). The browser will not recognise this new "bunny" attribute of the <input> element, but it will ignore it, on the basis that all browsers are designed to be future-proof in this respect: the "bunny" attribute may have been introduced since the browser was released.

So why does it work at all, if elements are always supposed to be quoted? Well, like I say, attributes should always be quoted, but many coders are extremely lazy, in this respect, and leave the quotes off. The browser is then designed to cope with this by taking whatever follows the equals sign, up to the first space, as the value of the attribute. It cannot also tell that "Bunny" is also part of that value, since without the quotes, that's the limit of its intelligence. After all, what if you were to have a element like this:

<input value="Please Type Here" type="text" name="user_input" id="user_input" onClick=""this.value=''" />

Let's leave the quotes off, and see what you get:

<input value=Please Type Here type=text name=user_input id=user_input onClick=this.value='' />

... an input element with two type attributes!

FWIW, many fairly ancient browsers, like the early netscapes will chock on unquoted element attributes (in response to which, those people who have the temerity to call themselves 'programmers', and yet cannot be arsed to use the quote key, would blame the browser. Such people can and must be driven out of our industry, of course, before they convince bosses around the world, that they can replace all their real programmers, with people who will work all hours in return for bananas and free tea, and never buy computer books.)

Hope that's clear.
Take it easy,
"If Darl McBride was in charge, he'd probably make marriage unconstitutional too, since clearly it de-emphasizes the commercial nature of normal human interaction, and is a major impediment to the commercial growth of prostitution."

Similar Threads
Thread Thread Starter Forum Replies Last Post
Performance in String concatenation rushman XSLT 4 September 27th, 2007 11:02 AM
concatenation string of an input file kgoldvas XSLT 11 September 10th, 2007 08:32 AM
String concatenation query. AjayLuthria XSLT 2 March 14th, 2007 11:55 AM
string concatenation matt_99 Access VBA 2 January 17th, 2004 09:55 AM
T-SQL String Concatenation jaucourt SQL Language 2 January 13th, 2004 10:54 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.